class Dry::Concrete
Public Class Methods
alias_fields(field, aliases: [], **options, &block)
click to toggle source
# File lib/dry/concrete.rb, line 83 def self.alias_fields(field, aliases: [], **options, &block) if options.key?(:alias) aliases << options.delete(:alias) end block[options] aliases.each do |alias_name| alias_method alias_name, field end end
attribute(field, *constrains, **options, &block)
click to toggle source
Adds attribute {name} to the struct
@example Add a new attribute
class User < Dry::Struct attribute :name, String end
@example Add a new attribute with a default value
class User < Dry::Struct attribute :name, String, default: "John" end
@example Add a new attribute with constraints
class User < Dry::Struct attribute :name, String, size: 3..20 end
@example Add a new attribute with array type
class User < Dry::Struct attribute :name, [String] end
@param name [Symbol] @param constrains [Array<#to_type>] @option default [#call, Any] @return [void]
Calls superclass method
# File lib/dry/concrete.rb, line 68 def self.attribute(field, *constrains, **options, &block) alias_fields(field, **options) do |inner_options| super(field, *build_type_from(*constrains, **inner_options, &block)) end end
attribute?(field, *constrains, **options, &block)
click to toggle source
Optional version of {#attribute}
@see attribute
Calls superclass method
# File lib/dry/concrete.rb, line 77 def self.attribute?(field, *constrains, **options, &block) alias_fields(field, **options) do |inner_options| super(field, *build_type_from(*constrains, **inner_options, &block)) end end
build_type_from(*constrains, **options, &block)
click to toggle source
@api private
# File lib/dry/concrete.rb, line 96 def self.build_type_from(*constrains, **options, &block) if block_given? return [Class.new(Concrete, &block)] end unless (type = constrains.map(&:to_type).reduce(:|)) return EMPTY_ARRAY end if options.key?(:default) options.delete(:default).to_default.then do |default_proc| return build_type_from(type.default(&default_proc), **options) end end if options.empty? return [type] end build_type_from(type.constrained(**options)) end
initializer(owner, &block)
click to toggle source
# File lib/dry/concrete.rb, line 38 def self.initializer(owner, &block) owner.schema owner.schema.constructor(&block) end