Class: RawgApi::Entities::BaseEntity

Inherits:
ApiStruct::Entity
  • Object
show all
Extended by:
RawgApi::Extensions::DryMonads
Defined in:
lib/rawg_api/entities/base_entity.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RawgApi::Extensions::DryMonads

from_success

Constructor Details

#initialize(entity, entity_status = true) ⇒ BaseEntity

Returns a new instance of BaseEntity.

Raises:

  • (EntityError)


45
46
47
48
49
50
# File 'lib/rawg_api/entities/base_entity.rb', line 45

def initialize(entity, entity_status = true)
  raise EntityError, "#{entity} must be Hash" unless entity.is_a?(Hash)
  @entity = BaseEntityHashie.new(extract_attributes(entity))
  @entity_status = entity_status
  __setobj__(@entity)
end

Class Attribute Details

.delegated_methodsObject

Returns the value of attribute delegated_methods.



15
16
17
# File 'lib/rawg_api/entities/base_entity.rb', line 15

def delegated_methods
  @delegated_methods
end

Class Method Details

.collection(entities, entity_type = self) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rawg_api/entities/base_entity.rb', line 33

def collection(entities, entity_type = self)
  if !entities.is_a?(Array) && entities.key?(:results)
     = entities.dup
    .delete(:results)
    results = ::ApiStruct::Collection.new(entities[:results], entity_type)
    EntityResponse.new(, results)
  else
    ::ApiStruct::Collection.new(entities, entity_type)
  end
end

.delegate_method(resolver_klass = nil, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rawg_api/entities/base_entity.rb', line 17

def delegate_method(resolver_klass = nil, *args)
  define_method = args[0].to_sym
  call_method = args[1].to_sym
  define_singleton_method(define_method) do |*attrs|
    id = attrs.shift
    args = attrs.shift || {}
    if args.empty?
      resolver_klass.send(call_method, id)
    else
      resolver_klass.send(call_method, id, **args)
    end
  end
  self.delegated_methods ||= {}
  self.delegated_methods[define_method] = {class: resolver_klass, method: call_method}
end