Class: WeThePeople::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/we_the_people/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, conditions, hash, parent = nil) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/we_the_people/collection.rb', line 6

def initialize(klass, conditions, hash, parent = nil)
  @criteria = conditions
  @parent = parent
  
  if hash['metadata']['resultset']
    @count = hash['metadata']['resultset']['count'].to_i
    @offset = hash['metadata']['resultset']['offset'].to_i
    @limit = hash['metadata']['resultset']['limit'].to_i
  else
    @count = WeThePeople::Config.default_page_size
    @offset = 0
    @limit = WeThePeople::Config.default_page_size
  end

  @klass = klass

  @all = []
  process_results(hash['results'])
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



4
5
6
# File 'lib/we_the_people/collection.rb', line 4

def all
  @all
end

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/we_the_people/collection.rb', line 4

def count
  @count
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/we_the_people/collection.rb', line 4

def current_page
  @current_page
end

#limitObject (readonly)

Returns the value of attribute limit.



4
5
6
# File 'lib/we_the_people/collection.rb', line 4

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



4
5
6
# File 'lib/we_the_people/collection.rb', line 4

def offset
  @offset
end

Instance Method Details

#eachObject



67
68
69
70
71
# File 'lib/we_the_people/collection.rb', line 67

def each
  @all.each do |record|
    yield record
  end
end

#fetch_current_pageObject



33
34
35
# File 'lib/we_the_people/collection.rb', line 33

def fetch_current_page
  fetch_page(@offset, @limit)
end

#get_all(refresh = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/we_the_people/collection.rb', line 44

def get_all(refresh = false)
  if refresh 
    @all = []
    @offset = 0
  end

  (@offset..@count).step(@limit) do |current_offset|
    @all += fetch_page(current_offset, @limit)
  end

  @all
end

#lengthObject Also known as: size



57
58
59
# File 'lib/we_the_people/collection.rb', line 57

def length
  @count
end

#next_pageObject



26
27
28
29
30
31
# File 'lib/we_the_people/collection.rb', line 26

def next_page
  @offset += @limit unless @all.empty?

  fetch_current_page
  @current_page
end

#page_lengthObject Also known as: page_size



62
63
64
# File 'lib/we_the_people/collection.rb', line 62

def page_length
  @limit
end

#previous_pageObject



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

def previous_page
  @offset -= @limit
  @offset = 0 if @offset < 0

  @all.slice(@offset, @limit)
end