cursored 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ae0a3f8e6c4e971ccd13824fb7116260c94f02c
4
+ data.tar.gz: 0b5214af83c7b3d8079f97a064e9019dddf24975
5
+ SHA512:
6
+ metadata.gz: 2221a85991e56b949f7a2e6e893018c6e8ecc9394765a7e1c51b252decfdd7cef9e705f432d7fc91b795b8048f218a4af2b785f2005bf819c307cf87f8c2428d
7
+ data.tar.gz: 77c8a5380ad559c1cef20bc18db37cf0f2bdbbe151b62e8a6801b14a02cf5e83cb5cc9cb10c012ed21373165fae9af2c5d810ec59f669de8da7b75886db11e85
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cursored.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jason Kriss
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.
@@ -0,0 +1,39 @@
1
+ # Cursored
2
+
3
+ Super simple cursor-based pagination.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cursored'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cursored
18
+
19
+ Finally:
20
+
21
+ $ rails g cursored:install
22
+
23
+ ## Usage
24
+
25
+ Simply use `render_cursored` in your controller actions:
26
+
27
+ ```ruby
28
+ def index
29
+ render_cursored(Item.all)
30
+ end
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( http://github.com/jasonkriss/cursored/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cursored/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cursored"
8
+ spec.version = Cursored::VERSION
9
+ spec.authors = ["Jason Kriss"]
10
+ spec.email = ["jasonkriss@gmail.com"]
11
+ spec.summary = %q{Super simple cursor-based pagination.}
12
+ spec.homepage = "https://github.com/jasonkriss/cursored"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'cursored/version'
2
+ require 'cursored/configuration'
3
+ require 'cursored/controller'
4
+
5
+ module Cursored
6
+
7
+ end
@@ -0,0 +1,38 @@
1
+ module Cursored
2
+ class Collection
3
+ def initialize(relation, opts = {})
4
+ @relation = relation
5
+ @limit = opts.fetch(:limit, Cursored.config.limit)
6
+ @cursor = opts[:cursor]
7
+ end
8
+
9
+ def records
10
+ padded_records[0..(limit - 1)]
11
+ end
12
+
13
+ def next
14
+ records.last.send(Cursored.config.field) if has_more?
15
+ end
16
+
17
+ private
18
+ attr_reader :relation, :limit, :cursor
19
+
20
+ def padded_limit
21
+ @padded_limit ||= limit + 1
22
+ end
23
+
24
+ def padded_records
25
+ @padded_records ||= fetch_padded_records
26
+ end
27
+
28
+ def fetch_padded_records
29
+ field = Cursored.config.field
30
+ padded = relation.order(field => :desc).limit(padded_limit)
31
+ cursor ? padded.where("#{relation.table_name}.#{field} < ?", cursor) : padded
32
+ end
33
+
34
+ def has_more?
35
+ padded_records.length == padded_limit
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ module Cursored
2
+ def self.configure
3
+ yield config
4
+ end
5
+
6
+ def self.config
7
+ @config ||= Configuration.new
8
+ end
9
+
10
+ class Configuration
11
+ attr_accessor :field, :limit
12
+
13
+ def initialize
14
+ @field = :id
15
+ @limit = 25
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'cursored/collection'
2
+
3
+ module Cursored
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+ def render_cursored(relation, opts = {})
9
+ options = opts.merge(params.slice(:cursor))
10
+ collection = Cursored::Collection.new(relation, options)
11
+
12
+ render({
13
+ status: :ok,
14
+ json: collection.records,
15
+ meta: {
16
+ cursor: collection.next
17
+ }
18
+ })
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Cursored
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ module Cursored
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_cursored_initializer
7
+ copy_file 'cursored.rb', 'config/initializers/cursored.rb'
8
+ end
9
+
10
+ def inject_cursored_into_application_controller
11
+ inject_into_class "app/controllers/application_controller.rb", ApplicationController do
12
+ " include Cursored::Controller\n"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ Cursored.configure do |config|
2
+ config.field = :id
3
+ config.limit = 25
4
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cursored
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kriss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - jasonkriss@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - cursored.gemspec
54
+ - lib/cursored.rb
55
+ - lib/cursored/collection.rb
56
+ - lib/cursored/configuration.rb
57
+ - lib/cursored/controller.rb
58
+ - lib/cursored/version.rb
59
+ - lib/generators/cursored/install/install_generator.rb
60
+ - lib/generators/cursored/install/templates/cursored.rb
61
+ homepage: https://github.com/jasonkriss/cursored
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Super simple cursor-based pagination.
85
+ test_files: []