Module: Dry::Types::Struct::ClassInterface
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Builder
#constrained, #constrained_type, #default, #enum, #maybe, #optional, #safe, #|
Instance Attribute Details
#constructor ⇒ Object
Returns the value of attribute constructor.
7
8
9
|
# File 'lib/dry/types/struct/class_interface.rb', line 7
def constructor
@constructor
end
|
#constructor_type(type = nil) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/dry/types/struct/class_interface.rb', line 55
def constructor_type(type = nil)
if type
@constructor_type = type
else
@constructor_type || :strict
end
end
|
#equalizer ⇒ Object
Returns the value of attribute equalizer.
9
10
11
|
# File 'lib/dry/types/struct/class_interface.rb', line 9
def equalizer
@equalizer
end
|
Instance Method Details
#attribute(name, type) ⇒ Object
30
31
32
|
# File 'lib/dry/types/struct/class_interface.rb', line 30
def attribute(name, type)
attributes(name => type)
end
|
#attributes(new_schema) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/dry/types/struct/class_interface.rb', line 34
def attributes(new_schema)
check_schema_duplication(new_schema)
prev_schema = schema
@schema = prev_schema.merge(new_schema)
@constructor = Types['coercible.hash'].public_send(constructor_type, schema)
attr_reader(*new_schema.keys)
equalizer.instance_variable_get('@keys').concat(new_schema.keys)
self
end
|
#default? ⇒ Boolean
97
98
99
|
# File 'lib/dry/types/struct/class_interface.rb', line 97
def default?
false
end
|
#default_attributes ⇒ Object
80
81
82
83
84
|
# File 'lib/dry/types/struct/class_interface.rb', line 80
def default_attributes
schema.each_with_object({}) { |(name, type), result|
result[name] = type.default? ? type.evaluate : type[nil]
}
end
|
#inherited(klass) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/dry/types/struct/class_interface.rb', line 15
def inherited(klass)
super
klass.equalizer = Equalizer.new(*schema.keys)
klass.constructor_type = constructor_type
klass.send(:include, klass.equalizer)
unless klass == Value
klass.constructor = Types['coercible.hash']
Types.register(Types.identifier(klass), klass)
end
klass.attributes({}) unless equal?(Struct)
end
|
#maybe? ⇒ Boolean
93
94
95
|
# File 'lib/dry/types/struct/class_interface.rb', line 93
def maybe?
false
end
|
#new(attributes = default_attributes) ⇒ Object
Also known as:
call, []
68
69
70
71
72
73
74
75
76
|
# File 'lib/dry/types/struct/class_interface.rb', line 68
def new(attributes = default_attributes)
if attributes.instance_of?(self)
attributes
else
super(constructor[attributes])
end
rescue SchemaError, SchemaKeyError => error
raise StructError, "[#{self}.new] #{error}"
end
|
#schema ⇒ Object
63
64
65
66
|
# File 'lib/dry/types/struct/class_interface.rb', line 63
def schema
super_schema = superclass.respond_to?(:schema) ? superclass.schema : {}
super_schema.merge(@schema || {})
end
|
#try(input) ⇒ Object
86
87
88
89
90
91
|
# File 'lib/dry/types/struct/class_interface.rb', line 86
def try(input)
Result::Success.new(self[input])
rescue StructError => e
failure = Result::Failure.new(input, e.message)
block_given? ? yield(failure) : failure
end
|
#valid?(value) ⇒ Boolean
101
102
103
|
# File 'lib/dry/types/struct/class_interface.rb', line 101
def valid?(value)
self === value
end
|