简体   繁体   English

Ruby嵌套哈希合并

[英]Ruby Nested Hash Merge

Given something like this: 给定这样的东西:

hey = {
  some_key: {
      type: :object,
      properties: {
        id: { type: :string, example: '123', description: 'Id' },
        created_at: { type: :string, example: '2019-02-14 14:13:55'},
        updated_at: { type: :string, example: '2019-02-14 14:13:55'},
        type: { type: :string, example: 'something', description: 'Resource type' },
        token: { type: :string, example: 'token', description: 'Some description of token' }
      }
    }
}

I would like to go through all keys until I find one named properties , then mutate its content such that the keys become the value of a description key if it doesn't exit in its nested hash. 我想遍历所有键,直到找到一个命名properties ,然后对其内容进行更改,以使键(如果它不在嵌套的哈希中退出)成为description键的值。

So for the example above, the hash would end up like this: 因此,对于上面的示例,哈希将最终如下所示:

hey = {
  some_key: {
      type: :object,
      properties: {
        id: { type: :string, example: '123', description: 'Id' },
        created_at: { type: :string, example: '2019-02-14 14:13:55', description: 'Created At'},
        updated_at: { type: :string, example: '2019-02-14 14:13:55', description: 'Updated At'},
        type: { type: :string, example: 'something', description: 'Resource type' },
        token: { type: :string, example: 'token', description: 'Some description of token' }
      }
    }
}

created_at and updated_at didn't have a description. created_atupdated_at没有描述。

It should also handle if token , for instance, had a properties property. 例如,它还应该处理token是否具有properties属性。

I came up with a solution that works but I am really curious on how I can improve it? 我想出了一个可行的解决方案,但是我真的很好奇如何改进它?

My solution below: 我的解决方案如下:

def add_descriptions(hash)
  return unless hash.is_a?(Hash)
  hash.each_pair do |key, value|
    if key == :properties
      value.each do |attr, props|
        if props[:description].nil?
          props.merge!(description: attr.to_s)
        end
      end
    end
    add_descriptions(value)
  end
end

As I understand all you know about the hash hey is that it is comprised of nested hashes. 据我所知,你知道的哈希hey是,它是由嵌套哈希。

def recurse(h)
  if h.key?(:properties)
    h[:properties].each do |k,g|
      g[:description] = k.to_s.split('_').map(&:capitalize).join(' ') unless
        g.key?(:description)
    end
  else
    h.find { |k,obj| recurse(obj) if obj.is_a?(Hash) }
  end
end

recurse hey
  #=> {:id=>{:type=>:string, :example=>"123", :description=>"Id"},
  #    :created_at=>{:type=>:string, :example=>"2019-02-14 14:13:55",
  #      :description=>"Created At"},
  #    :updated_at=>{:type=>:string, :example=>"2019-02-14 14:13:55",
  #    :description=>"Updated At"},
  #    :type=>{:type=>:string, :example=>"something",
  #      :description=>"Resource type"},
  #    :token=>{:type=>:string, :example=>"token",
  #      :description=>"Some description of token"}} 

The return value is the updated value of hey . 返回值是hey的更新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM