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

Returns a new instance of Base.



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

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.



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

def items
  @items
end

Instance Method Details

#[](index) ⇒ Object



22
23
24
# File 'lib/chgk_rating/collections/base.rb', line 22

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

#convertable?(method) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/chgk_rating/collections/base.rb', line 47

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

  false
end

#eachObject



18
19
20
# File 'lib/chgk_rating/collections/base.rb', line 18

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

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

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/chgk_rating/collections/base.rb', line 38

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

#to_aObject



26
27
28
29
30
# File 'lib/chgk_rating/collections/base.rb', line 26

def to_a
  raise ChgkRating::Error::NotArrayType unless respond_to?(:to_a)

  items.to_a.map(&:to_h)
end

#to_hObject



32
33
34
35
36
# File 'lib/chgk_rating/collections/base.rb', line 32

def to_h
  raise ChgkRating::Error::NotHashType unless respond_to?(:to_h)

  items.to_h { |k, v| revert_to_hash(k, v) }
end