Class: Operandi::Settings::Field
- Inherits:
-
Object
- Object
- Operandi::Settings::Field
- 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
-
#context ⇒ Boolean?
readonly
True if this is a context argument.
-
#default ⇒ Object, ...
readonly
The default value or proc.
-
#default_exists ⇒ Boolean
readonly
True if a default value was specified.
-
#name ⇒ Symbol
readonly
The field name.
-
#optional ⇒ Boolean?
readonly
True if nil values are allowed.
Instance Method Summary collapse
-
#initialize(name, service_class, opts = {}) ⇒ Field
constructor
Initialize a new field definition.
-
#validate_type!(value) ⇒ Object
Validate a value against the field's type definition.
Constructor Details
#initialize(name, service_class, opts = {}) ⇒ Field
Initialize a new field definition.
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
#context ⇒ Boolean? (readonly)
Returns true if this is a context argument.
18 19 20 |
# File 'lib/operandi/settings/field.rb', line 18 def context @context end |
#default ⇒ Object, ... (readonly)
Returns the default value or proc.
15 16 17 |
# File 'lib/operandi/settings/field.rb', line 15 def default @default end |
#default_exists ⇒ Boolean (readonly)
Returns true if a default value was specified.
12 13 14 |
# File 'lib/operandi/settings/field.rb', line 12 def default_exists @default_exists end |
#name ⇒ Symbol (readonly)
Returns the field name.
9 10 11 |
# File 'lib/operandi/settings/field.rb', line 9 def name @name end |
#optional ⇒ Boolean? (readonly)
Returns 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.
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 |