Module: Bank::Model

Defined in:
lib/bank/model.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
27
28
# File 'lib/bank/model.rb', line 24

def self.included(base)
  base.define_singleton_method(:config) do
    @_config ||= Bank::Model::Config.new(self)
  end
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
# File 'lib/bank/model.rb', line 55

def ==(other)
  other.is_a?(self.class) && other.to_hash == to_hash
end

#get(attr) ⇒ Object



41
42
43
# File 'lib/bank/model.rb', line 41

def get(attr)
  instance_variable_get(:"@#{attr}")
end

#initialize(attrs = {}) ⇒ Object



30
31
32
33
# File 'lib/bank/model.rb', line 30

def initialize(attrs = {})
  set(attrs)
  _defaults.reject { |key, _| get(key) }.each { |key, val| set(key, val) }
end

#set(attr, value = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/bank/model.rb', line 45

def set(attr, value = nil)
  if attr.is_a?(Hash)
    attr.each { |key, value| set(key, value) }
  else
    public_send(:"#{attr}=", value)
  end

  return self
end

#to_hashObject



35
36
37
38
39
# File 'lib/bank/model.rb', line 35

def to_hash
  _fields.reduce({}) do |acc, field|
    acc.merge(field => get(field))
  end
end