lib/avro/builder/field.rb in avro-builder-0.2.0 vs lib/avro/builder/field.rb in avro-builder-0.3.0

- old
+ new

@@ -8,19 +8,20 @@ class Field include Avro::Builder::DslAttributes include Avro::Builder::Namespaceable include Avro::Builder::TypeFactory - INTERNAL_ATTRIBUTES = Set.new(%i(optional)).freeze + INTERNAL_ATTRIBUTES = Set.new(%i(optional_field)).freeze - attr_accessor :type, :optional, :builder + attr_accessor :type, :optional_field, :builder, :record # These attributes may be set as options or via a block in the DSL dsl_attributes :doc, :aliases, :default, :order - def initialize(name:, type_name:, builder:, internal: {}, options: {}, &block) + def initialize(name:, type_name:, record:, builder:, internal: {}, options: {}, &block) @builder = builder + @record = record @name = name.to_s internal.each do |key, value| send("#{key}=", value) if INTERNAL_ATTRIBUTES.include?(key) end @@ -40,14 +41,18 @@ def respond_to_missing?(id, include_all = false) super || type.respond_to?(id, include_all) end - def method_missing(id, *args) - type.respond_to?(id) ? type.send(id, *args) : super + def method_missing(id, *args, &block) + type.respond_to?(id) ? type.send(id, *args, &block) : super end + def name_fragment + record.name_fragment + end + # Delegate setting name explicitly via DSL to type def name(value = nil) if value type.name(value) else @@ -66,13 +71,13 @@ }.reject { |_, v| v.nil? } end private - # Optional types must be serialized as a union -- an array of types. + # Optional fields must be serialized as a union -- an array of types. def serialized_type(reference_state) result = type.serialize(reference_state) - optional ? type.class.union_with_null(result) : result + optional_field ? type.class.union_with_null(result) : result end end end end