Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/mediator/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def many name, options = {}, &block
options = {construct: true}.merge options

data = get(name, options)
return if data.nil? and !options[:empty] # Can't use main empty? call here as [] does not count as empty..

if data.nil?
mediator.set name, nil if has?(name) and options[:empty]
return
end

mediator.set name, [] unless options[:merge]

Expand All @@ -72,6 +76,12 @@ def one name, options = {}, &block
options = {construct: true}.merge options

data = get name, options

if data.nil?
mediator.set name, nil if has?(name) and options[:empty]
return
end

subj = options[:subject] || mediator.get(name, options)

sub subj, data, options, &block
Expand Down
2 changes: 1 addition & 1 deletion mediator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
gem.test_files = `git ls-files -- test/*`.split "\n"
gem.name = "mediator"
gem.require_paths = ["lib"]
gem.version = "0.5.2"
gem.version = "0.5.3"

gem.required_ruby_version = ">= 1.9.2"

Expand Down
69 changes: 64 additions & 5 deletions test/mediator_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
p.id :foo, empty: true
assert_equal "", @subject.foo_id
end

it "Does not consider 0 empty" do
p = Mediator::Parser.new @mediator, foo: 0
@subject.foo_id = 42

p.id :foo
assert_equal 0, @subject.foo_id
end
end

describe "ids" do
Expand Down Expand Up @@ -159,15 +167,16 @@

describe "one" do
before do
Top = Class.new OpenStruct
Nest = Class.new OpenStruct
Top ||= Class.new OpenStruct
Nest ||= Class.new OpenStruct

Class.new Mediator do
accept Top

def parse! p
p.key :foo
p.one :nest
p.one :may_be_empty, empty: true
end
end

Expand All @@ -194,6 +203,31 @@ def parse! p
assert_equal d[:nest][:baz], s.nest.baz
assert_equal d[:nest][:quux], s.nest.quux
end

it "sets to nil when empty: true is used and a value is passed" do
s = Top.new
s.may_be_empty = Nest.new bla: :blo

m = Mediator[s]
d = { may_be_empty: nil }

m.parse d

assert_nil s.may_be_empty
end

it "does nothing when empty: true is used but no value is passed" do
s = Top.new
may_be_empty = Nest.new bla: :blo
s.may_be_empty = may_be_empty

m = Mediator[s]
d = { foo: "bar" }

m.parse d

assert_equal may_be_empty, s.may_be_empty
end
end

describe "many" do
Expand All @@ -207,11 +241,12 @@ def parse! p
def parse! p
p.many :foos
p.many :merge_foos, merge: true
p.many :may_be_empty, empty: true
end

def construct name
# name is either foo, replace_foo, replace_foos or foos..
subject.foos ||= []
subject.foos ||= []
subject.merge_foos ||= []

return subject.send(name) if subject.respond_to?(name)
Expand Down Expand Up @@ -268,6 +303,30 @@ def parse! p
assert_equal ["bar"], s.foos.map(&:baz)
end

it "sets to nil when empty: true is used and a value is passed" do
s = Bar.new may_be_empty: [ Foo.new(baz: "bar") ]

m = Mediator[s]
d = { may_be_empty: nil }

m.parse d

assert_nil s.may_be_empty
end

it "does nothing when empty: true is used but no value is passed" do
s = Bar.new
may_be_empty = [ Foo.new(baz: "bar") ]
s.may_be_empty = may_be_empty

m = Mediator[s]
d = { foos: [ { baz: "bar" } ] }

m.parse d

assert_equal may_be_empty, s.may_be_empty
end

it "does something with []" do
s = Bar.new foos: [ Foo.new(baz: "bar") ]

Expand Down Expand Up @@ -355,7 +414,7 @@ def parse! p

describe "hash" do
it "should parse hash with no exclude" do
Hashie = Class.new OpenStruct
Hashie ||= Class.new OpenStruct

Class.new Mediator do
accept Hashie
Expand All @@ -374,7 +433,7 @@ def parse! p
end

it "should parse hash with exclude" do
Hashie = Class.new OpenStruct
Hashie ||= Class.new OpenStruct

Class.new Mediator do
accept Hashie
Expand Down