Class: ChgkRating::Models::Base
- Inherits:
-
ChgkObject
- Object
- ChgkObject
- ChgkRating::Models::Base
- Includes:
- Request
- Defined in:
- lib/chgk_rating/models/base.rb
Direct Known Subclasses
Player, PlayerTournament, Rating, Recap, Team, Tournament, TournamentTeam, TournamentTeamPlayer, TournamentTeamResult
Constant Summary
Constants included from Connection
Instance Attribute Summary collapse
-
#lazy ⇒ Object
readonly
Returns the value of attribute lazy.
Class Method Summary collapse
-
.attribute_mapping ⇒ Object
Grab the attribute mapping for the class and its superclass (as superclass may present common mappings for multiple classes).
- .inherited(subclass) ⇒ Object
- .no_eager_loading! ⇒ Object
- .no_lazy_support! ⇒ Object
Instance Method Summary collapse
-
#eager_load!(force = false) ⇒ Object
Load data from API if the resource was initially lazily loaded.
-
#initialize(id_or_hash, params = {}) ⇒ Base
constructor
A new instance of Base.
- #to_h ⇒ Object
Methods included from Request
Methods included from Connection
Methods inherited from ChgkObject
#build_model, #extract_id_from
Constructor Details
#initialize(id_or_hash, params = {}) ⇒ Base
Returns a new instance of Base.
15 16 17 18 19 20 21 22 23 |
# File 'lib/chgk_rating/models/base.rb', line 15 def initialize(id_or_hash, params = {}) raw = raw_by id_or_hash, lazy_load?(params) # If we only have an ID and `lazy` is set to true, then simply store this ID # Otherwise extract all data from the hash raw.nil? ? @id = id_or_hash : extract_from(raw) @lazy = (params[:lazy] || false) unless self.class.const_defined?(:NO_LAZY_SUPPORT) end |
Instance Attribute Details
#lazy ⇒ Object (readonly)
Returns the value of attribute lazy.
8 9 10 |
# File 'lib/chgk_rating/models/base.rb', line 8 def lazy @lazy end |
Class Method Details
.attribute_mapping ⇒ Object
Grab the attribute mapping for the class and its superclass (as superclass may present common mappings for multiple classes)
57 58 59 60 61 62 |
# File 'lib/chgk_rating/models/base.rb', line 57 def attribute_mapping return nil unless name ChgkRating::AttributeMappings.find(name). merge(ChgkRating::AttributeMappings.find(superclass.name)) end |
.inherited(subclass) ⇒ Object
10 11 12 13 |
# File 'lib/chgk_rating/models/base.rb', line 10 def self.inherited(subclass) attr_reader(*subclass.attribute_mapping.keys) if subclass.attribute_mapping super end |
.no_eager_loading! ⇒ Object
35 36 37 38 39 |
# File 'lib/chgk_rating/models/base.rb', line 35 def self.no_eager_loading! define_method :eager_load! do |*_args| raise ChgkRating::Error::EagerLoadingNotSupported end end |
.no_lazy_support! ⇒ Object
41 42 43 44 |
# File 'lib/chgk_rating/models/base.rb', line 41 def self.no_lazy_support! const_set :NO_LAZY_SUPPORT, true undef_method :lazy end |
Instance Method Details
#eager_load!(force = false) ⇒ Object
Load data from API if the resource was initially lazily loaded. Set ‘force` to reload data even if it is already present.
27 28 29 30 31 32 33 |
# File 'lib/chgk_rating/models/base.rb', line 27 def eager_load!(force = false) return unless @lazy || force extract_from raw_by(id) @lazy = false self end |
#to_h ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/chgk_rating/models/base.rb', line 46 def to_h self.class.attribute_mapping.each_with_object({}) do |(attr, mapping), memo| data = send attr data = mapping[:transform_down].call(data) if mapping.key?(:transform_down) memo[mapping[:raw_name]] = data end end |