Class: Hashblue::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/hashblue/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, model_class, data, name) ⇒ Collection

Returns a new instance of Collection.



2
3
4
5
6
7
8
9
10
# File 'lib/hashblue/collection.rb', line 2

def initialize(client, model_class, data, name)
  @client = client
  @model_class = model_class
  @data = data
  @name = name
  @items = @data[name].collect do |item|
    model_class.build(client, item)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



16
17
18
# File 'lib/hashblue/collection.rb', line 16

def method_missing(method, *args, &block)
  @items.respond_to?(method) ? @items.send(method, *args, &block) : super
end

Instance Method Details

#each_page {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
# File 'lib/hashblue/collection.rb', line 44

def each_page(&block)
  yield self if block_given?
  next_page.each_page(&block) if next_page?
end

#next_pageObject



24
25
26
27
28
29
30
# File 'lib/hashblue/collection.rb', line 24

def next_page
  if next_page?
    self.class.new(@client, @model_class, @client.get(@data["next_page_uri"]), @name)
  else
    nil
  end
end

#next_page?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/hashblue/collection.rb', line 20

def next_page?
  !!@data["next_page_uri"]
end

#previous_pageObject



36
37
38
39
40
41
42
# File 'lib/hashblue/collection.rb', line 36

def previous_page
  if previous_page?
    self.class.new(@client, @model_class, @client.get(@data["previous_page_uri"]), @name)
  else
    nil
  end
end

#previous_page?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/hashblue/collection.rb', line 32

def previous_page?
  !!@data["previous_page_uri"]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hashblue/collection.rb', line 12

def respond_to?(method)
  super || @items.respond_to?(method)
end