Class: Cubits::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cubits/resource_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



7
8
9
10
11
# File 'lib/cubits/resource_collection.rb', line 7

def initialize(params = {})
  @path = params[:path]
  @resource = params[:resource]
  @exposed_methods = params[:expose_methods]
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/cubits/resource_collection.rb', line 5

def path
  @path
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/cubits/resource_collection.rb', line 5

def resource
  @resource
end

Instance Method Details

#allArray<Resource>

Loads collection of resources

Returns:



36
37
38
39
40
41
42
43
44
45
# File 'lib/cubits/resource_collection.rb', line 36

def all
  fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all)
  list = []
  page_count.times do |i|
    list += page(i + 1)
  end
  list
rescue NotFound
  []
end

#collection_nameObject

By convention collection name for the resource is the last part of the path



15
16
17
# File 'lib/cubits/resource_collection.rb', line 15

def collection_name
  path.split('/').last
end

#countObject Also known as: size

Returns number of elements in the collection



27
28
29
# File 'lib/cubits/resource_collection.rb', line 27

def count
  pagination.total_count
end

#each(&_block) ⇒ Object

Iterates through the collection, yielding the giving block for each resource.



84
85
86
87
88
89
90
91
# File 'lib/cubits/resource_collection.rb', line 84

def each(&_block)
  return to_enum unless block_given?
  page_count.times do |i|
    page(i + 1).each do |r|
      yield r
    end
  end
end

#exposed_method?(method_name) ⇒ Boolean

Returns true if the method is exposed by this resource.

Returns:

  • (Boolean)

    true if the method is exposed by this resource



21
22
23
# File 'lib/cubits/resource_collection.rb', line 21

def exposed_method?(method_name)
  (@exposed_methods || []).include?(method_name)
end

#find(id) ⇒ Object

Loads resource

Parameters:

  • id (String)

Returns:

  • nil if resource is not found



65
66
67
68
69
70
# File 'lib/cubits/resource_collection.rb', line 65

def find(id)
  fail NoMethodError, "Resource #{name} does not expose .find" unless exposed_method?(:find)
  resource.new Cubits.connection.get(path_to(id))
rescue NotFound
  nil
end

#firstObject

Returns first element of the collection



49
50
51
# File 'lib/cubits/resource_collection.rb', line 49

def first
  first_page.first
end

#lastObject

Returns last element of the collection



55
56
57
# File 'lib/cubits/resource_collection.rb', line 55

def last
  last_page.last
end

#nameObject



106
107
108
# File 'lib/cubits/resource_collection.rb', line 106

def name
  "Collection of #{resource.name}"
end

#path_to(resource_or_id = nil) ⇒ Object

Returns API path to resource



95
96
97
98
99
100
101
102
103
104
# File 'lib/cubits/resource_collection.rb', line 95

def path_to(resource_or_id = nil)
  fail ArgumentError, "Resource path is not set for #{self}" unless path
  if resource_or_id.is_a?(Resource)
    "#{path}/#{resource_or_id.id}"
  elsif resource_or_id
    "#{path}/#{resource_or_id}"
  else
    path
  end
end

#reloadself

Reloads collection

Returns:

  • (self)


76
77
78
79
80
# File 'lib/cubits/resource_collection.rb', line 76

def reload
  @pagination = nil
  @pages = nil
  self
end

#to_sObject



110
111
112
# File 'lib/cubits/resource_collection.rb', line 110

def to_s
  "<#{self.class.name} of #{resource.name}, #{path}>"
end