Module: Dry::Struct::ClassInterface
- Includes:
- Core::ClassAttributes, Types::Builder, Types::Type
- 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
136 137 138 |
# File 'lib/dry/struct/class_interface.rb', line 136 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.
43 44 45 |
# File 'lib/dry/struct/class_interface.rb', line 43 def attribute(name, type) attributes(name => type) end |
#attribute?(key) ⇒ Boolean
Checks if this Dry::Struct has the given attribute
199 200 201 |
# File 'lib/dry/struct/class_interface.rb', line 199 def attribute?(key) schema.key?(key) end |
#attribute_names ⇒ Array<Symbol>
Gets the list of attribute names
206 207 208 |
# File 'lib/dry/struct/class_interface.rb', line 206 def attribute_names @attribute_names ||= schema.keys end |
#attributes(new_schema) ⇒ Dry::Struct
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dry/struct/class_interface.rb', line 63 def attributes(new_schema) check_schema_duplication(new_schema) schema schema.merge(new_schema) input Types['coercible.hash'].public_send(constructor_type, schema) new_schema.each_key do |key| attr_reader(key) unless instance_methods.include?(key) end 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.
106 107 108 109 |
# File 'lib/dry/struct/class_interface.rb', line 106 def call(attributes = default_attributes) return attributes if attributes.is_a?(self) new(attributes) end |
#check_invalid_schema_keys ⇒ Object
131 132 133 134 |
# File 'lib/dry/struct/class_interface.rb', line 131 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
181 182 183 |
# File 'lib/dry/struct/class_interface.rb', line 181 def constrained? true end |
#constructor(constructor = nil, **_options, &block) ⇒ Dry::Struct::Constructor
116 117 118 |
# File 'lib/dry/struct/class_interface.rb', line 116 def constructor(constructor = nil, **, &block) Struct::Constructor.new(self, fn: constructor || block) end |
#default? ⇒ false
170 171 172 |
# File 'lib/dry/struct/class_interface.rb', line 170 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
124 125 126 127 128 129 |
# File 'lib/dry/struct/class_interface.rb', line 124 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
159 160 161 |
# File 'lib/dry/struct/class_interface.rb', line 159 def failure(*args) result(Types::Result::Failure, *args) end |
#inherited(klass) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/dry/struct/class_interface.rb', line 17 def inherited(klass) super klass.equalizer Equalizer.new(*schema.keys) klass.send(:include, klass.equalizer) end |
#new(attributes = default_attributes) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/dry/struct/class_interface.rb', line 91 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
191 192 193 |
# File 'lib/dry/struct/class_interface.rb', line 191 def optional? false end |
#primitive ⇒ self
186 187 188 |
# File 'lib/dry/struct/class_interface.rb', line 186 def primitive self end |
#result(klass, *args) ⇒ Object
165 166 167 |
# File 'lib/dry/struct/class_interface.rb', line 165 def result(klass, *args) klass.new(*args) end |
#success(*args) ⇒ Dry::Types::Result::Success
153 154 155 |
# File 'lib/dry/struct/class_interface.rb', line 153 def success(*args) result(Types::Result::Success, *args) end |
#try(input) {|failure| ... } ⇒ Dry::Types::Result
144 145 146 147 148 149 |
# File 'lib/dry/struct/class_interface.rb', line 144 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
176 177 178 |
# File 'lib/dry/struct/class_interface.rb', line 176 def valid?(value) self === value end |