Skip to content

Commit

Permalink
Call object.dump in rendered for Dumpable instances
Browse files Browse the repository at this point in the history
When fully dumping an object (i.e., when no subfields were selected), that is not a Blueprint, ensure we call `object.dump` if they have the Attributor::Dumpable module.


Signed-off-by: Josep M. Blanquer <blanquer@rightscale.com>
  • Loading branch information
blanquer committed Jan 27, 2016
1 parent 9962d0d commit 2a64bd0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## next

* Ensure we call `object.dump` in Renderer when dumping a Attributor::Hash or collection of Attributor::Hash if no subfields were selected.
* Ensure we call `object.dump` in Renderer when fully dumping an instance (or array of instances) that have the Attributor::Dumpable module (i.e., when no subfields were selected)


## 3.1

Expand Down
4 changes: 2 additions & 2 deletions lib/praxis-blueprints/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
def _render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT)
if fields == true
return case object
when Attributor::Hash
when Attributor::Dumpable
object.dump
else
object
Expand All @@ -82,7 +82,7 @@ def _render(object, fields, view=nil, context: Attributor::DEFAULT_ROOT_CONTEXT)

if subfields == true
hash[key] = case value
when Attributor::Hash
when Attributor::Dumpable
value.dump
else
value
Expand Down
33 changes: 30 additions & 3 deletions spec/praxis-blueprints/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

let(:address) { Address.example }
let(:prior_addresses) { 2.times.collect { Address.example } }
let(:alias_one) { FullName.example }
let(:alias_two) { FullName.example }
let(:aliases) { [alias_one, alias_two] }
let(:metadata_hash) { { something: 'here' } }
let(:metadata) { Attributor::Hash.load( metadata_hash ) }

let(:person) do
Person.example(
address: address,
email: nil,
prior_addresses: prior_addresses,
alive: false,
work_address: nil
work_address: nil,
aliases: aliases,
metadata: metadata
)
end

Expand All @@ -27,7 +35,9 @@
},
prior_addresses: [{name: true}],
work_address: true,
alive: true
alive: true,
metadata: true,
aliases: [true]
}
end

Expand All @@ -36,7 +46,7 @@
subject(:output) { renderer.render(person, fields) }

it 'renders existing attributes' do
output.keys.should match_array([:name, :full_name, :alive, :address, :prior_addresses])
output.keys.should match_array([:name, :full_name, :alive, :address, :prior_addresses, :metadata, :aliases])

output[:name].should eq person.name
output[:full_name].should eq({first: person.full_name.first, last: person.full_name.last})
Expand All @@ -50,6 +60,23 @@

expected_prior_addresses = prior_addresses.collect { |addr| {name: addr.name} }
output[:prior_addresses].should match_array(expected_prior_addresses)

expected_aliases = aliases.collect { |the_alias| the_alias.dump }
output[:aliases].should match_array( expected_aliases )

output[:metadata].should eq( metadata.dump )
end

context 'calls dump for non-Blueprint, but still Dumpable instances' do
it 'when rendering them in full as array members' do
alias_one.should_receive(:dump).and_call_original
output[:aliases].first.should eq( first: alias_one.first, last: alias_one.last )
end
it 'when rendering them in full as leaf object' do
metadata.should_receive(:dump).and_call_original
output[:metadata].should eq( metadata_hash )
end

end

it 'does not render attributes with nil values' do
Expand Down
1 change: 1 addition & 0 deletions spec/support/spec_blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Person < Praxis::Blueprint
attribute :alive, Attributor::Boolean, default: true
attribute :myself, Person
attribute :friends, Attributor::Collection.of(Person)
attribute :metadata, Attributor::Hash
end

view :default do
Expand Down

0 comments on commit 2a64bd0

Please sign in to comment.