Class: Universities::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/universities/entity.rb

Constant Summary collapse

ATTRIBUTES_MAPPING =
{
  web_pages: 'web_pages',
  alpha_two_code: 'alpha_two_code',
  state_province: 'state-province',
  country: 'country',
  domains: 'domains',
  name: 'name'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_pages: nil, alpha_two_code: nil, state_province: nil, country: nil, domains: nil, name: nil) ⇒ Entity



15
16
17
18
19
20
21
22
23
# File 'lib/universities/entity.rb', line 15

def initialize(web_pages: nil, alpha_two_code: nil, state_province: nil, country: nil, domains: nil, name: nil)
  local_variables.each do |key|
    self.class.class_eval do
      attr_reader key
    end
    value = eval(key.to_s)
    instance_variable_set("@#{key}", value)
  end
end

Class Method Details

.build(hash) ⇒ Object



25
26
27
# File 'lib/universities/entity.rb', line 25

def self.build(hash)
  new(**parse_attributes(hash))
end

.parse_attributes(hash) ⇒ Object



29
30
31
32
33
34
# File 'lib/universities/entity.rb', line 29

def self.parse_attributes(hash)
  hash = hash.with_indifferent_access
  ATTRIBUTES_MAPPING.map do |attribute, hash_key|
    [attribute, hash.dig(hash_key)]
  end.to_h
end