Class: Models::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Base

Returns a new instance of Base.



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

def initialize(raw)
  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

Instance Method Details

#dig_custom_field(field) ⇒ Object



18
19
20
21
22
23
# File 'lib/bob/models/base.rb', line 18

def dig_custom_field(field)
  Bob.custom_fields[field][:dig_path].reduce(self) { |response, sub_field| response.send(sub_field) }
rescue NoMethodError => e
  Rails.logger.info("Error: #{e.message}")
  nil
end