Module: Dry::Struct::ClassInterface

Includes:
Types::Builder
Included in:
Dry::Struct
Defined in:
lib/dry/struct/class_interface.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constructorObject

Returns the value of attribute constructor.



8
9
10
# File 'lib/dry/struct/class_interface.rb', line 8

def constructor
  @constructor
end

#constructor_type(type = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dry/struct/class_interface.rb', line 60

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

#equalizerObject

Returns the value of attribute equalizer.



10
11
12
# File 'lib/dry/struct/class_interface.rb', line 10

def equalizer
  @equalizer
end

Class Method Details

.extended(base) ⇒ Object



16
17
18
# File 'lib/dry/struct/class_interface.rb', line 16

def self.extended(base)
  base.instance_variable_set(:@schema, {})
end

Instance Method Details

#attribute(name, type) ⇒ Object



35
36
37
# File 'lib/dry/struct/class_interface.rb', line 35

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

#attributes(new_schema) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dry/struct/class_interface.rb', line 39

def 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

#constrained?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/dry/struct/class_interface.rb', line 118

def constrained?
  false
end

#default?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/dry/struct/class_interface.rb', line 110

def default?
  false
end

#default_attributesObject



85
86
87
88
89
# File 'lib/dry/struct/class_interface.rb', line 85

def default_attributes
  schema.each_with_object({}) { |(name, type), result|
    result[name] = type.default? ? type.evaluate : type[nil]
  }
end

#failure(*args) ⇒ Object



102
103
104
# File 'lib/dry/struct/class_interface.rb', line 102

def failure(*args)
  result(Types::Result::Failure, *args)
end

#inherited(klass) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dry/struct/class_interface.rb', line 20

def inherited(klass)
  super

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

  unless klass == Value
    klass.constructor = Types['coercible.hash']
  end

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

#new(attributes = default_attributes) ⇒ Object Also known as: call, []



73
74
75
76
77
78
79
80
81
# File 'lib/dry/struct/class_interface.rb', line 73

def new(attributes = default_attributes)
  if attributes.instance_of?(self)
    attributes
  else
    super(constructor[attributes])
  end
rescue Types::SchemaError, Types::MissingKeyError, Types::UnknownKeysError => error
  raise Struct::Error, "[#{self}.new] #{error}"
end

#primitiveObject



122
123
124
# File 'lib/dry/struct/class_interface.rb', line 122

def primitive
  self
end

#result(klass, *args) ⇒ Object



106
107
108
# File 'lib/dry/struct/class_interface.rb', line 106

def result(klass, *args)
  klass.new(*args)
end

#schemaObject



68
69
70
71
# File 'lib/dry/struct/class_interface.rb', line 68

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

#success(*args) ⇒ Object



98
99
100
# File 'lib/dry/struct/class_interface.rb', line 98

def success(*args)
  result(Types::Result::Success, *args)
end

#try(input) ⇒ Object



91
92
93
94
95
96
# File 'lib/dry/struct/class_interface.rb', line 91

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

Returns:

  • (Boolean)


114
115
116
# File 'lib/dry/struct/class_interface.rb', line 114

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