appengine-paginator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,26 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ .gems
17
+ WEB-INF/lib
18
+ WEB-INF/appengine-generated
19
+ config.ru
20
+
21
+ ## PROJECT::GENERAL
22
+ coverage
23
+ rdoc
24
+ pkg
25
+
26
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # Critical default settings:
2
+ disable_system_gems
3
+ disable_rubygems
4
+ bundle_path ".gems/bundler_gems"
5
+
6
+ # List gems to bundle here:
7
+ gem 'rspec'
8
+ gem 'dm-appengine'
9
+ gem 'dm-core'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Josh Moore
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ = appengine-paginator
2
+
3
+ This is a gem that will help you to get pagination working on the Google AppEngine. It uses the AppEngien Cursor so currently you can only get the next cursor, you cannot go in reverse and get previous records. In order to do that you would need to have two queries.
4
+
5
+ == Installation
6
+
7
+ gem install appengine-paginator
8
+
9
+ then
10
+
11
+ require 'appengine-paginator'
12
+
13
+ == Usage
14
+
15
+ Simply create a paginate object by passing it an AppEngine::Datastore::Query object, the limit, then an optional model and option cursor.
16
+
17
+ i.e. p = Paginate.new(query, 10, :model => Post, :cursor => c)
18
+
19
+ The query and the limit are pretty self explanitory. The :model is an optional datamapper model that is used if you want the paginate object to return active record models. The cursor is what tells the query where to start, thus giving it pagination.
20
+
21
+ Once the paginate object is created you can access the results by either calling result or datamapper_result. The result method returns the raw entities returned by the query. The datastore_result method returns an array of Datamapper models that are instiances of the the model class (the class you passed in at creating time). If a cursor is passed in then the result set will start with the record indicated by the cursor (i.e. if you passed in a limit of 10 and then created the cursor it would point to the 11th search result).
22
+
23
+ In order to get the cursor simply call cursor on the paginate object. This will return an instance of Cursor (a wrapper for com.google.appengine.api.datastore.Cursor) which points to the first row after the given limit. Simple call to_s on the cursor object to get a string representation that can be stored in a session, the datastore, or on the rendered HTML page. Calling Cursor.parse <string_representation_of_cursor> will convert the cursor string back into a cursor object that can be passed into a new Paginate object.
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so I don't break it in a
30
+ future version unintentionally.
31
+ * Commit, do not mess with rakefile, version, or history.
32
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
33
+ * Send me a pull request. Bonus points for topic branches.
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2010 Josh Moore. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "appengine-paginator"
8
+ gem.summary = %Q{A gem to assist in pagination for the Google AppEngine}
9
+ gem.description = %Q{Pagination on JRuby running on the Google AppEngine}
10
+ gem.email = "josh@codingforrent.com"
11
+ gem.homepage = "http://github.com/joshsmoore@gmail.com/appengine-paginator"
12
+ gem.authors = ["Josh Moore"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "appengine-paginator #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ public_root: public
@@ -0,0 +1,3 @@
1
+ #!/Users/jmoore/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/environment"))
3
+ load File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/gems/rspec-1.3.0/bin/autospec"))
@@ -0,0 +1,3 @@
1
+ #!/Users/jmoore/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/environment"))
3
+ load File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/gems/rack-1.2.1/bin/rackup"))
@@ -0,0 +1,3 @@
1
+ #!/Users/jmoore/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/environment"))
3
+ load File.expand_path(File.join(File.dirname(__FILE__), "../.gems/bundler_gems/jruby/1.8/gems/rspec-1.3.0/bin/spec"))
@@ -0,0 +1,3 @@
1
+ require 'paginate'
2
+ require 'cursor'
3
+
@@ -0,0 +1,19 @@
1
+ class Cursor
2
+
3
+ def initialize(cursor)
4
+ @_cursor = cursor
5
+ end
6
+
7
+ def to_s
8
+ @_cursor.to_web_safe_string
9
+ end
10
+
11
+ def self.parse(cursor_string)
12
+ Cursor.new Java::comGoogleAppengineApiDatastore::Cursor.from_web_safe_string(cursor_string)
13
+ end
14
+
15
+
16
+ def to_java
17
+ @_cursor
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ class Paginate
2
+ attr_reader :query, :limit, :options
3
+ def initialize(query, limit, *options)
4
+ @query = query
5
+ @limit = limit
6
+ @options = options.first || {}
7
+ end
8
+
9
+ def result
10
+ if options[:cursor].nil?
11
+ @query.pquery.as_query_result_list(query.convert_options(:limit => limit)[1])
12
+ else
13
+ java_cursor = options[:cursor].instance_of?(Cursor) ? options[:cursor].to_java : options[:cursor]
14
+ @query.pquery.as_query_result_list(query.convert_options(:limit => limit)[1].cursor(java_cursor))
15
+ end
16
+ end
17
+
18
+ def datamapper_result
19
+ model = options[:model]
20
+ dm_query =Struct.new(:repository, :fields, :reload?).new(DataMapper::repository, model.properties, false)
21
+ result.collect {|e| model.load([Paginate.entity_to_hash(e, model.properties)], dm_query)}.flatten
22
+ end
23
+
24
+ def cursor
25
+ Cursor.new result.cursor
26
+ end
27
+
28
+ #This should be moved to the Adapter
29
+ def self.entity_to_hash(entity, properties)
30
+ return if entity.nil?
31
+ key = entity.get_key
32
+ hash = {}
33
+ properties.each do |property|
34
+ name = property.field
35
+ if property.key?
36
+ hash[name] = if property.kind_of? DataMapper::Property::Key
37
+ key
38
+ elsif property.serial?
39
+ key.get_id
40
+ elsif property.kind_of? DataMapper::Property::String
41
+ key.get_name
42
+ else
43
+ key
44
+ end
45
+ else
46
+ value = entity.get_property(name)
47
+ if property.kind_of?(DataMapper::Property::Decimal) && value
48
+ value = Lexidecimal.string_to_decimal(entity.get_property(name))
49
+ end
50
+ hash[name] = value
51
+ end
52
+ end
53
+ hash
54
+ end
55
+ end
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AppenginePaginator" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Cursor do
4
+
5
+ before(:each) do
6
+ @cursor_string = 'E9oBJGofagR0ZXN0chcLEggKABoEUG9zdAwLEgRQb3N0GMEMDIIBAOABABQ'
7
+ java_cursor = Java::ComGoogleAppengineApiDatastore::Cursor.from_web_safe_string(@cursor_string)
8
+ @cursor = Cursor.new(java_cursor)
9
+ end
10
+
11
+ describe '#to_s' do
12
+ it 'should return the web safe string' do
13
+ @cursor.to_s.should == 'E9oBJGofagR0ZXN0chcLEggKABoEUG9zdAwLEgRQb3N0GMEMDIIBAOABABQ'
14
+ end
15
+ end
16
+
17
+ describe '.parse' do
18
+ it 'should return an object of class Cursor' do
19
+ Cursor.parse(@cursor_string).should be_instance_of Cursor
20
+ end
21
+ end
22
+
23
+
24
+ describe '#.to_java' do
25
+ it 'should return the java cursor' do
26
+ @cursor.to_java.should be_instance_of Java::ComGoogleAppengineApiDatastore::Cursor
27
+ end
28
+ end
29
+ end
30
+
31
+
@@ -0,0 +1,80 @@
1
+ require 'spec/spec_helper'
2
+
3
+ require 'spec/support/post'
4
+
5
+ describe Paginate do
6
+ before(:all) do
7
+ Post.create(:title => "t1")
8
+ Post.create(:title => "t2")
9
+ Post.create(:title => "t3")
10
+ Post.create(:title => "t4")
11
+ Post.create(:title => "t5")
12
+ Post.create(:title => "t6")
13
+ end
14
+
15
+ after(:all) do
16
+ Post.destroy
17
+ end
18
+ describe '.initialize' do
19
+ it 'should create an instance of the Paginate class and take two required pareters' do
20
+ Paginate.new(1,2).should be_instance_of Paginate
21
+ end
22
+
23
+ it 'should accept more parameters as a hash' do
24
+ Paginate.new(1,2,:cursor => 4, :model => 4).should be_instance_of Paginate
25
+ end
26
+ end
27
+
28
+ describe '#result' do
29
+ it 'should return the results of the query' do
30
+ paginate = Paginate.new(AppEngine::Datastore::Query.new('Posts'), 7)
31
+ paginate.result.count.should == 6
32
+ end
33
+
34
+ it 'should limit the results based on the limit value' do
35
+ paginate = Paginate.new(AppEngine::Datastore::Query.new('Posts'), 2)
36
+ paginate.result.count.should == 2
37
+ end
38
+
39
+ it 'should start at the cursor when a cursor is provided' do
40
+ p = Paginate.new(AppEngine::Datastore::Query.new('Posts'), 2)
41
+ paginate = Paginate.new(AppEngine::Datastore::Query.new('Posts'), 2, :cursor => p.cursor)
42
+
43
+ paginate.result.first[:title].should == 't3'
44
+ end
45
+ end
46
+
47
+ describe '#datamapper_result' do
48
+ before(:each) do
49
+ @paginate = Paginate.new(AppEngine::Datastore::Query.new('Posts'), 2, :model => Post)
50
+ end
51
+
52
+ it 'should call the result method' do
53
+ @paginate.should_receive(:result).once.and_return([])
54
+ @paginate.datamapper_result
55
+ end
56
+
57
+ it 'should return an array' do
58
+ @paginate.datamapper_result.should be_instance_of Array
59
+ end
60
+
61
+ it 'should return the same number of items as the result method' do
62
+ @paginate.datamapper_result.size.should == @paginate.result.count
63
+ end
64
+
65
+ it 'each item should be an instance of the passed in model' do
66
+ @paginate.datamapper_result.each {|e| e.should be_instance_of Post}
67
+ end
68
+ end
69
+
70
+ describe '#cursor' do
71
+ it 'should return the cursor' do
72
+ paginate = Paginate.new(AppEngine::Datastore::Query.new('Post'), 2, :model => Post)
73
+ paginate.should respond_to :cursor
74
+ end
75
+ it 'should return the java cursor' do
76
+ paginate = Paginate.new(AppEngine::Datastore::Query.new('Post'), 2, :model => Post)
77
+ paginate.cursor.should be_instance_of Cursor
78
+ end
79
+ end
80
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'appengine-paginator'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'dm-core'
7
+ require 'dm-appengine-adapter'
8
+
9
+ DataMapper.setup(:default, "app_engine://memory")
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
@@ -0,0 +1,6 @@
1
+ class Post
2
+ include DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :title, String
6
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ version = ">= 0"
5
+
6
+ if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
7
+ version = $1
8
+ ARGV.shift
9
+ end
10
+
11
+ #gem 'rspec', version
12
+
13
+ rspec_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(rspec_dir) unless $LOAD_PATH.include?(rspec_dir)
15
+ require 'spec/autorun'
16
+ exit ::Spec::Runner::CommandLine.run
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: appengine-paginator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Josh Moore
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-09 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Pagination on JRuby running on the Google AppEngine
38
+ email: josh@codingforrent.com
39
+ executables:
40
+ - autospec
41
+ - rackup
42
+ - spec
43
+ extensions: []
44
+
45
+ extra_rdoc_files:
46
+ - LICENSE
47
+ - README.rdoc
48
+ files:
49
+ - .document
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.rdoc
54
+ - Rakefile
55
+ - VERSION
56
+ - WEB-INF/app.yaml
57
+ - bin/autospec
58
+ - bin/rackup
59
+ - bin/spec
60
+ - config.ru
61
+ - lib/appengine-paginator.rb
62
+ - lib/cursor.rb
63
+ - lib/paginate.rb
64
+ - public/favicon.ico
65
+ - public/robots.txt
66
+ - spec/appengine-paginator_spec.rb
67
+ - spec/cursor_spec.rb
68
+ - spec/paginate_spec.rb
69
+ - spec/spec.opts
70
+ - spec/spec_helper.rb
71
+ - spec/support/post.rb
72
+ - spec_runner
73
+ has_rdoc: true
74
+ homepage: http://github.com/joshsmoore@gmail.com/appengine-paginator
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A gem to assist in pagination for the Google AppEngine
107
+ test_files:
108
+ - spec/appengine-paginator_spec.rb
109
+ - spec/cursor_spec.rb
110
+ - spec/paginate_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/support/post.rb