Class: ChgkRating::Collections::Base

Inherits:
ChgkRating::ChgkObject show all
Includes:
Request, Enumerable
Defined in:
lib/chgk_rating/collections/base.rb

Constant Summary

Constants included from ChgkRating::Connection

ChgkRating::Connection::BASE_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Request

#get

Methods included from ChgkRating::Connection

#connection

Methods inherited from ChgkRating::ChgkObject

#build_model, #extract_id_from

Constructor Details

#initialize(params = {}) ⇒ Base


9
10
11
12
13
14
# File 'lib/chgk_rating/collections/base.rb', line 9

def initialize(params = {})
  results = params[:collection] ||
      prepare(get(api_path, build_request_params_from(params)))

  @items = process results, params
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items


7
8
9
# File 'lib/chgk_rating/collections/base.rb', line 7

def items
  @items
end

Instance Method Details

#[](index) ⇒ Object


20
21
22
# File 'lib/chgk_rating/collections/base.rb', line 20

def [](index)
  @items[index]
end

#convertable?(method) ⇒ Boolean


43
44
45
46
47
# File 'lib/chgk_rating/collections/base.rb', line 43

def convertable?(method)
  return true if (method == :to_a && self.items.is_a?(Array)) ||
      (method == :to_h && self.items.is_a?(Hash))
  false
end

#eachObject


16
17
18
# File 'lib/chgk_rating/collections/base.rb', line 16

def each
  @items.each { |item| yield(*item) }
end

#respond_to?(method, include_all = false) ⇒ Boolean


34
35
36
37
38
39
40
41
# File 'lib/chgk_rating/collections/base.rb', line 34

def respond_to?(method, include_all = false)
  method = method.to_sym
  if i( to_a to_h ).include?(method.to_sym)
    self.convertable? method
  else
    super
  end
end

#to_aObject


24
25
26
27
# File 'lib/chgk_rating/collections/base.rb', line 24

def to_a
  raise ChgkRating::Error::NotArrayType unless self.respond_to?(:to_a)
  self.items.to_a.map &:to_h
end

#to_hObject


29
30
31
32
# File 'lib/chgk_rating/collections/base.rb', line 29

def to_h
  raise ChgkRating::Error::NotHashType unless self.respond_to?(:to_h)
  self.items.map { |k,v| revert_to_hash(k, v) }.to_h
end