Class: Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bob/models/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Base

rubocop:disable Metrics/AbcSize



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bob/models/base.rb', line 5

def initialize(raw) # rubocop:disable Metrics/AbcSize
  attributes = Bob::Util.underscorize_hash(raw)

  attributes.each do |k, v|
    k = "field_#{k}" if k.instance_of?(String) && k.to_i.positive?
    instance_variable_set("@#{k}", v.is_a?(Hash) ? Models::Base.new(v) : v)
    self.class.send(:define_method, k, proc { instance_variable_get("@#{k}") })
    self.class.send(:define_method, "#{k}=", proc { |val| instance_variable_set("@#{k}", val) })
  rescue NameError
    next
  end
end