Module: Dry::Struct::ClassInterface

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constructorObject

Returns the value of attribute constructor.



6
7
8
# File 'lib/dry/struct/class_interface.rb', line 6

def constructor
  @constructor
end

#constructor_type(type = nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/dry/struct/class_interface.rb', line 54

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

#equalizerObject

Returns the value of attribute equalizer.



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

def equalizer
  @equalizer
end

Instance Method Details

#attribute(name, type) ⇒ Object



29
30
31
# File 'lib/dry/struct/class_interface.rb', line 29

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

#attributes(new_schema) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dry/struct/class_interface.rb', line 33

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

#default?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/dry/struct/class_interface.rb', line 96

def default?
  false
end

#default_attributesObject



79
80
81
82
83
# File 'lib/dry/struct/class_interface.rb', line 79

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

#inherited(klass) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dry/struct/class_interface.rb', line 14

def inherited(klass)
  super

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

  unless klass == Value
    klass.constructor = Types['coercible.hash']
    Types.register(Types.identifier(klass), klass)
  end

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

#maybe?Boolean

Returns:

  • (Boolean)


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

def maybe?
  false
end

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



67
68
69
70
71
72
73
74
75
# File 'lib/dry/struct/class_interface.rb', line 67

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

#schemaObject



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

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

#try(input) ⇒ Object



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

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)


100
101
102
# File 'lib/dry/struct/class_interface.rb', line 100

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