Class: Evil::Client::Model

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer::Mixin
Defined in:
lib/evil/client/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject



24
25
26
# File 'lib/evil/client/model.rb', line 24

def attributes
  @attributes ||= []
end

.new(value) ⇒ Object Also known as: call, []



14
15
16
17
18
19
20
# File 'lib/evil/client/model.rb', line 14

def new(value)
  return value if value.is_a? self
  value = value.to_h.each_with_object({}) do |(key, val), obj|
    obj[key.to_sym] = val
  end
  super value
end

.option(name, type = nil, **opts) ⇒ Object Also known as: attribute, param



28
29
30
# File 'lib/evil/client/model.rb', line 28

def option(name, type = nil, **opts)
  super.tap { attributes << name.to_sym }
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/evil/client/model.rb', line 42

def ==(other)
  other.respond_to?(:to_h) ? to_h == other.to_h : false
end

#to_hObject



46
47
48
49
50
51
# File 'lib/evil/client/model.rb', line 46

def to_h
  self.class.attributes.each_with_object({}) do |key, hash|
    val = send(key)
    hash[key] = hashify(val) unless val == Dry::Initializer::UNDEFINED
  end
end