Class: NoSE::Serialize::FieldBuilder

Inherits:
Object
  • Object
show all
Includes:
Uber::Callable
Defined in:
lib/nose/serialize.rb

Overview

Construct a field from a parsed hash

Instance Method Summary collapse

Instance Method Details

#call(_, fragment:, user_options:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nose/serialize.rb', line 40

def call(_, fragment:, user_options:, **)
  field_class = Fields::Field.subtype_class fragment['type']

  # Extract the correct parameters and create a new field instance
  if field_class == Fields::StringField && !fragment['size'].nil?
    field = field_class.new fragment['name'], fragment['size']
  elsif field_class.ancestors.include? Fields::ForeignKeyField
    entity = user_options[:entity_map][fragment['entity']]
    field = field_class.new fragment['name'], entity
  else
    field = field_class.new fragment['name']
  end

  field *= fragment['cardinality'] if fragment['cardinality']

  field
end