我正在使用这个哈希数组来批量插入到mongo DB中。通过解析文本文件来填充每个哈希,因此字段的格式是不可预测的格式。它可能看起来像:
{date => "March 5", time => "05:22:21", first_name = "John", middle_initial = "JJ", ...}
我会有一系列格式化功能。也许吧:
def format_date
..convert if needed..
end
def format_time
...
end
我如何在各种记录上调用格式化函数?我可以看到做一些lambda调用,我遍历散列并调用format_record_name函数,但并非所有记录都有格式化函数。例如,first_name记录上面不需要一个。有什么想法吗?
答案 0 :(得分:2)
只需保留您想要处理的密钥列表。您甚至可以使用Hash将其与转换函数联系起来:
transformations = {
:date => lambda {|date| whatever},
:time => lambda {|time| whatever}
}
transformations.default = lambda {|v| v}
data.map do |hash|
Hash[ hash.map {|key, val| transformations[key][val] } ]
end
答案 1 :(得分:1)
这是一个想法,非常类似于你所说的。您可能只为您不想格式化的字段设置了标识功能
def pass(x)
x
end
method_hash = {:date=>method(:your_format_date)}
method_hash.default = method(:pass)
x = {:date => "March 5", :time => "05:22:21", :first_name => "John", :middle_initial => "JJ"}
x.reduce({}) { |hsh,k| hsh[k[0]] = method_hash[k[0]].call(k[1]); hsh }
答案 2 :(得分:1)
利用Ruby的Singleton(或Eigen)类,然后下面的一个内容解决了你的问题:
module Formatter
def format_date
Date.parse(self[:date]).strftime('%Y-%m-%d')
end
def format_time
self[:time].split(':')[0,2].join('-')
end
def format_first_name
self[:first_name].upcase
end
def format
{:date => format_date, :time => format_time, :first_name => format_first_name, :last_name => self[:last_name]}
end
end
records = [
{:date => 'March 05', :time => '12:13:00', :first_name => 'Wes', :last_name => 'Bailey'},
{:date => 'March 06', :time => '09:15:11', :first_name => 'Joe', :last_name => 'Buck'},
{:date => 'March 07', :time => '18:35:48', :first_name => 'Troy', :last_name => 'Aikmen'},
]
records.map {|h| h.extend(Formatter).format}
=> [{:date=>"2011-03-05", :time=>"12-13", :first_name=>"WES", :last_name=>"Bailey"},
{:date=>"2011-03-06", :time=>"09-15", :first_name=>"JOE", :last_name=>"Buck"},
{:date=>"2011-03-07", :time=>"18-35", :first_name=>"TROY", :last_name=>"Aikmen"}]
答案 3 :(得分:0)
class Formatters
def self.time(value)
"FORMATTED TIME"
end
def self.date(value)
"FORMATTED DATE"
end
def self.method_missing(name, arg)
arg
end
end
your_data = [{:date => "March 5", :time => "05:22:21", :first_name => "John", :middle_initial => "JJ"},
{:date => "March 6", :time => "05:22:22", :first_name => "Peter", :middle_initial => "JJ"},
{:date => "March 7", :time => "05:22:23", :first_name => "Paul", :middle_initial => "JJ"}]
formatted_data = your_data.map do |item|
Hash[ *item.map { |k, v| [k, Formatters.send(k, v)] }.flatten ]
end