tiny_filter 0.2.0 → 0.3.0
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 -0
- data/CHANGELOG.md +9 -1
- data/CODE_OF_CONDUCT.md +1 -1
- data/README.md +31 -11
- data/Rakefile +0 -9
- data/lib/generators/tiny_filter/filter/filter_generator.rb +2 -0
- data/lib/generators/tiny_filter/install/USAGE +1 -1
- data/lib/generators/tiny_filter/install/install_generator.rb +2 -0
- data/lib/tiny_filter/concern.rb +14 -7
- data/lib/tiny_filter/filter_finder.rb +10 -2
- data/lib/tiny_filter/version.rb +1 -1
- data/tiny_filter.gemspec +6 -6
- metadata +20 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33ab33afed080553844b220a53dd8f63f88d303b9b06f751de4f1985f11ca745
|
4
|
+
data.tar.gz: 1f62c70732e0cc8a529b851babcd5c3f0ef455626db4faf633a28d8ad79624ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3bc9efb9ada2976656c97b849adb4df37428218976f6d5bcc31be2907e8e2158fb7270e9ef89cccb4854a98fd2533fa86e7ea3b503fcefc255264ea48c14f69
|
7
|
+
data.tar.gz: 685a3e02de2e4a45d72ee394f961e567202860791b5fba770b6c4519246d611e20e760cd742c9bd850bd6abe98c632f5f776113cd71537c83c947e7802efcb7e
|
data/.rubocop.yml
CHANGED
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,11 +1,11 @@
|
|
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
4
|
[![Gem downloads count](https://img.shields.io/gem/dt/tiny_filter)](https://rubygems.org/gems/tiny_filter)
|
5
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)
|
6
6
|
|
7
7
|
TinyFilter is created to provide a simple object-oriented abstraction layer for filtering collections.
|
8
|
-
It is mainly purposed for ActiveRecord
|
8
|
+
It is mainly purposed for ActiveRecord/Sequel models, but you can also use it with any enumerable.
|
9
9
|
|
10
10
|
```ruby
|
11
11
|
Post.where(title: "Wow!").filter_by(from: 2.days.ago, to: 1.day.ago).order(:created_at)
|
@@ -83,11 +83,12 @@ Thus if the initial scope for filtering is an ActiveRecord collection,
|
|
83
83
|
it is a bad practice for filter to return not an ActiveRecord collection.
|
84
84
|
Otherwise you can face errors depending on the provided options order.
|
85
85
|
|
86
|
-
##
|
86
|
+
## ORM integration
|
87
87
|
|
88
|
-
###
|
88
|
+
### ActiveRecord
|
89
89
|
|
90
|
-
TinyFilter provides a simple concern, that adds just one method `filter_by`,
|
90
|
+
TinyFilter provides a simple concern, that adds just one method `filter_by`,
|
91
|
+
that can be used in ActiveRecord method chaining.
|
91
92
|
|
92
93
|
Just include `TinyFilter::Concern` in your model and that's all!
|
93
94
|
|
@@ -104,12 +105,30 @@ Post.where(title: "something interesting").filter_by(from: 2.days.ago, to: 1.day
|
|
104
105
|
Post.filter_by(from: 1.year.ago)
|
105
106
|
```
|
106
107
|
|
108
|
+
### Sequel
|
109
|
+
|
110
|
+
The previously mentioned filter concern can also be used in Sequel models.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
class Artist < Sequel::Model
|
114
|
+
include TinyFilter::Concern
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
Querying examples:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
Artist.where(name: "Kirill").filter_by(from: 2.days.ago, to: 1.day.ago).order(:name).all
|
122
|
+
Artist.filter_by(from: 1.year.ago).all
|
123
|
+
```
|
124
|
+
|
107
125
|
### Naming convention
|
108
126
|
|
109
127
|
By default a filter class and a model are mapped by a _model name_ with a _suffix_ `Filter`.
|
110
128
|
For example, the model `My::Class` by default will use the `My::ClassFilter` as a filter class.
|
111
129
|
|
112
|
-
You can customize this behavior by implementing a `filter_class` class method
|
130
|
+
You can customize this behavior by implementing a `filter_class` class method
|
131
|
+
with an appropriate class as a return value.
|
113
132
|
|
114
133
|
```ruby
|
115
134
|
class My::Class < ApplicationRecord
|
@@ -123,7 +142,7 @@ end
|
|
123
142
|
|
124
143
|
## Using with Plain objects
|
125
144
|
|
126
|
-
You can use filters with
|
145
|
+
You can use filters with Plain Old Ruby collections like so:
|
127
146
|
|
128
147
|
```ruby
|
129
148
|
options # filter options, for example: `{ from: 2.days.ago, to: 1.day.ago }`
|
@@ -134,11 +153,12 @@ MyFilter.filter(collection, options)
|
|
134
153
|
|
135
154
|
## Development
|
136
155
|
|
137
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
138
|
-
You can also run `bin/
|
156
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests.
|
157
|
+
You can also run `bin/rubocop` to lint the source code
|
158
|
+
and `bin/console` for an interactive prompt that will allow you to experiment.
|
139
159
|
|
140
|
-
To install this gem onto your local machine, run `
|
141
|
-
To release a new version, update the version number in `version.rb`, and then run `
|
160
|
+
To install this gem onto your local machine, run `bin/rake install`.
|
161
|
+
To release a new version, update the version number in `version.rb`, and then run `bin/rake release`,
|
142
162
|
which will create a git tag for the version, push git commits and the created tag,
|
143
163
|
and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
144
164
|
|
data/Rakefile
CHANGED
data/lib/tiny_filter/concern.rb
CHANGED
@@ -2,13 +2,20 @@
|
|
2
2
|
|
3
3
|
module TinyFilter
|
4
4
|
module Concern
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
class << self
|
6
|
+
def included(other)
|
7
|
+
if defined?(ActiveRecord::Base) && other <= ActiveRecord::Base
|
8
|
+
other.scope :filter_by, ->(args = {}) { TinyFilter::FilterFinder.find(self).filter(self, args) }
|
9
|
+
elsif defined?(Sequel::Model) && other <= Sequel::Model
|
10
|
+
other.dataset_module do
|
11
|
+
def filter_by(args = {})
|
12
|
+
TinyFilter::FilterFinder.find(self).filter(self, args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
else
|
16
|
+
raise Error, "unable to include TinyFilter::Concern in #{other} " \
|
17
|
+
"that is not an ActiveRecord::Base or Sequel::Model descendant"
|
18
|
+
end
|
12
19
|
end
|
13
20
|
end
|
14
21
|
end
|
@@ -15,10 +15,18 @@ module TinyFilter
|
|
15
15
|
if object.respond_to?(:filter_class)
|
16
16
|
object.filter_class
|
17
17
|
elsif object.respond_to?(:model_name)
|
18
|
-
"#{object.model_name}#{SUFFIX}"
|
18
|
+
Object.const_get("#{object.model_name}#{SUFFIX}")
|
19
|
+
elsif object.respond_to?(:model)
|
20
|
+
if object.model.respond_to?(:filter_class)
|
21
|
+
object.model.filter_class
|
22
|
+
else
|
23
|
+
Object.const_get("#{object.model}#{SUFFIX}")
|
24
|
+
end
|
19
25
|
else
|
20
|
-
|
26
|
+
Object.const_get("#{object}#{SUFFIX}")
|
21
27
|
end
|
28
|
+
rescue NameError
|
29
|
+
raise Error, "unable to find appropriate filter class for #{object}"
|
22
30
|
end
|
23
31
|
end
|
24
32
|
end
|
data/lib/tiny_filter/version.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
|
-
spec.summary = "Tiny filtering for
|
12
|
-
spec.description = "Simple filtering for ActiveRecord and enumerables."
|
11
|
+
spec.summary = "Tiny filtering for ActiveRecord, Sequel and enumerables."
|
12
|
+
spec.description = "Simple filtering for ActiveRecord, Sequel 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,8 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.require_paths = ["lib"]
|
31
31
|
|
32
32
|
# Dependencies
|
33
|
-
spec.
|
34
|
-
spec.add_development_dependency "
|
35
|
-
spec.add_development_dependency "railties", ">= 6.0", "< 7.1"
|
33
|
+
spec.add_development_dependency "activerecord", ">= 6.0"
|
34
|
+
spec.add_development_dependency "railties", ">= 6.0"
|
36
35
|
spec.add_development_dependency "rake"
|
37
36
|
spec.add_development_dependency "rspec"
|
38
37
|
spec.add_development_dependency "rubocop"
|
@@ -41,5 +40,6 @@ Gem::Specification.new do |spec|
|
|
41
40
|
spec.add_development_dependency "rubocop-rake"
|
42
41
|
spec.add_development_dependency "rubocop-rspec"
|
43
42
|
spec.add_development_dependency "rubocop-shopify"
|
43
|
+
spec.add_development_dependency "sequel"
|
44
44
|
spec.add_development_dependency "sqlite3"
|
45
45
|
end
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kirill Usanov
|
@@ -9,28 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - ">="
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '6.0'
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '7.1'
|
24
|
-
type: :runtime
|
25
|
-
prerelease: false
|
26
|
-
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
requirements:
|
28
|
-
- - ">="
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: '6.0'
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '7.1'
|
34
14
|
- !ruby/object:Gem::Dependency
|
35
15
|
name: activerecord
|
36
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,9 +18,6 @@ dependencies:
|
|
38
18
|
- - ">="
|
39
19
|
- !ruby/object:Gem::Version
|
40
20
|
version: '6.0'
|
41
|
-
- - "<"
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '7.1'
|
44
21
|
type: :development
|
45
22
|
prerelease: false
|
46
23
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -48,9 +25,6 @@ dependencies:
|
|
48
25
|
- - ">="
|
49
26
|
- !ruby/object:Gem::Version
|
50
27
|
version: '6.0'
|
51
|
-
- - "<"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '7.1'
|
54
28
|
- !ruby/object:Gem::Dependency
|
55
29
|
name: railties
|
56
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,9 +32,6 @@ dependencies:
|
|
58
32
|
- - ">="
|
59
33
|
- !ruby/object:Gem::Version
|
60
34
|
version: '6.0'
|
61
|
-
- - "<"
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version: '7.1'
|
64
35
|
type: :development
|
65
36
|
prerelease: false
|
66
37
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -68,9 +39,6 @@ dependencies:
|
|
68
39
|
- - ">="
|
69
40
|
- !ruby/object:Gem::Version
|
70
41
|
version: '6.0'
|
71
|
-
- - "<"
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '7.1'
|
74
42
|
- !ruby/object:Gem::Dependency
|
75
43
|
name: rake
|
76
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -183,6 +151,20 @@ dependencies:
|
|
183
151
|
- - ">="
|
184
152
|
- !ruby/object:Gem::Version
|
185
153
|
version: '0'
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: sequel
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
186
168
|
- !ruby/object:Gem::Dependency
|
187
169
|
name: sqlite3
|
188
170
|
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 ActiveRecord and enumerables.
|
201
|
-
email:
|
202
|
-
- kirill.usanov.dev@gmail.com
|
182
|
+
description: Simple filtering for ActiveRecord, Sequel and enumerables.
|
183
|
+
email: kirill@lassoid.ru
|
203
184
|
executables: []
|
204
185
|
extensions: []
|
205
186
|
extra_rdoc_files: []
|
@@ -247,8 +228,8 @@ 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
|
-
summary: Tiny filtering for
|
234
|
+
summary: Tiny filtering for ActiveRecord, Sequel and enumerables.
|
254
235
|
test_files: []
|