tiny_filter 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -3
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +1 -1
- data/README.md +14 -11
- data/Rakefile +0 -9
- data/lib/generators/tiny_filter/filter/filter_generator.rb +3 -2
- data/lib/generators/tiny_filter/install/USAGE +1 -1
- data/lib/generators/tiny_filter/install/install_generator.rb +3 -2
- data/lib/tiny_filter/base.rb +9 -7
- data/lib/tiny_filter/filter_finder.rb +0 -3
- data/lib/tiny_filter/version.rb +1 -1
- data/lib/tiny_filter.rb +0 -1
- data/tiny_filter.gemspec +5 -5
- metadata +7 -26
- /data/lib/generators/tiny_filter/filter/templates/{filter.rb.erb → filter.rb.tt} +0 -0
- /data/lib/generators/tiny_filter/install/templates/{application_filter.rb → application_filter.rb.tt} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cf60cc543019e4d02a3c8ffda90cd0e27d224feefdd0517f1b359c9e598b7f2
|
4
|
+
data.tar.gz: fc7bceb2b9ec9265d4535fcacbf65c207e2df6b689b3d3117a839ccffdba6524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1537075c3b79a1e3ab09080f7d9e68edb43e22dd97881782000d67d677cfdd543c118e445425f0bf729d2680ec8b8d35db8fb064d911f36b6727388ebcde957
|
7
|
+
data.tar.gz: 3b49cb06eca991359f8bf91500fb9f0ff20fcb36f503a25bd7011b239b9258411178844906e8ae424e4e7ab16720875c0c0f430161fb82f6640b35b4704c7146
|
data/.rubocop.yml
CHANGED
@@ -3,6 +3,7 @@ require:
|
|
3
3
|
- rubocop-rails
|
4
4
|
- rubocop-rake
|
5
5
|
- rubocop-rspec
|
6
|
+
- rubocop-factory_bot
|
6
7
|
|
7
8
|
inherit_gem:
|
8
9
|
rubocop-shopify: rubocop.yml
|
@@ -17,9 +18,6 @@ Layout/EmptyLinesAroundAccessModifier:
|
|
17
18
|
Layout/IndentationConsistency:
|
18
19
|
EnforcedStyle: normal
|
19
20
|
|
20
|
-
Layout/EmptyLinesAroundClassBody:
|
21
|
-
EnforcedStyle: empty_lines_special
|
22
|
-
|
23
21
|
Style/SymbolArray:
|
24
22
|
EnforcedStyle: percent
|
25
23
|
|
data/CHANGELOG.md
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -39,7 +39,7 @@ This Code of Conduct applies within all community spaces, and also applies when
|
|
39
39
|
|
40
40
|
## Enforcement
|
41
41
|
|
42
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kirill
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kirill@lassoid.ru. All complaints will be reviewed and investigated promptly and fairly.
|
43
43
|
|
44
44
|
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
45
|
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# TinyFilter
|
2
2
|
|
3
|
-
[![Gem Version](https://
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/tiny_filter?color=blue&label=version)](https://rubygems.org/gems/tiny_filter)
|
4
|
+
[![Gem downloads count](https://img.shields.io/gem/dt/tiny_filter)](https://rubygems.org/gems/tiny_filter)
|
5
|
+
[![Github Actions CI](https://github.com/lassoid/tiny_filter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/lassoid/tiny_filter/actions/workflows/ci.yml)
|
4
6
|
|
5
7
|
TinyFilter is created to provide a simple object-oriented abstraction layer for filtering collections.
|
6
8
|
It is mainly purposed for ActiveRecord collections, but you can also use it with any enumerable.
|
@@ -42,11 +44,11 @@ Each filter is defined by calling `filters` method inside class body.
|
|
42
44
|
|
43
45
|
`filters` accepts two arguments:
|
44
46
|
- `key` - a filter name, used as identifier;
|
45
|
-
- `block` - a block
|
47
|
+
- `block` - a block with filter logic, that returns filtered collection and itself accepts two arguments:
|
46
48
|
- `scope` - a collection that should be filtered;
|
47
49
|
- `value` - a value for filtering.
|
48
50
|
|
49
|
-
When you perform filtering, provided
|
51
|
+
When you perform filtering, provided key indicate filter `key` and provided value is passed to `value` param in corresponding filter `block`.
|
50
52
|
`scope`s receive collections in a pipeline manner:
|
51
53
|
_first_ executed filter receives _original collection_,
|
52
54
|
_second and further_ receive the _return collection_ of the previous filter.
|
@@ -60,7 +62,7 @@ class UserFilter < ApplicationFilter
|
|
60
62
|
end
|
61
63
|
|
62
64
|
UserFilter.filter(User, name: "John", surname: "Doe")
|
63
|
-
#
|
65
|
+
# Which is equivalent to:
|
64
66
|
# User.where(first_name: "John").where(last_name: "Doe")
|
65
67
|
```
|
66
68
|
|
@@ -70,14 +72,14 @@ It guarantees that scope behaves the same way as in other filters in this class.
|
|
70
72
|
```ruby
|
71
73
|
filters(:title) { |scope, value| scope.where("title ILIKE ?", value) }
|
72
74
|
|
73
|
-
# bad - scope is
|
75
|
+
# bad - scope is an ActiveRecord collection, but the return value is an array.
|
74
76
|
filters(:from) { |scope, value| scope.select { |e| e.created_at >= value } }
|
75
77
|
|
76
78
|
# good - scope and return value are both ActiveRecord collections.
|
77
79
|
filters(:from) { |scope, value| scope.where("created_at >= ?", value) }
|
78
80
|
```
|
79
81
|
|
80
|
-
|
82
|
+
Thus if the initial scope for filtering is an ActiveRecord collection,
|
81
83
|
it is a bad practice for filter to return not an ActiveRecord collection.
|
82
84
|
Otherwise you can face errors depending on the provided options order.
|
83
85
|
|
@@ -119,7 +121,7 @@ class My::Class < ApplicationRecord
|
|
119
121
|
end
|
120
122
|
```
|
121
123
|
|
122
|
-
|
124
|
+
## Using with Plain objects
|
123
125
|
|
124
126
|
You can use filters with non-ActiveRecord collections like so:
|
125
127
|
|
@@ -132,11 +134,12 @@ MyFilter.filter(collection, options)
|
|
132
134
|
|
133
135
|
## Development
|
134
136
|
|
135
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
136
|
-
You can also run `bin/
|
137
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests.
|
138
|
+
You can also run `bin/rubocop` to lint the source code
|
139
|
+
and `bin/console` for an interactive prompt that will allow you to experiment.
|
137
140
|
|
138
|
-
To install this gem onto your local machine, run `
|
139
|
-
To release a new version, update the version number in `version.rb`, and then run `
|
141
|
+
To install this gem onto your local machine, run `bin/rake install`.
|
142
|
+
To release a new version, update the version number in `version.rb`, and then run `bin/rake release`,
|
140
143
|
which will create a git tag for the version, push git commits and the created tag,
|
141
144
|
and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
142
145
|
|
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "rails/generators"
|
4
|
+
|
3
5
|
module TinyFilter
|
4
6
|
module Generators
|
5
7
|
class FilterGenerator < ::Rails::Generators::NamedBase
|
@@ -11,7 +13,7 @@ module TinyFilter
|
|
11
13
|
|
12
14
|
def create_filter
|
13
15
|
template_file = File.join("app/filters", class_path, filter_file_name)
|
14
|
-
template "filter.rb.
|
16
|
+
template "filter.rb.tt", template_file
|
15
17
|
end
|
16
18
|
|
17
19
|
private
|
@@ -19,7 +21,6 @@ module TinyFilter
|
|
19
21
|
def filter_file_name
|
20
22
|
"#{file_name}_filter.rb"
|
21
23
|
end
|
22
|
-
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "rails/generators"
|
4
|
+
|
3
5
|
module TinyFilter
|
4
6
|
module Generators
|
5
7
|
class InstallGenerator < ::Rails::Generators::Base
|
@@ -8,9 +10,8 @@ module TinyFilter
|
|
8
10
|
desc "This generator creates an application filter"
|
9
11
|
|
10
12
|
def copy_application_filter
|
11
|
-
template "application_filter.rb", "app/filters/application_filter.rb"
|
13
|
+
template "application_filter.rb.tt", "app/filters/application_filter.rb"
|
12
14
|
end
|
13
|
-
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/tiny_filter/base.rb
CHANGED
@@ -3,12 +3,14 @@
|
|
3
3
|
module TinyFilter
|
4
4
|
class Base
|
5
5
|
class << self
|
6
|
+
def inherited(subclass)
|
7
|
+
super
|
8
|
+
dup_filters = __filters__.dup
|
9
|
+
subclass.__filters__ = dup_filters.each { |key, value| dup_filters[key] = value.dup }
|
10
|
+
end
|
6
11
|
|
7
12
|
def filters(key, &block)
|
8
|
-
key =
|
9
|
-
raise AlreadyDefinedError, "filter :#{key} defined more than once in #{self}" if __filters__.key?(key)
|
10
|
-
|
11
|
-
__filters__[key] = block
|
13
|
+
__filters__[key.to_sym] = block
|
12
14
|
end
|
13
15
|
|
14
16
|
def filter(base_scope, args = {})
|
@@ -20,13 +22,13 @@ module TinyFilter
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
|
-
|
25
|
+
protected
|
26
|
+
|
27
|
+
attr_writer :__filters__
|
24
28
|
|
25
29
|
def __filters__
|
26
30
|
@__filters__ ||= {}
|
27
31
|
end
|
28
|
-
|
29
32
|
end
|
30
|
-
|
31
33
|
end
|
32
34
|
end
|
@@ -5,7 +5,6 @@ module TinyFilter
|
|
5
5
|
SUFFIX = "Filter"
|
6
6
|
|
7
7
|
class << self
|
8
|
-
|
9
8
|
def find(object)
|
10
9
|
filter_class(object)
|
11
10
|
end
|
@@ -21,8 +20,6 @@ module TinyFilter
|
|
21
20
|
raise Error, "unable to find appropriate filter class for #{object}"
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
25
23
|
end
|
26
|
-
|
27
24
|
end
|
28
25
|
end
|
data/lib/tiny_filter/version.rb
CHANGED
data/lib/tiny_filter.rb
CHANGED
data/tiny_filter.gemspec
CHANGED
@@ -6,10 +6,10 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.name = "tiny_filter"
|
7
7
|
spec.version = TinyFilter::VERSION
|
8
8
|
spec.authors = ["Kirill Usanov", "LassoID"]
|
9
|
-
spec.email =
|
9
|
+
spec.email = "kirill@lassoid.ru"
|
10
10
|
|
11
11
|
spec.summary = "Tiny filtering for Rails."
|
12
|
-
spec.description = "Simple filtering for
|
12
|
+
spec.description = "Simple filtering for ActiveRecord and enumerables."
|
13
13
|
spec.homepage = "https://github.com/lassoid/tiny_filter"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
@@ -30,9 +30,9 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.require_paths = ["lib"]
|
31
31
|
|
32
32
|
# Dependencies
|
33
|
-
spec.add_dependency "activesupport", ">= 6.0"
|
34
|
-
spec.add_development_dependency "activerecord", ">= 6.0"
|
35
|
-
spec.add_development_dependency "railties", ">= 6.0"
|
33
|
+
spec.add_dependency "activesupport", ">= 6.0"
|
34
|
+
spec.add_development_dependency "activerecord", ">= 6.0"
|
35
|
+
spec.add_development_dependency "railties", ">= 6.0"
|
36
36
|
spec.add_development_dependency "rake"
|
37
37
|
spec.add_development_dependency "rspec"
|
38
38
|
spec.add_development_dependency "rubocop"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kirill Usanov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -18,9 +18,6 @@ dependencies:
|
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '6.0'
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '7.1'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,9 +25,6 @@ dependencies:
|
|
28
25
|
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
27
|
version: '6.0'
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '7.1'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: activerecord
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,9 +32,6 @@ dependencies:
|
|
38
32
|
- - ">="
|
39
33
|
- !ruby/object:Gem::Version
|
40
34
|
version: '6.0'
|
41
|
-
- - "<"
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '7.1'
|
44
35
|
type: :development
|
45
36
|
prerelease: false
|
46
37
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -48,9 +39,6 @@ dependencies:
|
|
48
39
|
- - ">="
|
49
40
|
- !ruby/object:Gem::Version
|
50
41
|
version: '6.0'
|
51
|
-
- - "<"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '7.1'
|
54
42
|
- !ruby/object:Gem::Dependency
|
55
43
|
name: railties
|
56
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,9 +46,6 @@ dependencies:
|
|
58
46
|
- - ">="
|
59
47
|
- !ruby/object:Gem::Version
|
60
48
|
version: '6.0'
|
61
|
-
- - "<"
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version: '7.1'
|
64
49
|
type: :development
|
65
50
|
prerelease: false
|
66
51
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -68,9 +53,6 @@ dependencies:
|
|
68
53
|
- - ">="
|
69
54
|
- !ruby/object:Gem::Version
|
70
55
|
version: '6.0'
|
71
|
-
- - "<"
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '7.1'
|
74
56
|
- !ruby/object:Gem::Dependency
|
75
57
|
name: rake
|
76
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -197,9 +179,8 @@ dependencies:
|
|
197
179
|
- - ">="
|
198
180
|
- !ruby/object:Gem::Version
|
199
181
|
version: '0'
|
200
|
-
description: Simple filtering for
|
201
|
-
email:
|
202
|
-
- kirill.usanov.dev@gmail.com
|
182
|
+
description: Simple filtering for ActiveRecord and enumerables.
|
183
|
+
email: kirill@lassoid.ru
|
203
184
|
executables: []
|
204
185
|
extensions: []
|
205
186
|
extra_rdoc_files: []
|
@@ -214,10 +195,10 @@ files:
|
|
214
195
|
- Rakefile
|
215
196
|
- lib/generators/tiny_filter/filter/USAGE
|
216
197
|
- lib/generators/tiny_filter/filter/filter_generator.rb
|
217
|
-
- lib/generators/tiny_filter/filter/templates/filter.rb.
|
198
|
+
- lib/generators/tiny_filter/filter/templates/filter.rb.tt
|
218
199
|
- lib/generators/tiny_filter/install/USAGE
|
219
200
|
- lib/generators/tiny_filter/install/install_generator.rb
|
220
|
-
- lib/generators/tiny_filter/install/templates/application_filter.rb
|
201
|
+
- lib/generators/tiny_filter/install/templates/application_filter.rb.tt
|
221
202
|
- lib/tiny_filter.rb
|
222
203
|
- lib/tiny_filter/base.rb
|
223
204
|
- lib/tiny_filter/concern.rb
|
@@ -247,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
247
228
|
- !ruby/object:Gem::Version
|
248
229
|
version: '0'
|
249
230
|
requirements: []
|
250
|
-
rubygems_version: 3.
|
231
|
+
rubygems_version: 3.4.17
|
251
232
|
signing_key:
|
252
233
|
specification_version: 4
|
253
234
|
summary: Tiny filtering for Rails.
|
File without changes
|
File without changes
|