Skip to content

Commit

Permalink
Evaluate :as option using the represented instance
Browse files Browse the repository at this point in the history
  • Loading branch information
avit committed Nov 5, 2017
1 parent 19fa98e commit 5ada798
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/roar/json/hal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def to_hash(*)
super.tap do |hash|
embedded = {}
representable_attrs.find_all do |dfn|
name = dfn[:as] ? dfn[:as].(nil) : dfn.name # DISCUSS: should we simplify that in Representable?
name = dfn[:as] ? dfn[:as].(represented) : dfn.name # DISCUSS: should we simplify that in Representable?
next unless dfn[:embedded] and fragment = hash.delete(name)
embedded[name] = fragment
end
Expand Down
3 changes: 3 additions & 0 deletions test/hal_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class JsonHalTest < MiniTest::Spec
property :name
end

property :units, as: proc { media_type }

collection :songs, as: :my_songs, class: Song, embedded: true do
property :title
end
Expand All @@ -167,6 +169,7 @@ class JsonHalTest < MiniTest::Spec

it { decorator_class.new(Album.new(Artist.new("Bare, Jr."), [Song.new("Tobacco Spit")])).to_hash.must_equal({"_embedded"=>{"my_artist"=>{"name"=>"Bare, Jr."}, "my_songs"=>[{"title"=>"Tobacco Spit"}]}}) }
it { decorator_class.new(Album.new).from_hash({"_embedded"=>{"my_artist"=>{"name"=>"Bare, Jr."}, "my_songs"=>[{"title"=>"Tobacco Spit"}]}}).inspect.must_equal "#<struct JsonHalTest::Album artist=#<struct JsonHalTest::Artist name=\"Bare, Jr.\">, songs=[#<struct JsonHalTest::Song title=\"Tobacco Spit\">]>" }
it { decorator_class.new(Album.new(units: 2, media_type: "discs")).to_hash.must_equal({"discs" => 2}) }
end
end

Expand Down

0 comments on commit 5ada798

Please sign in to comment.