处理数据,计算并创建新的哈希

时间:2015-04-30 11:48:23

标签: ruby-on-rails ruby arrays hash ruby-2.0

我需要将下面的intermediate_hash转换为required_hash

参考数组

ref_arr = [{"ver"=>"3.2", "start"=>"2014/2/12", "end"=>"2014/2/20", "id"=>"345"}, {"ver"=>"3.3", "start"=>"2014/3/19", "end"=>"2014/4/3", "id"=>"233"}]

中级哈希

intermediate_hash = {
   "ver": {
      "abc-xxxx": {
         "'Process Start Date'":  "2014/1/10",
         "'Ready Start Date'": "2014/1/12",
         "'Product'": "Lumia-720",
       },
      "adc-xxxx": {
         "'Process Start Date'":  "2014/2/15",
         "'Ready Start Date'": "2014/2/17",
         "'Product'": "iphone5",
       },
      "efg-xxxx": {
         "'Process Start Date'":  "2013/12/10",
         "'Ready Start Date'": "2013/12/12",
         "'Product'": "htc",
       },
      "ghi-xxxx": {
         "'Process Start Date'":  "2013/10/3",
         "'Ready Start Date'": "2013/10/7",
         "'Product'": "iphone5",
       }, 
      "jkl-xxxx": {
         "'Process Start Date'":  "2013/10/23",
         "'Ready Start Date'": "2013/10/24",
         "'Product'": "Lumia-720",
       },
      "mno-xxxx": {
         "'Process Start Date'":  "2013/11/3",
         "'Ready Start Date'": "2013/11/4",
         "'Product'": "iphone5",
       },
    ...
   }
}

必需的哈希

result = {
"3.2": {
    "lumia-720": {
        "'Before process QA'": 2
        "'After process'": 0,
    },
   "iphone5": {
        "'Before process QA'": 2,
        "'After process'": 1,
    },
    "htc": {
        "'Before process QA'": 1
        "'After process'": 0,
    }, 
  },
"3.3":..
}

比较规则

在处理之前: reference_hash [:start]< = intermediate_hash [:process_start_date]

处理后: reference_hash [:start]> intermediate_hash [:process_start_date]

我的方法到现在为止:

第一步:我尝试group_by product使用此代码。

hash = JSON.parse(File.read('details.json'))
summary = {}
hash.map do |x| 
 summary[x[0]] = {}
 summary[x[0]] = x[1].group_by{|x| x[1]['Product']}.each{|_, v| v.map!{|h| h[1]}}
end

所以,我得到了grouped by产品的最终回复。但我无法理解如何有效地生成intermediate_hashrequired_hash。因为我的尝试是多级迭代。有人可以帮助我。 ?

提前致谢

0 个答案:

没有答案