Skip to content

Commit

Permalink
Merge fa070ad into b74c821
Browse files Browse the repository at this point in the history
  • Loading branch information
blanquer committed Aug 11, 2016
2 parents b74c821 + fa070ad commit 5dfd39f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: ruby
cache: bundler
sudo: false
rvm:
- 2.2.3
- 2.2.5
- 2.3.1
script:
- bundle exec rspec spec
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Include Attributor::Dumpable in Blueprint, so it renders (semi) correctly if
rendered with just `true` specified for fields.
* Fix bug rendering subobjects with nil values (manifested when `include_nil: true` there’s an explicit subsection of fields)

## 3.2

Expand Down
4 changes: 2 additions & 2 deletions lib/praxis-blueprints/blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ def validate(context = Attributor::DEFAULT_ROOT_CONTEXT)
if value.respond_to?(:validating) # really, it's a thing with sub-attributes
next if value.validating
end
errors.push(*sub_attribute.validate(value, sub_context))
errors.concat(sub_attribute.validate(value, sub_context))
end
self.class.attribute.type.requirements.each do |req|
validation_errors = req.validate(keys_with_values, context)
errors.push(*validation_errors) unless validation_errors.empty?
errors.concat(validation_errors) unless validation_errors.empty?
end
errors
ensure
Expand Down
6 changes: 5 additions & 1 deletion lib/praxis-blueprints/config_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def to_hash
@hash
end

def method_missing(name, value, *rest, &block)
def respond_to_missing?(_method_name, _include_private = false)
true
end

def method_missing(name, value, *rest, &block) # rubocop:disable Style/MethodMissing
if (existing = @hash[name])
if block
existing << [value, block]
Expand Down
2 changes: 1 addition & 1 deletion lib/praxis-blueprints/field_expander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def expand_with_member_attribute(object, fields = true)
new_fields = fields.is_a?(Array) ? fields[0] : fields

result = [expand(object.member_attribute.type, new_fields)]
history[object][fields].push(*result)
history[object][fields].concat(result)

result
end
Expand Down
5 changes: 4 additions & 1 deletion lib/praxis-blueprints/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def _render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEX
raise Attributor::DumpError, context: context, name: key, type: object.class, original_exception: e
end

next if value.nil? && !include_nil
if value.nil?
hash[key] = nil if self.include_nil
next
end

if subfields == true
hash[key] = case value
Expand Down
64 changes: 64 additions & 0 deletions spec/praxis-blueprints/config_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Praxis::ConfigHash do
subject(:instance) { Praxis::ConfigHash.new(hash, &block) }
let(:hash) { { one: ['existing'], two: 'dos' } }
let(:block) do
proc { 'abc' }
end

context 'initialization' do
it 'saves the passed hash' do
expect(subject.hash).to be(hash)
end
end

context '.from' do
subject(:instance) { Praxis::ConfigHash.from(hash, &block) }
it 'returns an instance' do
expect(subject).to be_kind_of(Praxis::ConfigHash)
expect(subject.hash).to be(hash)
end
end

context '#to_hash' do
let(:block) do
proc { hash['i_was'] = 'here' }
end
it 'evaluates the block and returns the resulting hash' do
expect(subject.to_hash).to eq(subject.hash)
expect(subject.hash['i_was']).to eq('here')
end
end

context '#method_missing' do
context 'when keys do not exist in the hash key' do
it 'sets a single value to the hash' do
subject.some_name 'someval'
expect(subject.hash[:some_name]).to eq('someval')
end
it 'sets a multiple values to the hash key' do
subject.some_name 'someval', 'other1', 'other2'
expect(subject.hash[:some_name]).to include('someval', 'other1', 'other2')
end
end
context 'when keys already exist in the hash key' do
it 'adds one value to the hash' do
subject.one'newval'
expect(subject.hash[:one]).to match_array(%w(existing newval))
end
it 'adds multiple values to the hash key' do
subject.one 'newval', 'other1', 'other2'
expect(subject.hash[:one]).to match_array(%w(existing newval other1 other2))
end
context 'when passing a value and a block' do
let(:my_block) { proc {} }
it 'adds the tuple to the hash key' do
subject.one 'val', &my_block
expect(subject.hash[:one]).to include(['val', my_block])
end
end
end
end
end
6 changes: 6 additions & 0 deletions spec/praxis-blueprints/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@

context 'with include_nil: true' do
let(:renderer) { Praxis::Renderer.new(include_nil: true) }
let(:address) { nil }

it 'renders attributes with nil values' do
output.should have_key :email
Expand All @@ -108,6 +109,11 @@
output.should have_key :work_address
output[:work_address].should be nil
end

it 'renders nil directly for nil subobjects' do
output.should have_key :address
output[:address].should be nil
end
end

context '#render_collection' do
Expand Down

0 comments on commit 5dfd39f

Please sign in to comment.