Class: Erede::Models::Base

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

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



4
5
6
# File 'lib/erede/models/base.rb', line 4

def initialize
  set_accessors_for_api_attributes
end

Instance Method Details

#set_accessors_for_api_attributesObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/erede/models/base.rb', line 8

def set_accessors_for_api_attributes
  self.class.const_get(:API_ATTRIBUTES).each do |attr_name|
    define_singleton_method attr_name do
      instance_variable_get("@#{attr_name}")
    end

    define_singleton_method "#{attr_name}=" do |value|
      instance_variable_set("@#{attr_name}", value)
    end
  end
end

#to_jsonObject



20
21
22
23
24
25
26
# File 'lib/erede/models/base.rb', line 20

def to_json(*)
  Hash[
    self.class.const_get(:API_ATTRIBUTES).map { |attr_name| [attr_name, send(attr_name)] }
  ]
  .delete_if { |k, v| v.nil? }
  .to_json
end