Class: Dry::Types::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/types/struct.rb

Direct Known Subclasses

Result::Failure, Result::Success, Value

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Struct

Returns a new instance of Struct.



75
76
77
# File 'lib/dry/types/struct.rb', line 75

def initialize(attributes)
  attributes.each { |key, value| instance_variable_set("@#{key}", value) }
end

Class Attribute Details

.constructorObject (readonly)

Returns the value of attribute constructor.



5
6
7
# File 'lib/dry/types/struct.rb', line 5

def constructor
  @constructor
end

Class Method Details

.attribute(name, type) ⇒ Object



27
28
29
# File 'lib/dry/types/struct.rb', line 27

def self.attribute(name, type)
  attributes(name => type)
end

.attributes(new_schema) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dry/types/struct.rb', line 31

def self.attributes(new_schema)
  check_schema_duplication(new_schema)

  prev_schema = schema

  @schema = prev_schema.merge(new_schema)
  @constructor = Types['coercible.hash'].public_send(constructor_type, schema)

  attr_reader(*new_schema.keys)
  equalizer.instance_variable_get('@keys').concat(new_schema.keys)

  self
end

.constructor_type(type = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/dry/types/struct.rb', line 52

def self.constructor_type(type = nil)
  if type
    @constructor_type = type
  else
    @constructor_type || :strict
  end
end

.equalizerObject



23
24
25
# File 'lib/dry/types/struct.rb', line 23

def self.equalizer
  @equalizer
end

.inherited(klass) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dry/types/struct.rb', line 8

def self.inherited(klass)
  super

  klass.instance_variable_set('@equalizer', Equalizer.new(*schema.keys))
  klass.instance_variable_set('@constructor_type', constructor_type)
  klass.send(:include, klass.equalizer)

  unless klass == Value
    klass.instance_variable_set('@constructor', Types['coercible.hash'])
    Types.register_class(klass)
  end

  klass.attributes({}) unless equal?(Struct)
end

.new(attributes = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/dry/types/struct.rb', line 65

def self.new(attributes = {})
  if attributes.instance_of?(self)
    attributes
  else
    super(constructor[attributes])
  end
rescue SchemaError, SchemaKeyError => error
  raise StructError, "[#{self}.new] #{error}"
end

.schemaObject



60
61
62
63
# File 'lib/dry/types/struct.rb', line 60

def self.schema
  super_schema = superclass.respond_to?(:schema) ? superclass.schema : {}
  super_schema.merge(@schema || {})
end

Instance Method Details

#[](name) ⇒ Object



79
80
81
# File 'lib/dry/types/struct.rb', line 79

def [](name)
  public_send(name)
end

#to_hashObject Also known as: to_h



83
84
85
86
87
88
# File 'lib/dry/types/struct.rb', line 83

def to_hash
  self.class.schema.keys.each_with_object({}) { |key, result|
    value = self[key]
    result[key] = value.respond_to?(:to_hash) ? value.to_hash : value
  }
end