Class: Paginator::Paginate
- Inherits:
-
Object
- Object
- Paginator::Paginate
- Defined in:
- lib/appengine-paginator/paginate.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
-
.entity_to_hash(entity, properties) ⇒ Object
This should be moved to the Adapter.
Instance Method Summary collapse
-
#cursor ⇒ Object
returns the cursor (the starting position for the next page).
-
#datamapper_result ⇒ Object
Returns an array of DataMapper objects.
-
#initialize(query, limit, *options) ⇒ Paginate
constructor
creates a paginate object * query - an instance of AppEngine::Datastore::Query * limit - number of items per page * :model => DataMapperObject - used to cast the datamapper_result (optional) * :cursor => instance of Cursor - used to skip to the next page in the query (optional).
-
#result ⇒ Object
Returns an array of entities.
Constructor Details
#initialize(query, limit, *options) ⇒ Paginate
creates a paginate object
-
query - an instance of AppEngine::Datastore::Query
-
limit - number of items per page
-
:model => DataMapperObject - used to cast the datamapper_result (optional)
-
:cursor => instance of Cursor - used to skip to the next page in the query (optional)
10 11 12 13 14 |
# File 'lib/appengine-paginator/paginate.rb', line 10 def initialize(query, limit, *) @query = query @limit = limit @options = .first || {} end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
3 4 5 |
# File 'lib/appengine-paginator/paginate.rb', line 3 def limit @limit end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/appengine-paginator/paginate.rb', line 3 def @options end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/appengine-paginator/paginate.rb', line 3 def query @query end |
Class Method Details
.entity_to_hash(entity, properties) ⇒ Object
This should be moved to the Adapter
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/appengine-paginator/paginate.rb', line 42 def self.entity_to_hash(entity, properties) return if entity.nil? key = entity.get_key hash = {} properties.each do |property| name = property.field if property.key? hash[name] = if property.kind_of? DataMapper::Property::Key key elsif property.serial? key.get_id elsif property.kind_of? DataMapper::Property::String key.get_name else key end else value = entity.get_property(name) if property.kind_of?(DataMapper::Property::Decimal) && value value = Lexidecimal.string_to_decimal(entity.get_property(name)) end hash[name] = value end end hash end |
Instance Method Details
#cursor ⇒ Object
returns the cursor (the starting position for the next page)
37 38 39 |
# File 'lib/appengine-paginator/paginate.rb', line 37 def cursor Cursor.new result.cursor end |
#datamapper_result ⇒ Object
Returns an array of DataMapper objects. This array is the current page based on the position of the cursor (or the first page if no cursor is passed in). In order for the DataMapper objects to be loaded the paginate object must be crated with the :model option
30 31 32 33 34 |
# File 'lib/appengine-paginator/paginate.rb', line 30 def datamapper_result model = [:model] dm_query =Struct.new(:repository, :fields, :reload?).new(DataMapper::repository, model.properties, false) result.collect { |e| model.load([Paginate.entity_to_hash(e, model.properties)], dm_query) }.flatten end |
#result ⇒ Object
Returns an array of entities. This array is the current page based on the position of the cursor (or the first page if no cursor is passed in).
18 19 20 21 22 23 24 25 |
# File 'lib/appengine-paginator/paginate.rb', line 18 def result if [:cursor].nil? @query.pquery.as_query_result_list(query.(:limit => limit)[1]) else java_cursor = [:cursor].instance_of?(Cursor) ? [:cursor].to_java : [:cursor] @query.pquery.as_query_result_list(query.(:limit => limit)[1].cursor(java_cursor)) end end |