diff --git a/lib/pbbuilder.rb b/lib/pbbuilder.rb index 18a7358..44d46ec 100644 --- a/lib/pbbuilder.rb +++ b/lib/pbbuilder.rb @@ -75,6 +75,7 @@ def set!(field, *args, &block) if arg.respond_to?(:to_hash) # example syntax that should end up here: # pb.fields {"one" => "two"} + arg.to_hash.each { |k, v| @message[name][k] = v } elsif arg.respond_to?(:to_ary) && !descriptor.type.eql?(:message) # pb.fields ["one", "two"] @@ -88,11 +89,13 @@ def set!(field, *args, &block) else # example syntax that should end up here: # pb.fields "one" - @message[name].push arg + + @message[name].replace Array.wrap(arg) end else # example syntax that should end up here: # pb.field "value" + @message[name] = arg end else diff --git a/test/pbbuilder_test.rb b/test/pbbuilder_test.rb index fca62eb..ba86f1b 100644 --- a/test/pbbuilder_test.rb +++ b/test/pbbuilder_test.rb @@ -25,7 +25,7 @@ class PbbuilderTest < ActiveSupport::TestCase assert_equal "Hello world", person.name assert_equal "Friend #1", person.friends.first.name - assert_equal ["ok", "that's", "cool"], person.field_mask.paths + assert_equal ["cool"], person.field_mask.paths assert_equal "Manuelo", person.best_friend.name assert_equal "Eggs", person.favourite_foods["Breakfast"] end @@ -45,6 +45,17 @@ class PbbuilderTest < ActiveSupport::TestCase assert_equal(["ok", "that's"], p.field_mask.paths) end + + test "Would not stack duplicated values, uses last one defined" do + person = Pbbuilder.new(API::Person.new) do |pb| + pb.field_mask do + pb.paths ["ok", "that's"] + pb.paths ["cool"] + end + end.target! + + assert_equal ["cool"], person.field_mask.paths + end test "it can extract fields in a nice way" do klass = Struct.new(:name)