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
- #===(other) ⇒ Boolean (also: #primitive?)
-
#abstract ⇒ Object
Make the struct abstract.
-
#attribute(name, type = Undefined) { ... } ⇒ Dry::Struct
Adds an attribute for this Dry::Struct with given ‘name` and `type` and modifies #schema accordingly.
-
#attribute?(*args) ⇒ Dry::Struct
Adds an omittable (key is not required on initialization) attribute for this Dry::Struct.
-
#attribute_names ⇒ Array<Symbol>
Gets the list of attribute names.
- #attributes(new_schema) ⇒ Dry::Struct
-
#attributes_from(struct) ⇒ Object
Add atributes from another struct.
- #call_safe(input) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #constrained? ⇒ true
- #constructor(constructor = nil, &block) ⇒ Dry::Struct::Constructor
- #default? ⇒ false
- #failure(*args) ⇒ Dry::Types::Result::Failure
-
#has_attribute?(key) ⇒ Boolean
Checks if this Dry::Struct has the given attribute.
- #load(attributes) ⇒ Object private
- #meta(meta = Undefined) ⇒ {Symbol => Object}
- #new(attributes = default_attributes, safe = false) ⇒ Object
- #optional? ⇒ false
- #primitive ⇒ self
- #result(klass, *args) ⇒ Object
- #success(*args) ⇒ Dry::Types::Result::Success
-
#to_ast(meta: true) ⇒ Array
Dump to the AST.
- #to_proc ⇒ Proc
-
#transform_keys(proc = nil, &block) ⇒ Object
Add an arbitrary transformation for input hash keys.
-
#transform_types(proc = nil, &block) ⇒ Object
Add an arbitrary transformation for new attribute types.
- #try(input) {|failure| ... } ⇒ Dry::Types::Result
- #try_struct(input) ⇒ Dry::Types::Result
-
#|(type) ⇒ Dry::Types::Sum
Build a sum type.
Instance Method Details
#===(other) ⇒ Boolean Also known as: primitive?
331 |
# File 'lib/dry/struct/class_interface.rb', line 331 def ===(other) = other.is_a?(self) |
#abstract ⇒ Object
Make the struct abstract. This class will be used as a default parent class for nested structs
387 388 389 |
# File 'lib/dry/struct/class_interface.rb', line 387 def abstract abstract_class self end |
#attribute(name, type = Undefined) { ... } ⇒ Dry::Struct
Adds an attribute for this Dry::Struct with given ‘name` and `type` and modifies Dry::Struct#schema accordingly.
86 87 88 |
# File 'lib/dry/struct/class_interface.rb', line 86 def attribute(name, type = Undefined, &) attributes(name => build_type(name, type, &)) end |
#attribute?(*args) ⇒ Dry::Struct
Adds an omittable (key is not required on initialization) attribute for this Dry::Struct
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/dry/struct/class_interface.rb', line 139 def attribute?(*args, &) if args.size == 1 && !block_given? Core::Deprecations.warn( "Dry::Struct.attribute? is deprecated for checking attribute presence, " \ "use has_attribute? instead", tag: :"dry-struct" ) has_attribute?(args[0]) else name, * = args attribute(:"#{name}?", build_type(*args, &)) end end |
#attribute_names ⇒ Array<Symbol>
Gets the list of attribute names
357 358 359 |
# File 'lib/dry/struct/class_interface.rb', line 357 def attribute_names @attribute_names ||= schema.map(&:name) end |
#attributes(new_schema) ⇒ Dry::Struct
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dry/struct/class_interface.rb', line 173 def attributes(new_schema) keys = new_schema.keys.map { |k| k.to_s.chomp("?").to_sym } check_schema_duplication(keys) schema schema.schema(new_schema) define_accessors(keys) @attribute_names = nil subclasses.each do |d| inherited_attrs = new_schema.reject { |k, _| d.has_attribute?(k.to_s.chomp("?").to_sym) } d.attributes(inherited_attrs) end self end |
#attributes_from(struct) ⇒ Object
Add atributes from another struct
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/dry/struct/class_interface.rb', line 114 def attributes_from(struct) extracted_schema = struct.schema.keys.to_h do |key| if key.required? [key.name, key.type] else [:"#{key.name}?", key.type] end end attributes(extracted_schema) end |
#call_safe(input) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
261 262 263 264 265 266 267 |
# File 'lib/dry/struct/class_interface.rb', line 261 def call_safe(input, &) if input.is_a?(self) input else new(input, true, &) end end |
#call_unsafe(input) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
270 271 272 273 274 275 276 |
# File 'lib/dry/struct/class_interface.rb', line 270 def call_unsafe(input) if input.is_a?(self) input else new(input) end end |
#constrained? ⇒ true
335 |
# File 'lib/dry/struct/class_interface.rb', line 335 def constrained? = true |
#constructor(constructor = nil, &block) ⇒ Dry::Struct::Constructor
288 289 290 |
# File 'lib/dry/struct/class_interface.rb', line 288 def constructor(constructor = nil, **, &block) Constructor[self, fn: constructor || block] end |
#default? ⇒ false
327 |
# File 'lib/dry/struct/class_interface.rb', line 327 def default? = false |
#failure(*args) ⇒ Dry::Types::Result::Failure
320 |
# File 'lib/dry/struct/class_interface.rb', line 320 def failure(*args) = result(Types::Result::Failure, *args) |
#has_attribute?(key) ⇒ Boolean
Checks if this Dry::Struct has the given attribute
352 |
# File 'lib/dry/struct/class_interface.rb', line 352 def has_attribute?(key) = schema.key?(key) |
#load(attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
279 280 281 282 283 |
# File 'lib/dry/struct/class_interface.rb', line 279 def load(attributes) struct = allocate struct.__send__(:initialize, attributes) struct end |
#meta(meta = Undefined) ⇒ {Symbol => Object}
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/dry/struct/class_interface.rb', line 362 def ( = Undefined) if .equal?(Undefined) schema. elsif .empty? self else ::Class.new(self) do schema schema.() unless .empty? end end end |
#new(attributes = default_attributes, safe = false) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/dry/struct/class_interface.rb', line 239 def new(attributes = default_attributes, safe = false, &) # rubocop:disable Style/OptionalBooleanParameter if attributes.is_a?(Struct) if equal?(attributes.class) attributes else # This implicit coercion is arguable but makes sense overall # in cases there you pass child struct to the base struct constructor # User.new(super_user) # # We may deprecate this behavior in future forcing people to be explicit new(attributes.to_h, safe, &) end elsif safe load(schema.call_safe(attributes) { |output = attributes| return yield output }) else load(schema.call_unsafe(attributes)) end rescue Types::CoercionError => e raise Error, "[#{self}.new] #{e}", e.backtrace end |
#optional? ⇒ false
341 |
# File 'lib/dry/struct/class_interface.rb', line 341 def optional? = false |
#primitive ⇒ self
338 |
# File 'lib/dry/struct/class_interface.rb', line 338 def primitive = self |
#result(klass, *args) ⇒ Object
324 |
# File 'lib/dry/struct/class_interface.rb', line 324 def result(klass, *args) = klass.new(*args) |
#success(*args) ⇒ Dry::Types::Result::Success
316 |
# File 'lib/dry/struct/class_interface.rb', line 316 def success(*args) = result(Types::Result::Success, *args) |
#to_ast(meta: true) ⇒ Array
Dump to the AST
396 397 398 |
# File 'lib/dry/struct/class_interface.rb', line 396 def to_ast(meta: true) [:struct, [::WeakRef.new(self), schema.to_ast(meta: )]] end |
#to_proc ⇒ Proc
344 345 346 |
# File 'lib/dry/struct/class_interface.rb', line 344 def to_proc @to_proc ||= proc { |input| call(input) } end |
#transform_keys(proc = nil, &block) ⇒ Object
Add an arbitrary transformation for input hash keys.
221 222 223 |
# File 'lib/dry/struct/class_interface.rb', line 221 def transform_keys(proc = nil, &block) schema schema.with_key_transform(proc || block) end |
#transform_types(proc = nil, &block) ⇒ Object
Add an arbitrary transformation for new attribute types.
204 205 206 |
# File 'lib/dry/struct/class_interface.rb', line 204 def transform_types(proc = nil, &block) schema schema.with_type_transform(proc || block) end |
#try(input) {|failure| ... } ⇒ Dry::Types::Result
296 297 298 299 300 301 |
# File 'lib/dry/struct/class_interface.rb', line 296 def try(input) success(self[input]) rescue Error => e failure_result = failure(input, e) block_given? ? yield(failure_result) : failure_result end |
#try_struct(input) ⇒ Dry::Types::Result
306 307 308 309 310 311 312 |
# File 'lib/dry/struct/class_interface.rb', line 306 def try_struct(input) if input.is_a?(self) input else yield end end |