inazuma 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/inazuma.gemspec +25 -0
- data/lib/inazuma.rb +9 -0
- data/lib/inazuma/config.rb +45 -0
- data/lib/inazuma/configuration_methods.rb +52 -0
- data/lib/inazuma/page_scope_methods.rb +70 -0
- data/lib/inazuma/paginatable_array.rb +58 -0
- data/lib/inazuma/version.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MTk3NjVjZDBhMmMzNWJkM2M3NDNkMjAwNmJjZDU5M2ZjZjYyMWJhNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTRiZDQyOThhMzJjMDRhZmIyYTk3YjdiZTNmZGY2NzIxM2JhMWI4Mg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmZhZmMxNzE1ODkzZjE0MjFhNWFiMDU3N2ZlODExNGYxYThlZWE1NmRlMThm
|
10
|
+
YzdjNDc0MWVkM2YyNDFhODFkNTcyYThjNzRkNDQ1MzYzYTZkY2YxMzcyYjZm
|
11
|
+
OTRmZDgwNTdhZDc1ZjVkZTE3NjRkNWVlYzVkMDRmMGU2ZDM2ZDg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjkzODI3NDZjNjgxNWUyMjFjZmE3Yjg3YTAxMGEyN2YwYTQ5ZmE2MWNhMTdh
|
14
|
+
MDZmMzZhMTdiNWZhYmQxZDQ2MjIyNmRkZjM1NjFlNzAxNTZmZDIwNmZhMGM4
|
15
|
+
NDI0NTgzZDcyZTQwYWYzODgyOTUwZDEwMjc0MDJlM2RiMWE2MjE=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Zachary Friedman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Inazuma
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'inazuma'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install inazuma
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/inazuma.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'inazuma/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "inazuma"
|
8
|
+
spec.version = Inazuma::VERSION
|
9
|
+
spec.authors = ["Zachary Friedman"]
|
10
|
+
spec.email = ["zafriedman@gmail.com"]
|
11
|
+
spec.description = %q{Lightweight, ORM and framework agnostic pagination library for Ruby}
|
12
|
+
spec.summary = %q{A simple pagination library for Ruby}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport", "~> 3.2.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/lib/inazuma.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module Inazuma
|
4
|
+
# Configures global settings for Inazuma
|
5
|
+
# Inazuma.configure do |config|
|
6
|
+
# config.default_per_page = 10
|
7
|
+
# end
|
8
|
+
def self.configure(&block)
|
9
|
+
yield @config ||= Inazuma::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Global settings for Inazuma
|
13
|
+
def self.config
|
14
|
+
@config
|
15
|
+
end
|
16
|
+
|
17
|
+
# need a Class for 3.0
|
18
|
+
class Configuration #:nodoc:
|
19
|
+
include ActiveSupport::Configurable
|
20
|
+
config_accessor :default_per_page
|
21
|
+
config_accessor :max_per_page
|
22
|
+
config_accessor :page_method_name
|
23
|
+
config_accessor :max_pages
|
24
|
+
config_accessor :add_link_headers
|
25
|
+
|
26
|
+
def param_name
|
27
|
+
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
28
|
+
end
|
29
|
+
|
30
|
+
# define param_name writer (copied from AS::Configurable)
|
31
|
+
writer, line = 'def param_name=(value); config.param_name = value; end', __LINE__
|
32
|
+
singleton_class.class_eval writer, __FILE__, line
|
33
|
+
class_eval writer, __FILE__, line
|
34
|
+
end
|
35
|
+
|
36
|
+
# this is ugly. why can't we pass the default value to config_accessor...?
|
37
|
+
configure do |config|
|
38
|
+
config.default_per_page = 25
|
39
|
+
config.max_per_page = nil
|
40
|
+
config.page_method_name = :page
|
41
|
+
config.param_name = :page
|
42
|
+
config.max_pages = nil
|
43
|
+
config.add_link_headers = false
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Inazuma
|
2
|
+
module ConfigurationMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module ClassMethods
|
5
|
+
# Overrides the default +per_page+ value per model
|
6
|
+
# class Article < ActiveRecord::Base
|
7
|
+
# paginates_per 10
|
8
|
+
# end
|
9
|
+
def paginates_per(val)
|
10
|
+
@_default_per_page = val
|
11
|
+
end
|
12
|
+
|
13
|
+
# This model's default +per_page+ value
|
14
|
+
# returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
|
15
|
+
def default_per_page
|
16
|
+
(defined?(@_default_per_page) && @_default_per_page) || Inazuma.config.default_per_page
|
17
|
+
end
|
18
|
+
|
19
|
+
# Overrides the max +per_page+ value per model
|
20
|
+
# class Article < ActiveRecord::Base
|
21
|
+
# max_paginates_per 100
|
22
|
+
# end
|
23
|
+
def max_paginates_per(val)
|
24
|
+
@_max_per_page = val
|
25
|
+
end
|
26
|
+
|
27
|
+
# This model's max +per_page+ value
|
28
|
+
# returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt>
|
29
|
+
def max_per_page
|
30
|
+
(defined?(@_max_per_page) && @_max_per_page) || Inazuma.config.max_per_page
|
31
|
+
end
|
32
|
+
|
33
|
+
# Overrides the max_pages value per model
|
34
|
+
# class Article < ActiveRecord::Base
|
35
|
+
# max_pages_per 100
|
36
|
+
# end
|
37
|
+
def max_pages_per(val)
|
38
|
+
@_max_pages = val
|
39
|
+
end
|
40
|
+
|
41
|
+
# This model's max_pages value
|
42
|
+
# returns max_pages value unless explicitly overridden via <tt>max_pages_per</tt>
|
43
|
+
def max_pages
|
44
|
+
(defined?(@_max_pages) && @_max_pages) || Inazuma.config.max_pages
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_link_headers?
|
48
|
+
Inazuma.config.add_link_headers
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Inazuma
|
2
|
+
module PageScopeMethods
|
3
|
+
# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
|
4
|
+
# Model.page(3).per(10)
|
5
|
+
def per(num)
|
6
|
+
if (n = num.to_i) <= 0
|
7
|
+
self
|
8
|
+
elsif max_per_page && max_per_page < n
|
9
|
+
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
|
10
|
+
else
|
11
|
+
limit(n).offset(offset_value / limit_value * n)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def padding(num)
|
16
|
+
@_padding = num
|
17
|
+
offset(offset_value + num.to_i)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Total number of pages
|
21
|
+
def total_pages
|
22
|
+
count_without_padding = total_count
|
23
|
+
count_without_padding -= @_padding if defined?(@_padding) && @_padding
|
24
|
+
count_without_padding = 0 if count_without_padding < 0
|
25
|
+
|
26
|
+
total_pages_count = (count_without_padding.to_f / limit_value).ceil
|
27
|
+
if max_pages.present? && max_pages < total_pages_count
|
28
|
+
max_pages
|
29
|
+
else
|
30
|
+
total_pages_count
|
31
|
+
end
|
32
|
+
end
|
33
|
+
#FIXME for compatibility. remove num_pages at some time in the future
|
34
|
+
alias num_pages total_pages
|
35
|
+
|
36
|
+
# Current page number
|
37
|
+
def current_page
|
38
|
+
offset_without_padding = offset_value
|
39
|
+
offset_without_padding -= @_padding if defined?(@_padding) && @_padding
|
40
|
+
offset_without_padding = 0 if offset_without_padding < 0
|
41
|
+
|
42
|
+
(offset_without_padding / limit_value) + 1
|
43
|
+
end
|
44
|
+
|
45
|
+
# Next page number in the collection
|
46
|
+
def next_page
|
47
|
+
current_page + 1 unless last_page?
|
48
|
+
end
|
49
|
+
|
50
|
+
# Previous page number in the collection
|
51
|
+
def prev_page
|
52
|
+
current_page - 1 unless first_page?
|
53
|
+
end
|
54
|
+
|
55
|
+
# First page of the collection?
|
56
|
+
def first_page?
|
57
|
+
current_page == 1
|
58
|
+
end
|
59
|
+
|
60
|
+
# Last page of the collection?
|
61
|
+
def last_page?
|
62
|
+
current_page >= total_pages
|
63
|
+
end
|
64
|
+
|
65
|
+
# Out of range of the collection?
|
66
|
+
def out_of_range?
|
67
|
+
current_page > total_pages
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_support/core_ext/module'
|
2
|
+
module Inazuma
|
3
|
+
# Kind of Array that can paginate
|
4
|
+
class PaginatableArray < Array
|
5
|
+
include Inazuma::ConfigurationMethods::ClassMethods
|
6
|
+
|
7
|
+
attr_internal_accessor :limit_value, :offset_value
|
8
|
+
|
9
|
+
# ==== Options
|
10
|
+
# * <tt>:limit</tt> - limit
|
11
|
+
# * <tt>:offset</tt> - offset
|
12
|
+
# * <tt>:total_count</tt> - total_count
|
13
|
+
def initialize(original_array = [], options = {})
|
14
|
+
@_original_array, @_limit_value, @_offset_value, @_total_count, @_padding = original_array, (options[:limit] || default_per_page).to_i, options[:offset].to_i, options[:total_count], options[:padding].to_i
|
15
|
+
|
16
|
+
if options[:limit] && options[:offset]
|
17
|
+
extend Inazuma::PageScopeMethods
|
18
|
+
end
|
19
|
+
|
20
|
+
if options[:total_count]
|
21
|
+
super original_array
|
22
|
+
else
|
23
|
+
super(original_array[@_offset_value, @_limit_value] || [])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# items at the specified "page"
|
28
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
29
|
+
def #{Inazuma.config.page_method_name}(num = 1)
|
30
|
+
offset(limit_value * ([num.to_i, 1].max - 1))
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
|
34
|
+
# returns another chunk of the original array
|
35
|
+
def limit(num)
|
36
|
+
self.class.new @_original_array, :limit => num, :offset => @_offset_value, :total_count => @_total_count, :padding => @_padding
|
37
|
+
end
|
38
|
+
|
39
|
+
# total item numbers of the original array
|
40
|
+
def total_count
|
41
|
+
@_total_count || @_original_array.count
|
42
|
+
end
|
43
|
+
|
44
|
+
# returns another chunk of the original array
|
45
|
+
def offset(num)
|
46
|
+
self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count, :padding => @_padding
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Wrap an Array object to make it paginatable
|
51
|
+
# ==== Options
|
52
|
+
# * <tt>:limit</tt> - limit
|
53
|
+
# * <tt>:offset</tt> - offset
|
54
|
+
# * <tt>:total_count</tt> - total_count
|
55
|
+
def self.paginate_array(array, options = {})
|
56
|
+
PaginatableArray.new array, options
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inazuma
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zachary Friedman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Lightweight, ORM and framework agnostic pagination library for Ruby
|
56
|
+
email:
|
57
|
+
- zafriedman@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- inazuma.gemspec
|
68
|
+
- lib/inazuma.rb
|
69
|
+
- lib/inazuma/config.rb
|
70
|
+
- lib/inazuma/configuration_methods.rb
|
71
|
+
- lib/inazuma/page_scope_methods.rb
|
72
|
+
- lib/inazuma/paginatable_array.rb
|
73
|
+
- lib/inazuma/version.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.1.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: A simple pagination library for Ruby
|
98
|
+
test_files: []
|