Class: RawgApi::Entities::BaseEntity
- Inherits:
-
ApiStruct::Entity
- Object
- ApiStruct::Entity
- RawgApi::Entities::BaseEntity
- Extended by:
- RawgApi::Extensions::DryMonads
- Defined in:
- lib/rawg_api/entities/base_entity.rb
Direct Known Subclasses
Creator, CreatorRole, Developer, Game::Achievements, Game::Additions, Game::Details, Game::DevelopmentTeam, Game::Entity, Game::ParentGames, Game::Reddit, Game::Screenshots, Game::Series, Game::Stores, Game::SuggestedGames, Game::Trailers, Game::Twitch, Game::YouTube, Genres, ParentPlatform, Platform, Publisher, Store, Tag, TagDetails
Class Attribute Summary collapse
-
.delegated_methods ⇒ Object
Returns the value of attribute delegated_methods.
Class Method Summary collapse
- .collection(entities, entity_type = self) ⇒ Object
- .delegate_method(resolver_klass = nil, *args) ⇒ Object
Instance Method Summary collapse
-
#initialize(entity, entity_status = true) ⇒ BaseEntity
constructor
A new instance of BaseEntity.
Methods included from RawgApi::Extensions::DryMonads
Constructor Details
#initialize(entity, entity_status = true) ⇒ BaseEntity
Returns a new instance of BaseEntity.
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_methods ⇒ Object
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 |