Skip to content

Commit

Permalink
Merge pull request #31 from ajsharp/nil-objects
Browse files Browse the repository at this point in the history
Breaking Change: Properly handle nil objects
  • Loading branch information
ajsharp committed Apr 11, 2013
2 parents 818f49b + 43e7feb commit c1d5a66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
19 changes: 9 additions & 10 deletions lib/bldr/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ def object(base = nil, &block)
value = nil
end

# Short circuit here if the object passed in pointed
# at a nil value. There's some debate about how this
# should behave by default -- should it build the keyspace,
# pointing a null value, or should it leave the key out.
# With this implementation, it leaves the keyspace out.
return nil if value.nil? and keyed_object?(base)
# handle nil objects
if value.nil? && keyed_object?(base)
merge_result!(key, nil)
return self
end

node = Node.new(value, opts.merge(:parent => self), &block)
merge_result!(key, node.result)
Expand Down Expand Up @@ -160,7 +159,7 @@ def collection(items, &block)
else
@result = massage_value(vals)
end

self
end

Expand Down Expand Up @@ -246,7 +245,7 @@ def attribute(*args,&block)
# object :person => dude do
# template "path/to/template"
# end
#
#
# @example Using locals
# object :person => dude do
# template "path/to/template", :locals => {:foo => 'bar'}
Expand Down Expand Up @@ -285,7 +284,7 @@ def copy_instance_variables_from(object)
def keyed_object?(obj)
obj.respond_to?(:keys)
end

def find_template(template)
path = []
path << views if views
Expand All @@ -312,6 +311,6 @@ def massage_value(val)
val
end
end

end
end
19 changes: 15 additions & 4 deletions spec/unit/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ module Bldr
end

describe Node, "#object" do
it 'renders the object structure for a nil object' do
node = Node.new do
object :person => nil do
attributes :name
end
attribute(:foo) { "bar" }
end
node.result.should == {
person: nil,
foo: 'bar'
}
end

it 'is passes block the block variable to the block' do
denver = Person.new('John Denver')
node = Node.new do
Expand Down Expand Up @@ -231,7 +244,6 @@ def wrap(&block)
end
end


describe "#attributes" do
describe "when an object key is passed a null value" do
subject {
Expand All @@ -250,13 +262,14 @@ def wrap(&block)
}.not_to raise_error(ArgumentError, ERROR_MESSAGES[:attributes_inferred_missing])
end

its(:result) { should == {} }
its(:result) { should == {:person => nil} }
end

it "renders each argument against the inferred object" do
node = wrap { attributes(:name, :age) }
node.result.should == {:person => {:name => 'alex', :age => 25}}
end

it "renders nil attributes" do
node = node_wrap do
object :person => Person.new('alex') do
Expand All @@ -266,9 +279,7 @@ def wrap(&block)

node.result.should == {:person => {:name => 'alex', :age => nil}}
end

end

end

describe "embedded objects" do
Expand Down

0 comments on commit c1d5a66

Please sign in to comment.