Class: Sfn::Page

Inherits:
HasSatisfaction show all
Extended by:
Forwardable
Defined in:
lib/satisfaction/resource.rb

Instance Attribute Summary collapse

Attributes inherited from HasSatisfaction

#satisfaction

Instance Method Summary collapse

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, options={})
  super(collection.satisfaction)
  @collection = collection
  @klass = collection.klass
  @page = page
  @path = collection.path
  @options = options
  @options[:limit] ||= 10
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



121
122
123
# File 'lib/satisfaction/resource.rb', line 121

def collection
  @collection
end

#totalObject (readonly)

Returns the value of attribute total.



120
121
122
# File 'lib/satisfaction/resource.rb', line 120

def total
  @total
end

Instance Method Details

#itemsObject

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", @options.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

Returns:

  • (Boolean)


154
155
156
# File 'lib/satisfaction/resource.rb', line 154

def loaded?
  !@data.nil?
end

#nextObject



168
169
170
171
# File 'lib/satisfaction/resource.rb', line 168

def next
  return nil unless next?
  self.class.new(@collection, @page + 1, @options)
end

#next?Boolean

Returns:

  • (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_countObject



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_sizeObject



158
159
160
# File 'lib/satisfaction/resource.rb', line 158

def page_size
  @options[:limit]
end

#prevObject



178
179
180
181
# File 'lib/satisfaction/resource.rb', line 178

def prev
  return nil unless prev?
  self.class.new(@collection, @page - 1, @options)
end

#prev?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/satisfaction/resource.rb', line 174

def prev?
  @page > 1
end