Class: WeThePeople::Collection
- Inherits:
-
Object
- Object
- WeThePeople::Collection
- Includes:
- Enumerable
- Defined in:
- lib/we_the_people/collection.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #each ⇒ Object
- #fetch_current_page ⇒ Object
- #get_all(refresh = false) ⇒ Object
-
#initialize(klass, conditions, hash, parent = nil) ⇒ Collection
constructor
A new instance of Collection.
- #length ⇒ Object (also: #size)
- #next_page ⇒ Object
- #page_length ⇒ Object (also: #page_size)
- #previous_page ⇒ Object
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
#all ⇒ Object (readonly)
Returns the value of attribute all.
4 5 6 |
# File 'lib/we_the_people/collection.rb', line 4 def all @all end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
4 5 6 |
# File 'lib/we_the_people/collection.rb', line 4 def count @count end |
#current_page ⇒ Object (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 |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
4 5 6 |
# File 'lib/we_the_people/collection.rb', line 4 def limit @limit end |
#offset ⇒ Object (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
#each ⇒ Object
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_page ⇒ Object
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 |
#length ⇒ Object Also known as: size
57 58 59 |
# File 'lib/we_the_people/collection.rb', line 57 def length @count end |
#next_page ⇒ Object
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_length ⇒ Object Also known as: page_size
62 63 64 |
# File 'lib/we_the_people/collection.rb', line 62 def page_length @limit end |
#previous_page ⇒ Object
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 |