From: Josh Cheek Date: 2013-07-22T03:31:05+09:00 Subject: Re: ActiveSupport undercore behaviour --001a11c366820c3a3c04e209c3c4 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jul 21, 2013 at 7:03 AM, Serguei Cambour wrote: > I changed as follows and it works for the first nest level for the > moment: > > def format_hash_key(hash) > Hash[hash.map {|k, v| [ActiveSupport::Inflector.underscore(k.to_s), v] > }] > end > > arr = [] > TECHS.each_value do |array_values| > array_values.each do |hash| > arr << format_hash_key(hash) > end > end > puts arr > > Output: > > > {"assigned_name"=>"P1", "export_price"=>"USD", "id"=>"1", > "delivery"=>"Y", "registered_members"=>[{"Name"=>"Admin", "Id"=>"3", > "AvailablePosts"=>[{"Seat"=>"2", "Colour"=>18}, {"Seat"=>"3", > "Colour"=>19}, {"Seat"=>"4", "Colour"=>181}, {"Seat"=>"5", > "Colour"=>183}]}]} > > Is there any elegant way to call the same method for other nested levels > (values) ? > > Thank you. > > -- > Posted via http://www.ruby-forum.com/. > > I'd probably do it like this. (I like using the word "json" in the name b/c it makes it informs the caller of what the data structure can contain) require 'active_support/inflector' TECHS = {"platform"=> [{"AssignedName"=>"P1", "ExportPrice"=>"USD", "Id"=>"1", "Delivery"=>"Y", "RegisteredMembers"=>[ {"Name"=>"Admin", "Id"=>"3", "AvailablePosts"=>[ {"Seat"=>"2", "Colour"=>18, }, {"Seat"=>"3", "Colour"=>19, }, {"Seat"=>"4", "Colour"=>181, }, {"Seat"=>"5", "Colour"=>183, } ] } ] }] } def normalize_json(deserialized_json) case deserialized_json when Array deserialized_json.map { |v| normalize_json v } when Hash deserialized_json.each_with_object Hash.new do |(k, v), new_hash| new_hash[ActiveSupport::Inflector.underscore(k)] = normalize_json(v) end else deserialized_json end end require 'pp' pp normalize_json TECHS # >> {"platform"=> # >> [{"assigned_name"=>"P1", # >> "export_price"=>"USD", # >> "id"=>"1", # >> "delivery"=>"Y", # >> "registered_members"=> # >> [{"name"=>"Admin", # >> "id"=>"3", # >> "available_posts"=> # >> [{"seat"=>"2", "colour"=>18}, # >> {"seat"=>"3", "colour"=>19}, # >> {"seat"=>"4", "colour"=>181}, # >> {"seat"=>"5", "colour"=>183}]}]}]} --001a11c366820c3a3c04e209c3c4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sun, Jul 21, 2013 at 7:03 AM, Serguei Cambour <lists@ruby-forum.com<= /a>> wrote:

I'd probably do it l= ike this. (I like using the word "json" in the name b/c it makes = it informs the caller of what the data structure can contain)

require 'active_support/inflector'

TECHS =3D {"platform"=3D>
=A0 [{"AssignedNam= e"=3D>"P1", "ExportPrice"=3D>"USD"= , "Id"=3D>"1",
=A0 =A0 "Delivery"=3D>"Y",
=A0 =A0 &q= uot;RegisteredMembers"=3D>[
=A0 =A0 =A0 {"Name"= =3D>"Admin", "Id"=3D>"3",
=A0= =A0 =A0 =A0 "AvailablePosts"=3D>[
=A0 =A0 =A0 =A0 =A0 {"Seat"=3D>"2", "Colou= r"=3D>18, },
=A0 =A0 =A0 =A0 =A0 {"Seat"=3D>= "3", "Colour"=3D>19, },
=A0 =A0 =A0 =A0 = =A0 {"Seat"=3D>"4", "Colour"=3D>181, },=
=A0 =A0 =A0 =A0 =A0 {"Seat"=3D>"5", "Colou= r"=3D>183, }
=A0 =A0 =A0 =A0 ]
=A0 =A0 =A0 }
=A0 =A0 ]
=A0 }]
}

def= normalize_json(deserialized_json)
=A0 case deserialized_json
=A0 when Array
=A0 =A0 = deserialized_json.map { |v| normalize_json v }
=A0 when Hash
=A0 =A0 deserialized_json.each_with_object Hash.new do |(k, v), new_h= ash|
=A0 =A0 =A0 new_hash[ActiveSupport::Inflector.underscore(k)] =3D norma= lize_json(v)
=A0 =A0 end
=A0 else
=A0 =A0 des= erialized_json
=A0 end
end

req= uire 'pp'
pp normalize_json TECHS

# >> {"pla= tform"=3D>
# >> =A0 [{"assigned_name"=3D&= gt;"P1",
# >> =A0 =A0 "export_price"=3D= >"USD",
# >> =A0 =A0 "id"=3D>"1",
# >= ;> =A0 =A0 "delivery"=3D>"Y",
# >>= ; =A0 =A0 "registered_members"=3D>
# >> =A0 = =A0 =A0[{"name"=3D>"Admin",
# >> =A0 =A0 =A0 =A0"id"=3D>"3",
# >> =A0 =A0 =A0 =A0"available_posts"=3D>
# = >> =A0 =A0 =A0 =A0 [{"seat"=3D>"2", "colo= ur"=3D>18},
# >> =A0 =A0 =A0 =A0 =A0{"seat"=3D>"3", "= colour"=3D>19},
# >> =A0 =A0 =A0 =A0 =A0{"seat= "=3D>"4", "colour"=3D>181},
# >= > =A0 =A0 =A0 =A0 =A0{"seat"=3D>"5", "colour= "=3D>183}]}]}]}

--001a11c366820c3a3c04e209c3c4--