Module: Taro::Types::Shared::Fields

Included in:
ObjectType
Defined in:
lib/taro/types/shared/fields.rb

Overview

Adds the ‘::field` method to object and input types.

Instance Method Summary collapse

Instance Method Details

#field(name, **attributes) ⇒ Object

Field types are set using class name Strings. The respective type classes are evaluated lazily to allow for circular or recursive type references, and to avoid unnecessary autoloading of all types in dev/test envs.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/taro/types/shared/fields.rb', line 6

def field(name, **attributes)
  attributes[:defined_at] ||= caller_locations(1..1)[0]
  field_def = Taro::Types::FieldDef.new(name:, **attributes)

  (prev = field_defs[name]) && raise(Taro::ArgumentError, <<~MSG)
    field #{name} at #{field_def.defined_at}
    previously defined at #{prev.defined_at}.
  MSG

  field_defs[name] = field_def
end

#fieldsObject



18
19
20
# File 'lib/taro/types/shared/fields.rb', line 18

def fields
  @fields ||= field_defs.transform_values(&:evaluate)
end