class ChgkRating::Models::Base

Attributes

lazy[R]

Public Class Methods

inherited(subclass) click to toggle source
Calls superclass method
# File lib/chgk_rating/models/base.rb, line 8
def self.inherited(subclass)
  attr_reader(*subclass.attribute_mapping.keys) if subclass.attribute_mapping
  super
end
new(id_or_hash, params = {}) click to toggle source
# File lib/chgk_rating/models/base.rb, line 13
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
no_eager_loading!() click to toggle source
# File lib/chgk_rating/models/base.rb, line 32
def self.no_eager_loading!
  define_method :eager_load! do |*_args|
    raise ChgkRating::Error::EagerLoadingNotSupported
  end
end
no_lazy_support!() click to toggle source
# File lib/chgk_rating/models/base.rb, line 38
def self.no_lazy_support!
  self.const_set :NO_LAZY_SUPPORT, true
  undef_method :lazy
end

Private Class Methods

attribute_mapping() click to toggle source
# File lib/chgk_rating/models/base.rb, line 54
def self.attribute_mapping
  return nil unless self.name
  ChgkRating::AttributeMappings.find self.name
end

Public Instance Methods

eager_load!(force = false) click to toggle source

Load data from API if the resource was initially lazily loaded. Set `force` to reload data even if it is already present.

# File lib/chgk_rating/models/base.rb, line 25
def eager_load!(force = false)
  return unless @lazy || force
  extract_from raw_by(self.id)
  @lazy = false
  self
end
to_h() click to toggle source
# File lib/chgk_rating/models/base.rb, line 43
def to_h
  self.class.attribute_mapping.inject({}) do |memo, (attr, mapping)|
    data = self.send attr
    data = mapping[:transform_down].call(data) if mapping.has_key?(:transform_down)
    memo[ mapping[:raw_name] ] = data
    memo
  end
end

Private Instance Methods

extract_from(raw_data) click to toggle source
# File lib/chgk_rating/models/base.rb, line 81
def extract_from(raw_data)
  self.class.attribute_mapping.each do |attr, mapping|
    data = raw_data.is_a?(self.class) ? raw_data.send(attr) : raw_data[ mapping[:raw_name] ]
    data = mapping[:transform_up].call(data) if mapping.has_key?(:transform_up)
    instance_variable_set "@#{attr}", data
  end
end
get_result_by(object) click to toggle source
# File lib/chgk_rating/models/base.rb, line 76
def get_result_by(object)
  result = get "#{api_path}/#{object}"
  result.is_a?(Array) ? result.first : result
end
lazy_load?(params) click to toggle source
# File lib/chgk_rating/models/base.rb, line 59
def lazy_load?(params)
  self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
end
raw_by(object, lazy = false) click to toggle source

If the `object` is a `String` or `Integer` - that's an id. If `lazy` is not set to `true` we need to perform an API request. If `lazy` is set to `true` - do nothing, we are going to only save this ID. If the `object` is not a `String` or `Integer`, it already has all the necessary information and so we simply create a new instance of the class using it.

# File lib/chgk_rating/models/base.rb, line 69
def raw_by(object, lazy = false)
  return object unless object.is_a?(String) || object.is_a?(Integer) || object.is_a?(Symbol)
  return nil if lazy

  get_result_by object
end