Class: Operandi::Settings::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/operandi/settings/field.rb

Overview

Stores configuration for a single argument or output field. Created automatically when using the arg or output DSL methods.

Constant Summary collapse

FIELD_TYPE_TO_IVAR =
{
  FieldTypes::ARGUMENT => :@arg,
  FieldTypes::OUTPUT => :@output,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, service_class, opts = {}) ⇒ Field

Initialize a new field definition.

Parameters:

  • name (Symbol)

    the field name

  • service_class (Class)

    the service class this field belongs to

  • opts (Hash) (defaults to: {})

    field options

Options Hash (opts):

  • :type (Class, Array<Class>)

    type(s) to validate against

  • :optional (Boolean)

    whether nil is allowed

  • :default (Object, Proc)

    default value or proc

  • :context (Boolean)

    whether to pass to child services

  • :field_type (Symbol)

    :argument or :output



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/operandi/settings/field.rb', line 33

def initialize(name, service_class, opts = {})
  @name = name
  @service_class = service_class
  @field_type = opts.delete(:field_type) || :argument

  @type = opts.delete(:type)
  @context = opts.delete(:context)
  @default_exists = opts.key?(:default)
  @default = opts.delete(:default)
  @optional = opts.delete(:optional)

  define_methods
end

Instance Attribute Details

#contextBoolean? (readonly)

Returns true if this is a context argument.

Returns:

  • (Boolean, nil)

    true if this is a context argument



18
19
20
# File 'lib/operandi/settings/field.rb', line 18

def context
  @context
end

#defaultObject, ... (readonly)

Returns the default value or proc.

Returns:

  • (Object, Proc, nil)

    the default value or proc



15
16
17
# File 'lib/operandi/settings/field.rb', line 15

def default
  @default
end

#default_existsBoolean (readonly)

Returns true if a default value was specified.

Returns:

  • (Boolean)

    true if a default value was specified



12
13
14
# File 'lib/operandi/settings/field.rb', line 12

def default_exists
  @default_exists
end

#nameSymbol (readonly)

Returns the field name.

Returns:

  • (Symbol)

    the field name



9
10
11
# File 'lib/operandi/settings/field.rb', line 9

def name
  @name
end

#optionalBoolean? (readonly)

Returns true if nil values are allowed.

Returns:

  • (Boolean, nil)

    true if nil values are allowed



21
22
23
# File 'lib/operandi/settings/field.rb', line 21

def optional
  @optional
end

Instance Method Details

#validate_type!(value) ⇒ Object

Validate a value against the field's type definition. Supports Ruby class types and Sorbet runtime types.

Parameters:

  • value (Object)

    the value to validate

Returns:

  • (Object)

    the validated value

Raises:

  • (ArgTypeError)

    if the value doesn't match the expected type



53
54
55
56
57
58
59
60
61
62
# File 'lib/operandi/settings/field.rb', line 53

def validate_type!(value)
  return value unless @type

  if sorbet_type?(@type) || (sorbet_available? && plain_class_type?(@type))
    validate_sorbet_type!(value)
  else
    validate_ruby_type!(value)
    value
  end
end