jsonapi-resources-optional_paginators 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d01c0519bdca1e9ae06c94b54f5a2fb24558f521363892e69eb9f1e8a7e3a6a1
4
+ data.tar.gz: 47aa97a7a99c7cdbaad20152ce743e1e857a78761d47e792998fa62854cf059c
5
+ SHA512:
6
+ metadata.gz: bf673b3a49af1a409ace776beb1412b4bb8b333e262e7421fadfb2566e10439da6bb2fc311109e8e130eee2ff12601f7f1a18ae245caeb6fb7aff1d47ebf8766
7
+ data.tar.gz: a73732cb519796d6cd3f2e494e3ae3a72e1cffe05999e08d9b79d2c201b72fe3d8bdfae3a1575fbd5b1d55f2e5d046f604ac8d22ceed243f5fd0e0ec4e908dfb
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonapi-resources-optional_paginators.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Josh Justice
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # JSONAPI::Resources::OptionalPaginators
2
+
3
+ JSONAPI::Resources paginators that allow returning records unpaginated.
4
+
5
+ JSONAPI::Resources allows you to configure resources to either have no pagination, or to have a paginator of a certain type. This often works fine.
6
+
7
+ But sometimes you may want the option to either paginate or not paginate a resource, depending on the request. For example, in a todo list app, your completed todo list will get longer over time, so you might want to paginate it. But your uncompleted todo list should stay short, so you might want to retrieve all incomplete todos unpaginated.
8
+
9
+ This gem allows you to make JR paginators optional, so that if you don't pass any `page[]` parameters, all records are returned. This is accomplished with an `OptionalPaginator` class that wraps any other paginator. For convenience, a wrapped version of the built-in `PagedPaginator` is provided as `OptionalPagedPaginator`.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'jsonapi-resources-optional_paginators'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install jsonapi-resources-optional_paginators
26
+
27
+ ## Usage
28
+
29
+ To use the `OptionalPagedPaginator`, configure it as the paginator for a resource or the default paginator for the app:
30
+
31
+ JSONAPI.configure do |config|
32
+ config.default_paginator = :optional_paged
33
+ end
34
+
35
+ To make another paginator class optional, call the `OptionalPaginator.for` method, passing the paginator class to wrap. It returns a new class, so assign it to a constant:
36
+
37
+ OptionalOffsetPaginator =
38
+ JSONAPI::Resources::OptionalPaginators::OptionalPaginator.for(OffsetPaginator)
39
+
40
+ Note that the class should not be nested inside a module, so that JR can find the class name based on the symbol passed to the config:
41
+
42
+ JSONAPI.configure do |config|
43
+ config.default_paginator = :optional_offset
44
+ end
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codingitwrong/jsonapi-resources-optional_paginators.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonapi/resources/optional_paginators"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/jsonapi/resources/optional_paginators/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'jsonapi-resources-optional_paginators'
5
+ spec.version = JSONAPI::Resources::OptionalPaginators::VERSION
6
+ spec.authors = ['Josh Justice']
7
+ spec.email = ['me@codingitwrong.com']
8
+
9
+ spec.summary = 'JR paginators that allow returning records unpaginated.'
10
+ spec.homepage = 'https://github.com/CodingItWrong/jsonapi-resources-optional_paginators'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+
14
+ spec.metadata['homepage_uri'] = spec.homepage
15
+ spec.metadata['source_code_uri'] = 'https://github.com/CodingItWrong/jsonapi-resources-optional_paginators.git'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'jsonapi-resources', ['>= 0']
27
+ end
@@ -0,0 +1,14 @@
1
+ require 'jsonapi/resources/optional_paginators/version'
2
+
3
+ require_relative 'optional_paginators/null_paginator'
4
+ require_relative 'optional_paginators/optional_paginator'
5
+ require_relative '../../optional_paged_paginator'
6
+
7
+ module JSONAPI
8
+ module Resources
9
+ module OptionalPaginators
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+ module Resources
5
+ module OptionalPaginators
6
+ class NullPaginator < JSONAPI::Paginator
7
+ def initialize(params); end
8
+
9
+ def apply(relation, _order_options)
10
+ relation
11
+ end
12
+
13
+ def calculate_page_count(_record_count)
14
+ 1
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'null_paginator'
4
+
5
+ module JSONAPI
6
+ module Resources
7
+ module OptionalPaginators
8
+ class OptionalPaginator < SimpleDelegator
9
+ class << self
10
+ attr_accessor :wrapped_class
11
+
12
+ def for(paginator_class_argument)
13
+ Class.new(self).tap do |wrapper_class|
14
+ wrapper_class.wrapped_class = paginator_class_argument
15
+ end
16
+ end
17
+ end
18
+
19
+ attr_reader :delegate
20
+
21
+ def self.requires_record_count
22
+ wrapped_class.requires_record_count
23
+ end
24
+
25
+ def initialize(params)
26
+ inner_paginator = paginator_for_params(params).new(params)
27
+ super(inner_paginator)
28
+ end
29
+
30
+ def paginator_for_params(params)
31
+ if params.nil?
32
+ NullPaginator
33
+ else
34
+ self.class.wrapped_class
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ module JSONAPI
2
+ module Resources
3
+ module OptionalPaginators
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ OptionalPagedPaginator =
4
+ JSONAPI::Resources::OptionalPaginators::OptionalPaginator.for(PagedPaginator)
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi-resources-optional_paginators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Justice
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jsonapi-resources
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - me@codingitwrong.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - jsonapi-resources-optional_paginators.gemspec
44
+ - lib/jsonapi/resources/optional_paginators.rb
45
+ - lib/jsonapi/resources/optional_paginators/null_paginator.rb
46
+ - lib/jsonapi/resources/optional_paginators/optional_paginator.rb
47
+ - lib/jsonapi/resources/optional_paginators/version.rb
48
+ - lib/optional_paged_paginator.rb
49
+ homepage: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators
54
+ source_code_uri: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators.git
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.3.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: JR paginators that allow returning records unpaginated.
74
+ test_files: []