Class: Paginator::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-paginator/paginate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, *options)
  @query   = query
  @limit   = limit
  @options = options.first || {}
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



3
4
5
# File 'lib/appengine-paginator/paginate.rb', line 3

def limit
  @limit
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/appengine-paginator/paginate.rb', line 3

def options
  @options
end

#queryObject (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

#cursorObject

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_resultObject

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    = options[: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

#resultObject

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 options[:cursor].nil?
    @query.pquery.as_query_result_list(query.convert_options(:limit => limit)[1])
  else
    java_cursor = options[:cursor].instance_of?(Cursor) ? options[:cursor].to_java : options[:cursor]
    @query.pquery.as_query_result_list(query.convert_options(:limit => limit)[1].cursor(java_cursor))
  end
end