Module: Dry::Struct::ClassInterface
- Includes:
- Core::ClassAttributes, Types::Builder
- Included in:
- Dry::Struct
- Defined in:
- lib/dry/struct/class_interface.rb
Overview
Class-level interface of Dry::Struct and Value
Instance Method Summary collapse
- #argument_error_msg(keys) ⇒ Object
-
#attribute(name, type) ⇒ Dry::Struct
Adds an attribute for this Dry::Struct with given
name
andtype
and modifies #schema accordingly. -
#attribute?(key) ⇒ Boolean
Checks if this Dry::Struct has the given attribute.
-
#attribute_names ⇒ Array<Symbol>
Gets the list of attribute names.
- #attributes(new_schema) ⇒ Dry::Struct
-
#call(attributes = default_attributes) ⇒ Dry::Struct
(also: #[])
Calls type constructor.
- #check_invalid_schema_keys ⇒ Object
- #constrained? ⇒ true
- #constructor(constructor = nil, **_options, &block) ⇒ Dry::Struct::Constructor
- #default? ⇒ false
-
#default_attributes ⇒ Hash{Symbol => Object}
Retrieves default attributes from defined #schema.
- #failure(*args) ⇒ Dry::Types::Result::Failure
- #inherited(klass) ⇒ Object
- #new(attributes = default_attributes) ⇒ Object
- #optional? ⇒ false
- #primitive ⇒ self
- #result(klass, *args) ⇒ Object
- #success(*args) ⇒ Dry::Types::Result::Success
- #try(input) {|failure| ... } ⇒ Dry::Types::Result
- #valid?(value) ⇒ Boolean
Instance Method Details
#argument_error_msg(keys) ⇒ Object
132 133 134 |
# File 'lib/dry/struct/class_interface.rb', line 132 def argument_error_msg(keys) "Invaild argument for #{keys.join(', ')}" end |
#attribute(name, type) ⇒ Dry::Struct
Adds an attribute for this Dry::Struct with given name
and type
and modifies Dry::Struct#schema accordingly.
42 43 44 |
# File 'lib/dry/struct/class_interface.rb', line 42 def attribute(name, type) attributes(name => type) end |
#attribute?(key) ⇒ Boolean
Checks if this Dry::Struct has the given attribute
195 196 197 |
# File 'lib/dry/struct/class_interface.rb', line 195 def attribute?(key) schema.key?(key) end |
#attribute_names ⇒ Array<Symbol>
Gets the list of attribute names
202 203 204 |
# File 'lib/dry/struct/class_interface.rb', line 202 def attribute_names schema.keys end |
#attributes(new_schema) ⇒ Dry::Struct
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dry/struct/class_interface.rb', line 62 def attributes(new_schema) check_schema_duplication(new_schema) schema schema.merge(new_schema) input Types['coercible.hash'].public_send(constructor_type, schema) attr_reader(*new_schema.keys) equalizer.instance_variable_get('@keys').concat(new_schema.keys) self end |
#call(attributes = default_attributes) ⇒ Dry::Struct Also known as: []
Calls type constructor. The behavior is identical to .new
but returns
the input back if it's a subclass of the struct.
102 103 104 105 |
# File 'lib/dry/struct/class_interface.rb', line 102 def call(attributes = default_attributes) return attributes if attributes.is_a?(self) new(attributes) end |
#check_invalid_schema_keys ⇒ Object
127 128 129 130 |
# File 'lib/dry/struct/class_interface.rb', line 127 def check_invalid_schema_keys invalid_keys = schema.select { |name, type| type.instance_of?(String) } raise ArgumentError, argument_error_msg(invalid_keys.keys) if invalid_keys.any? end |
#constrained? ⇒ true
177 178 179 |
# File 'lib/dry/struct/class_interface.rb', line 177 def constrained? true end |
#constructor(constructor = nil, **_options, &block) ⇒ Dry::Struct::Constructor
112 113 114 |
# File 'lib/dry/struct/class_interface.rb', line 112 def constructor(constructor = nil, **, &block) Struct::Constructor.new(self, fn: constructor || block) end |
#default? ⇒ false
166 167 168 |
# File 'lib/dry/struct/class_interface.rb', line 166 def default? false end |
#default_attributes ⇒ Hash{Symbol => Object}
Retrieves default attributes from defined Dry::Struct#schema. Used in a Dry::Struct constructor if no attributes provided to #new
120 121 122 123 124 125 |
# File 'lib/dry/struct/class_interface.rb', line 120 def default_attributes check_invalid_schema_keys schema.each_with_object({}) { |(name, type), result| result[name] = type.evaluate if type.default? } end |
#failure(*args) ⇒ Dry::Types::Result::Failure
155 156 157 |
# File 'lib/dry/struct/class_interface.rb', line 155 def failure(*args) result(Types::Result::Failure, *args) end |
#inherited(klass) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/dry/struct/class_interface.rb', line 16 def inherited(klass) super klass.equalizer Equalizer.new(*schema.keys) klass.send(:include, klass.equalizer) end |
#new(attributes = default_attributes) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/dry/struct/class_interface.rb', line 87 def new(attributes = default_attributes) if attributes.instance_of?(self) attributes else super(input[attributes]) end rescue Types::SchemaError, Types::MissingKeyError, Types::UnknownKeysError => error raise Struct::Error, "[#{self}.new] #{error}" end |
#optional? ⇒ false
187 188 189 |
# File 'lib/dry/struct/class_interface.rb', line 187 def optional? false end |
#primitive ⇒ self
182 183 184 |
# File 'lib/dry/struct/class_interface.rb', line 182 def primitive self end |
#result(klass, *args) ⇒ Object
161 162 163 |
# File 'lib/dry/struct/class_interface.rb', line 161 def result(klass, *args) klass.new(*args) end |
#success(*args) ⇒ Dry::Types::Result::Success
149 150 151 |
# File 'lib/dry/struct/class_interface.rb', line 149 def success(*args) result(Types::Result::Success, *args) end |
#try(input) {|failure| ... } ⇒ Dry::Types::Result
140 141 142 143 144 145 |
# File 'lib/dry/struct/class_interface.rb', line 140 def try(input) Types::Result::Success.new(self[input]) rescue Struct::Error => e failure = Types::Result::Failure.new(input, e.) block_given? ? yield(failure) : failure end |
#valid?(value) ⇒ Boolean
172 173 174 |
# File 'lib/dry/struct/class_interface.rb', line 172 def valid?(value) self === value end |