Class: Datmachine::Pager
- Inherits:
-
Object
- Object
- Datmachine::Pager
- Includes:
- Enumerable
- Defined in:
- lib/datmachine/pager.rb
Constant Summary collapse
- DEFAULT_SEP =
/[&;] */n
- DEFAULT_LIMIT =
10
Instance Attribute Summary collapse
-
#href ⇒ Object
Returns the value of attribute href.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #create(options = {}) ⇒ Object
- #current_page ⇒ Object
-
#each {|record| ... } ⇒ Array
Iterates through the current page of records.
- #fetch(uri) ⇒ Object
- #fetch_each {|record| ... } ⇒ nil
- #first ⇒ Object
-
#initialize(href, options = {}) ⇒ Pager
constructor
A pager for paginating through resource records.
- #items ⇒ Object
- #limit ⇒ Object (also: #limit_value)
-
#load! ⇒ Array?
(also: #reload)
original, supplied options.
-
#next ⇒ Array?
the next page.
- #num_pages ⇒ Object
- #offset ⇒ Object (also: #offset_value)
-
#paginate(options = {}) ⇒ Pager
(also: #scoped, #where)
supplied.
-
#prev ⇒ Array?
the previous page.
- #resource_class ⇒ Object
-
#start ⇒ Array?
the first page.
- #total ⇒ Object
Constructor Details
#initialize(href, options = {}) ⇒ Pager
A pager for paginating through resource records.
20 21 22 23 24 25 |
# File 'lib/datmachine/pager.rb', line 20 def initialize(href, = {}) @href = href @options = @page = nil @resource_class = nil end |
Instance Attribute Details
#href ⇒ Object
Returns the value of attribute href.
10 11 12 |
# File 'lib/datmachine/pager.rb', line 10 def href @href end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/datmachine/pager.rb', line 11 def @options end |
Instance Method Details
#all(options = {}) ⇒ Object
157 158 159 |
# File 'lib/datmachine/pager.rb', line 157 def all( = {}) paginate().to_a end |
#create(options = {}) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/datmachine/pager.rb', line 170 def create(={}) opts = Datmachine::Utils.indifferent_read_access opts[:href] = @href # if we don't have a media type for the href, # let's try to inspect the url to look it up from the # registry if @resource_class.nil? @resource_class = Datmachine.from_href(@href) end @resource_class.new(opts).save end |
#current_page ⇒ Object
73 74 75 |
# File 'lib/datmachine/pager.rb', line 73 def current_page (offset / limit) + 1 end |
#each {|record| ... } ⇒ Array
Returns Iterates through the current page of records.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/datmachine/pager.rb', line 85 def each return enum_for :each unless block_given? load! unless @page loop do items.each do |r| envelope = { :meta => @page[:meta], :links => @page[:links], @resource_class.collection_name.to_sym => [r] } yield resource_class.construct_from_response(envelope) end raise StopIteration if @page[:meta].nil? #raise StopIteration if @page[:meta][:next].nil? self.next end end |
#fetch(uri) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/datmachine/pager.rb', line 161 def fetch(uri) if resource_class.respond_to? :find raise NoMethodError, "#find must be called on #{resource_class} directly" end resource_class.fetch uri end |
#fetch_each {|record| ... } ⇒ nil
107 108 109 110 111 112 |
# File 'lib/datmachine/pager.rb', line 107 def fetch_each return enum_for :fetch_each unless block_given? begin each { |record| yield record } end while self.next end |
#first ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/datmachine/pager.rb', line 33 def first load! unless @page if items.first.nil? nil else envelope = { :meta => @page[:meta], :links => @page[:links], @resource_class.collection_name.to_sym => [items.first] } resource_class.construct_from_response(envelope) end end |
#items ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/datmachine/pager.rb', line 64 def items load! unless @page if @resource_class.nil? [] else @page[@resource_class.collection_name] end end |
#limit ⇒ Object Also known as: limit_value
52 53 54 55 |
# File 'lib/datmachine/pager.rb', line 52 def limit load! unless @page @page[:meta][:limit] end |
#load! ⇒ Array? Also known as: reload
original, supplied options.
140 141 142 |
# File 'lib/datmachine/pager.rb', line 140 def load! load_from @href, @options end |
#next ⇒ Array?
the next page.
116 117 118 119 120 |
# File 'lib/datmachine/pager.rb', line 116 def next load! unless @page next_uri = @page[:meta][:next] load_from next_uri, nil unless next_uri.nil? end |
#num_pages ⇒ Object
77 78 79 80 81 |
# File 'lib/datmachine/pager.rb', line 77 def num_pages num = total / limit num += 1 if total % limit > 0 num end |
#offset ⇒ Object Also known as: offset_value
58 59 60 61 |
# File 'lib/datmachine/pager.rb', line 58 def offset load! unless @page @page[:meta][:offset] end |
#paginate(options = {}) ⇒ Pager Also known as: scoped, where
supplied. Useful for resource scopes.
148 149 150 151 152 153 |
# File 'lib/datmachine/pager.rb', line 148 def paginate( = {}) dup.instance_eval { @page = nil @options.update and self } end |
#prev ⇒ Array?
the previous page.
124 125 126 127 128 |
# File 'lib/datmachine/pager.rb', line 124 def prev load! unless @page prev_uri = @page[:meta][:prev] load_from prev_uri, nil unless prev_uri.nil? end |
#resource_class ⇒ Object
27 28 29 30 31 |
# File 'lib/datmachine/pager.rb', line 27 def resource_class return @resource_class unless @resource_class.nil? load! unless @page @resource_class end |
#start ⇒ Array?
the first page.
132 133 134 135 136 |
# File 'lib/datmachine/pager.rb', line 132 def start load! unless @page first_page = @page[:meta][:first] load_from first_page, nil unless first_page.nil? end |
#total ⇒ Object
47 48 49 50 |
# File 'lib/datmachine/pager.rb', line 47 def total load! unless @page @page[:meta][:total] end |