tiny_filter 0.2.1 → 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/CHANGELOG.md +4 -1
- data/README.md +25 -6
- 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 +3 -3
- metadata +18 -18
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/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
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 }`
|
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
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Kirill Usanov", "LassoID"]
|
9
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,7 +30,6 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.require_paths = ["lib"]
|
31
31
|
|
32
32
|
# Dependencies
|
33
|
-
spec.add_dependency "activesupport", ">= 6.0"
|
34
33
|
spec.add_development_dependency "activerecord", ">= 6.0"
|
35
34
|
spec.add_development_dependency "railties", ">= 6.0"
|
36
35
|
spec.add_development_dependency "rake"
|
@@ -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,22 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-
|
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
|
-
type: :runtime
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: '6.0'
|
28
14
|
- !ruby/object:Gem::Dependency
|
29
15
|
name: activerecord
|
30
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -165,6 +151,20 @@ dependencies:
|
|
165
151
|
- - ">="
|
166
152
|
- !ruby/object:Gem::Version
|
167
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'
|
168
168
|
- !ruby/object:Gem::Dependency
|
169
169
|
name: sqlite3
|
170
170
|
requirement: !ruby/object:Gem::Requirement
|
@@ -179,7 +179,7 @@ dependencies:
|
|
179
179
|
- - ">="
|
180
180
|
- !ruby/object:Gem::Version
|
181
181
|
version: '0'
|
182
|
-
description: Simple filtering for ActiveRecord and enumerables.
|
182
|
+
description: Simple filtering for ActiveRecord, Sequel and enumerables.
|
183
183
|
email: kirill@lassoid.ru
|
184
184
|
executables: []
|
185
185
|
extensions: []
|
@@ -231,5 +231,5 @@ requirements: []
|
|
231
231
|
rubygems_version: 3.4.17
|
232
232
|
signing_key:
|
233
233
|
specification_version: 4
|
234
|
-
summary: Tiny filtering for
|
234
|
+
summary: Tiny filtering for ActiveRecord, Sequel and enumerables.
|
235
235
|
test_files: []
|