Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 71e6d48ad06c353f545f2710d2c6386945c04f0d @tj committed Oct 5, 2009
Showing with 194 additions and 0 deletions.
  1. +5 −0 History.md
  2. +16 −0 Rakefile
  3. +29 −0 Readme.md
  4. +62 −0 lib/dm-is-paginated.rb
  5. +24 −0 spec/pager_spec.rb
  6. +2 −0 spec/spec.opts
  7. +15 −0 spec/spec_helper.rb
  8. +13 −0 tasks/docs.rake
  9. +3 −0 tasks/gemspec.rake
  10. +25 −0 tasks/spec.rake
@@ -0,0 +1,5 @@
+
+0.0.1 / YYYY-MM-DD
+==================
+
+ * Initial release
@@ -0,0 +1,16 @@
+
+$:.unshift 'lib'
+require 'GEM_NAME'
+require 'rubygems'
+require 'rake'
+require 'echoe'
+
+Echoe.new "GEM_NAME", GEM_CLASS::VERSION do |p|
+ p.author = "GEM_AUTHOR"
+ p.email = "GEM_EMAIL"
+ p.summary = "GEM_DESCRIPTION"
+ p.url = "GEM_URL"
+ p.runtime_dependencies = []
+end
+
+Dir['tasks/**/*.rake'].sort.each { |f| load f }
@@ -0,0 +1,29 @@
+
+# DataMapper::Is::Paginated
+
+ Better paging solution for DataMapper.
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, an d/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
+#--
+# Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#++
+
+module DataMapper
+ module Is
+ module Paginated
+
+ ##
+ # Plugin API.
+
+ def is_paginated options = {}
+ extend ClassMethods
+ end
+
+ module ClassMethods
+
+ ##
+ # Page collection by _current_page_ number and _options_ provided.
+ #
+ # === Options
+ #
+ # :per_page Results per page; defaults to 6
+ # :order Defaults to [:id.desc]
+ #
+ # === Examples
+ #
+ # code
+ #
+
+ def page current_page = 1, options = {}
+ new_collection scoped_query({
+ :limit => per_page = (options.delete(:per_page) || 6),
+ :offset => (current_page - 1) * per_page,
+ :order => [:id.desc]
+ }.merge(options))
+ end
+ end
+ end
+ end
+end
+
+DataMapper::Model.append_extensions DataMapper::Is::Paginated
@@ -0,0 +1,24 @@
+
+# User.all.page(5, :per_page => 2)
+
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe DataMapper::Is::Paginated do
+ before :each do
+ 1.upto 20 do |n|
+ instance_variable_set :"@item_#{n}", Item.create
+ end
+ end
+
+ def items from, to
+ (from..to).map do |n|
+ instance_variable_get :"@item_#{n}"
+ end
+ end
+
+ describe "#page" do
+ it "should default page to 1, :per_page to 6, and :order to :id.desc" do
+ Item.all.page.should == items(1, 6).reverse
+ end
+ end
+end
@@ -0,0 +1,2 @@
+--color
+--format specdoc
@@ -0,0 +1,15 @@
+
+$:.unshift 'lib'
+require 'rubygems'
+require 'dm-core'
+require 'dm-is-paginated'
+
+DataMapper.setup :default, 'sqlite3::memory:'
+
+class Item
+ include DataMapper::Resource
+ is :paginated
+ property :id, Serial
+end
+
+DataMapper.auto_migrate!
@@ -0,0 +1,13 @@
+
+namespace :docs do
+
+ desc 'Remove rdoc products'
+ task :remove => [:clobber_docs]
+
+ desc 'Build docs, and open in browser for viewing (specify BROWSER)'
+ task :open do
+ browser = ENV["BROWSER"] || "safari"
+ sh "open -a #{browser} doc/index.html"
+ end
+
+end
@@ -0,0 +1,3 @@
+
+desc 'Build gemspec file'
+task :gemspec => [:build_gemspec]
@@ -0,0 +1,25 @@
+
+require 'spec/rake/spectask'
+
+desc "Run all specifications"
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.libs << "lib"
+ t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
+end
+
+namespace :spec do
+
+ desc "Run all specifications verbosely"
+ Spec::Rake::SpecTask.new(:verbose) do |t|
+ t.libs << "lib"
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
+ end
+
+ desc "Run specific specification verbosely (specify SPEC)"
+ Spec::Rake::SpecTask.new(:select) do |t|
+ t.libs << "lib"
+ t.spec_files = [ENV["SPEC"]]
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
+ end
+
+end

0 comments on commit 71e6d48

Please sign in to comment.