Class: Typed::Field
Constant Summary collapse
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#inline_serializer ⇒ Object
readonly
Returns the value of attribute inline_serializer.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(name:, type:, optional: false, default: nil, inline_serializer: nil) ⇒ Field
constructor
A new instance of Field.
- #optional? ⇒ Boolean
- #required? ⇒ Boolean
- #serialize(value) ⇒ Object
- #validate(value) ⇒ Object
- #works_with?(value) ⇒ Boolean
Constructor Details
#initialize(name:, type:, optional: false, default: nil, inline_serializer: nil) ⇒ Field
Returns a new instance of Field.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/typed/field.rb', line 33 def initialize(name:, type:, optional: false, default: nil, inline_serializer: nil) @name = name # TODO: Guarentee type signature of the serializer will be valid @inline_serializer = inline_serializer coerced_type = T::Utils.coerce(type) if coerced_type.valid?(nil) @required = T.let(false, T::Boolean) @type = T.let(T.unsafe(coerced_type).unwrap_nilable, T::Types::Base) else @required = true @type = coerced_type end if optional @required = false end if !default.nil? && @type.valid?(default) @default = T.let(default, T.untyped) @required = false elsif !default.nil? && @required raise ArgumentError, "Given #{default} with class of #{default.class} for default, invalid with type #{@type}" end end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
16 17 18 |
# File 'lib/typed/field.rb', line 16 def default @default end |
#inline_serializer ⇒ Object (readonly)
Returns the value of attribute inline_serializer.
22 23 24 |
# File 'lib/typed/field.rb', line 22 def inline_serializer @inline_serializer end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/typed/field.rb', line 10 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
19 20 21 |
# File 'lib/typed/field.rb', line 19 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/typed/field.rb', line 13 def type @type end |
Instance Method Details
#==(other) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/typed/field.rb', line 61 def ==(other) name == other.name && type == other.type && required == other.required && default == other.default && inline_serializer == other.inline_serializer end |
#optional? ⇒ Boolean
75 76 77 |
# File 'lib/typed/field.rb', line 75 def optional? !required end |
#required? ⇒ Boolean
70 71 72 |
# File 'lib/typed/field.rb', line 70 def required? required end |
#serialize(value) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/typed/field.rb', line 80 def serialize(value) if inline_serializer && value T.must(inline_serializer).call(value) else value end end |
#validate(value) ⇒ Object
89 90 91 |
# File 'lib/typed/field.rb', line 89 def validate(value) Validations::FieldTypeValidator.new.validate(field: self, value: value) end |
#works_with?(value) ⇒ Boolean
94 95 96 |
# File 'lib/typed/field.rb', line 94 def works_with?(value) type.recursively_valid?(value) end |