格式化数据哈希的最有效方法?

发布于 2024-10-26 21:36:11 字数 445 浏览 2 评论 0原文

我正在使用这个哈希数组将批量插入到 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记录就不需要一个。有什么想法吗?

I am using this array of hashes to do a batch insert into a mongo DB. Each hash was populated by parsing a text file so the formatting of fields are in an unpredictable format. It might look something like:

{date => "March 5", time => "05:22:21", first_name = "John", middle_initial = "JJ", ...}

And I would have a series of formatting functions. So maybe:

def format_date
..convert if needed..
end

def format_time
...
end

How would I go about calling the formatting functions on various records? I could see doing some kind of lambda call where I iterate through the hash and call a format_record_name function, but not all records will have formatting functions. For instance above the first_name record wouldn't need one. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

子栖 2024-11-02 21:36:11

只需保留您想要处理的键的列表即可。您甚至可以使用哈希将其与转换函数联系起来:

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

Just keep a list of the keys that you do want to handle. You could even tie it to the transformation functions with a 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
隱形的亼 2024-11-02 21:36:11

这是一个想法,与您所说的非常相似。您可能只有一个用于不想格式化的字段的标识函数

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 }

Here's one idea, pretty similar to what you stated. You might just have an identity function for the fields you don't want to format

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 }
找回味觉 2024-11-02 21:36:11

利用 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"}] 

Make use of Ruby's Singleton (or Eigen) class and then the following one liner solves your problem:

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"}] 
匿名。 2024-11-02 21:36:11
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文