Class: Sfn::Page
- Inherits:
-
HasSatisfaction
- Object
- HasSatisfaction
- Sfn::Page
- Extended by:
- Forwardable
- Defined in:
- lib/satisfaction/resource.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Attributes inherited from HasSatisfaction
Instance Method Summary collapse
-
#initialize(collection, page, options = {}) ⇒ Page
constructor
A new instance of Page.
-
#items ⇒ Object
Retrieve the items for this page * Caches.
- #load(force = false) ⇒ Object
- #loaded? ⇒ Boolean
- #next ⇒ Object
- #next? ⇒ Boolean
- #page_count ⇒ Object
- #page_size ⇒ Object
- #prev ⇒ Object
- #prev? ⇒ Boolean
Constructor Details
#initialize(collection, page, options = {}) ⇒ Page
Returns a new instance of Page.
137 138 139 140 141 142 143 144 145 |
# File 'lib/satisfaction/resource.rb', line 137 def initialize(collection, page, ={}) super(collection.satisfaction) @collection = collection @klass = collection.klass @page = page @path = collection.path = [:limit] ||= 10 end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
121 122 123 |
# File 'lib/satisfaction/resource.rb', line 121 def collection @collection end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
120 121 122 |
# File 'lib/satisfaction/resource.rb', line 120 def total @total end |
Instance Method Details
#items ⇒ Object
Retrieve the items for this page
-
Caches
149 150 151 152 |
# File 'lib/satisfaction/resource.rb', line 149 def items load @data end |
#load(force = false) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/satisfaction/resource.rb', line 189 def load(force=false) return @data if loaded? && !force result = satisfaction.get("#{@path}.json", .merge(:page => @page)) if result.first == :ok json = JSON.parse(result.last) @total = json["total"] @data = json["data"].map do |result| obj = @klass.decode_sfn(result, satisfaction) satisfaction.identity_map.get_record(@klass, obj.id) do obj end end else result end end |
#loaded? ⇒ Boolean
154 155 156 |
# File 'lib/satisfaction/resource.rb', line 154 def loaded? !@data.nil? end |
#next ⇒ Object
168 169 170 171 |
# File 'lib/satisfaction/resource.rb', line 168 def next return nil unless next? self.class.new(@collection, @page + 1, ) end |
#next? ⇒ Boolean
162 163 164 165 166 |
# File 'lib/satisfaction/resource.rb', line 162 def next? load #this loads the data, we shold probably make load set the ivar instead of items ;) last_item = @page * page_size @total > last_item end |
#page_count ⇒ Object
183 184 185 186 187 |
# File 'lib/satisfaction/resource.rb', line 183 def page_count result = @total / length result += 1 if @total % length != 0 result end |
#page_size ⇒ Object
158 159 160 |
# File 'lib/satisfaction/resource.rb', line 158 def page_size [:limit] end |
#prev ⇒ Object
178 179 180 181 |
# File 'lib/satisfaction/resource.rb', line 178 def prev return nil unless prev? self.class.new(@collection, @page - 1, ) end |
#prev? ⇒ Boolean
174 175 176 |
# File 'lib/satisfaction/resource.rb', line 174 def prev? @page > 1 end |