Class: Reasonable::Value

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/reasonable/value.rb,
lib/reasonable/value/version.rb

Constant Summary collapse

VERSION =
'0.2.7'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Value

Returns a new instance of Value.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reasonable/value.rb', line 14

def initialize(attributes = {})
  @attributes = {}

  self.class.send(:config).each do |name, config|
    options = config[:options]
    if options[:optional]
      attributes[name] = options[:default] if attributes[name].nil?
      next if attributes[name].nil?
    end

    type_error(name, config[:type], NilClass) if attributes[name].nil?

    @attributes[name] = coerce(attributes, name, config)
  end
end

Class Method Details

.inherited(subklass) ⇒ Object



40
41
42
43
44
# File 'lib/reasonable/value.rb', line 40

def inherited(subklass)
  config.each do |name, config|
    subklass.attribute(name, config[:type], options: config[:options])
  end
end

.new(object = {}) ⇒ Object



36
37
38
# File 'lib/reasonable/value.rb', line 36

def new(object = {})
  Try.(MethodName.(self), object) || super(object)
end

Instance Method Details

#<=>(other) ⇒ Object



10
11
12
# File 'lib/reasonable/value.rb', line 10

def <=>(other)
  @attributes <=> other.instance_variable_get(:@attributes)
end

#to_hashObject



30
31
32
# File 'lib/reasonable/value.rb', line 30

def to_hash
  @attributes
end