Class: RatingChgkV2::Models::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rating_chgk_v2/models/base_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data, endpoint = nil) ⇒ BaseModel

Returns a new instance of BaseModel.



14
15
16
17
18
19
20
# File 'lib/rating_chgk_v2/models/base_model.rb', line 14

def initialize(raw_data, endpoint = nil)
  @endpoint = endpoint

  self.class.const_get(:ATTRIBUTES).each do |att|
    instance_variable_set :"@#{att}", (raw_data[att] || raw_data[att.to_sym])
  end
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/rating_chgk_v2/models/base_model.rb', line 8

def endpoint
  @endpoint
end

Class Method Details

.inherited(subclass) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rating_chgk_v2/models/base_model.rb', line 22

def self.inherited(subclass)
  subclass.class_exec do
    filename = subclass.name.base_class_name.snakecase.delete_suffix('_model')
    attrs = YAML.load_file File.expand_path("../data/#{filename}.yml", __dir__)

    const_set :ATTRIBUTES, attrs

    attrs.each do |att|
      attr_reader(att)
    end
  end

  super
end

.load(method, endpoint) ⇒ Object



10
11
12
# File 'lib/rating_chgk_v2/models/base_model.rb', line 10

def self.load(method, endpoint)
  new endpoint.send(method), endpoint
end