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

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.

Examples:

class Language < Dry::Struct
  attribute :name, Types::String
end

Language.schema
  #=> {name: #<Dry::Types::Definition primitive=String options={}>}

ruby = Language.new(name: 'Ruby')
ruby.name #=> 'Ruby'

Parameters:

  • name (Symbol)

    name of the defined attribute

  • type (Dry::Types::Definition)

Returns:

Raises:



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

Parameters:

  • key (Symbol)

    Attribute name

Returns:

  • (Boolean)


195
196
197
# File 'lib/dry/struct/class_interface.rb', line 195

def attribute?(key)
  schema.key?(key)
end

#attribute_namesArray<Symbol>

Gets the list of attribute names

Returns:

  • (Array<Symbol>)


202
203
204
# File 'lib/dry/struct/class_interface.rb', line 202

def attribute_names
  schema.keys
end

#attributes(new_schema) ⇒ Dry::Struct

Examples:

class Book1 < Dry::Struct
  attributes(
    title: Types::String,
    author: Types::String
  )
end

Book.schema
  #=> {title: #<Dry::Types::Definition primitive=String options={}>,
  #    author: #<Dry::Types::Definition primitive=String options={}>}

Parameters:

  • new_schema (Hash{Symbol => Dry::Types::Definition})

Returns:

Raises:

See Also:



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.

Parameters:

  • attributes (Hash{Symbol => Object}, Dry::Struct) (defaults to: default_attributes)

Returns:



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_keysObject

Raises:

  • (ArgumentError)


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

Returns:

  • (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

Parameters:

  • constructor (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:



112
113
114
# File 'lib/dry/struct/class_interface.rb', line 112

def constructor(constructor = nil, **_options, &block)
  Struct::Constructor.new(self, fn: constructor || block)
end

#default?false

Returns:

  • (false)


166
167
168
# File 'lib/dry/struct/class_interface.rb', line 166

def default?
  false
end

#default_attributesHash{Symbol => Object}

Retrieves default attributes from defined Dry::Struct#schema. Used in a Dry::Struct constructor if no attributes provided to #new

Returns:

  • (Hash{Symbol => Object})


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

Parameters:

  • args (({Symbol => Object}))

Returns:

  • (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

Parameters:

  • klass (Class)


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

Parameters:

  • attributes (Hash{Symbol => Object}, Dry::Struct) (defaults to: default_attributes)

Raises:



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

Returns:

  • (false)


187
188
189
# File 'lib/dry/struct/class_interface.rb', line 187

def optional?
  false
end

#primitiveself

Returns:

  • (self)


182
183
184
# File 'lib/dry/struct/class_interface.rb', line 182

def primitive
  self
end

#result(klass, *args) ⇒ Object

Parameters:

  • klass (Class)
  • args (({Symbol => 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

Parameters:

  • args (({Symbol => Object}))

Returns:

  • (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

Parameters:

  • input (Hash{Symbol => Object})

Yield Parameters:

  • failure (Dry::Types::Result::Failure)

Yield Returns:

  • (Dry::Types::ResultResult)

Returns:

  • (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.message)
  block_given? ? yield(failure) : failure
end

#valid?(value) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


172
173
174
# File 'lib/dry/struct/class_interface.rb', line 172

def valid?(value)
  self === value
end