diff --git a/lib/pbbuilder.rb b/lib/pbbuilder.rb index 1ff2c65..8a4198a 100644 --- a/lib/pbbuilder.rb +++ b/lib/pbbuilder.rb @@ -53,7 +53,7 @@ def set!(field, *args, &block) descriptor = _descriptor_for_field(name) ::Kernel.raise ::ArgumentError, "Unknown field #{name}" if descriptor.nil? - if block + if ::Kernel.block_given? ::Kernel.raise ::ArgumentError, "can't pass block to non-message field" unless descriptor.type == :message if descriptor.label == :repeated @@ -61,13 +61,12 @@ def set!(field, *args, &block) ::Kernel.raise ::ArgumentError, "wrong number of arguments #{args.length} (expected 1)" unless args.length == 1 collection = args.first _append_repeated(name, descriptor, collection, &block) - return + else + # pb.field { pb.name "hello" } + ::Kernel.raise ::ArgumentError, "wrong number of arguments (expected 0)" unless args.empty? + message = (@message[name] ||= _new_message_from_descriptor(descriptor)) + _scope(message, &block) end - - ::Kernel.raise ::ArgumentError, "wrong number of arguments (expected 0)" unless args.empty? - # pb.field { pb.name "hello" } - message = (@message[name] ||= _new_message_from_descriptor(descriptor)) - _scope(message, &block) elsif args.length == 1 arg = args.first if descriptor.label == :repeated @@ -123,11 +122,11 @@ def extract!(element, *args) def merge!(object) ::Kernel.raise Pbbuilder::MergeError.build(target!, object) unless object.class == ::Hash - object.each_key do |key| - next if object[key].respond_to?(:empty?) && object[key].empty? + object.each do |key, value| + next if value.respond_to?(:empty?) && value.empty? descriptor = _descriptor_for_field(key) - ::Kernel.raise ::ArgumentError, "Unknown field #{name}" if descriptor.nil? + ::Kernel.raise ::ArgumentError, "Unknown field #{key}" if descriptor.nil? if descriptor.label == :repeated # optional empty fields don't show up in @message object, @@ -136,48 +135,48 @@ def merge!(object) @message[key.to_s] = _new_message_from_descriptor(descriptor) end - if object[key].respond_to?(:to_hash) - object[key].to_hash.each {|k, v| @message[key.to_s][k] = v} - elsif object[key].respond_to?(:to_ary) - elements = object[key].map do |obj| + if value.respond_to?(:to_hash) + value.to_hash.each {|k, v| @message[key.to_s][k] = v} + elsif value.respond_to?(:to_ary) + elements = value.map do |obj| descriptor.subtype ? descriptor.subtype.msgclass.new(obj) : obj end @message[key.to_s].replace(elements) end else - if object[key].class == ::String + if value.class == ::String # pb.fields {"one" => "two"} - @message[key.to_s] = object[key] - elsif object[key].class == ::TrueClass || object[key].class == ::FalseClass + @message[key.to_s] = value + elsif value.class == ::TrueClass || value.class == ::FalseClass # pb.boolean true || false - @message[key.to_s] = object[key] - elsif object[key].class == ::Array + @message[key.to_s] = value + elsif value.class == ::Array # pb.field_name do # pb.tags ["ok", "cool"] # end - @message[key.to_s] = object[key] - elsif object[key].class == ::Hash + @message[key.to_s] = value + elsif descriptor.type == :message if @message[key.to_s].nil? @message[key.to_s] = _new_message_from_descriptor(descriptor) end - object[key].each do |k, v| - if object[key][k].respond_to?(:to_hash) + value.each do |k, v| + if value[k].respond_to?(:to_hash) if @message[key.to_s][k.to_s].nil? descriptor = @message[key.to_s].class.descriptor.lookup(k.to_s) @message[key.to_s][k.to_s] = _new_message_from_descriptor(descriptor) end - _scope(@message[key.to_s][k.to_s]) { self.merge!(object[key][k]) } - elsif object[key][k].respond_to?(:to_ary) - @message[key.to_s][k.to_s].replace object[key][k] + _scope(@message[key.to_s][k.to_s]) { self.merge!(value[k]) } + elsif value[k].respond_to?(:to_ary) + @message[key.to_s][k.to_s].replace value[k] else # Throws an error, if we try to merge nil object into empty value. - next if object[key][k].nil? && @message[key.to_s][k.to_s].nil? + next if value[k].nil? && @message[key.to_s][k.to_s].nil? - @message[key.to_s][k.to_s] = object[key][k] + @message[key.to_s][k.to_s] = value[k] end end end