Class: Typed::Field

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/typed/field.rb

Constant Summary collapse

InlineSerializer =
T.type_alias { T.proc.params(arg0: T.untyped).returns(T.untyped) }

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#defaultObject (readonly)

Returns the value of attribute default.



16
17
18
# File 'lib/typed/field.rb', line 16

def default
  @default
end

#inline_serializerObject (readonly)

Returns the value of attribute inline_serializer.



22
23
24
# File 'lib/typed/field.rb', line 22

def inline_serializer
  @inline_serializer
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/typed/field.rb', line 10

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



19
20
21
# File 'lib/typed/field.rb', line 19

def required
  @required
end

#typeObject (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

Returns:

  • (Boolean)


75
76
77
# File 'lib/typed/field.rb', line 75

def optional?
  !required
end

#required?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


94
95
96
# File 'lib/typed/field.rb', line 94

def works_with?(value)
  type.recursively_valid?(value)
end