tiny_filter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52eb68e11715585163330d5f814e72744ad26114320d87dd9dd1cb80326bf3a7
4
- data.tar.gz: c57812b379ade0d6dcdd4f262c6cd1e00773c15f9e1d6292594eff133eef4556
3
+ metadata.gz: b25430ea35a61c7a2ce2bc81b38596990385179c11b4b2797c441df5e1813725
4
+ data.tar.gz: ec073eef8a8faf00b171e9ba92fa356a76fa3947b76f314cab3e6997855a618c
5
5
  SHA512:
6
- metadata.gz: 757d3bc888fc7658bb1c72ce82871be05f4dcf90d50eb48b9cf610267f11f2a44b12e6844b84d074593f19de61462e4daf44cb1e43452ce55117562bebebd5e4
7
- data.tar.gz: 59f7ae5777c94a9b01c7f51848ae9e3ee3b365643e44702e836ccd42c3a77d5fc93f21eecd47aefac9a327dee8d644d3f39476c6e114ce1fa1779fbcc9245174
6
+ metadata.gz: '0905513bd90fe26f0bc2ead908f73688e3dbe69da16d4864f5e7d64a3be6b0aa1e3f159b270498bb7e8f3c1faced5ac9707a318034c433e3bdc2746e7d9742ba'
7
+ data.tar.gz: 98498c4a965f6b7262418e7a952a6b164ef6baa08c869503b971a3461486cb0c6580cf62be73aaff98282d0c4ad3a087203751554fcd92b668ca2f63d2d64e11
data/.rubocop.yml CHANGED
@@ -17,9 +17,6 @@ Layout/EmptyLinesAroundAccessModifier:
17
17
  Layout/IndentationConsistency:
18
18
  EnforcedStyle: normal
19
19
 
20
- Layout/EmptyLinesAroundClassBody:
21
- EnforcedStyle: empty_lines_special
22
-
23
20
  Style/SymbolArray:
24
21
  EnforcedStyle: percent
25
22
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-12-17
4
+
5
+ - Filters inheritance
6
+
7
+ ## [0.1.1] - 2022-12-12
8
+
9
+ - Fixed generators
10
+
3
11
  ## [0.1.0] - 2022-12-05
4
12
 
5
13
  - Initial release
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # TinyFilter
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/tiny_filter.svg)](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)
6
+
3
7
  TinyFilter is created to provide a simple object-oriented abstraction layer for filtering collections.
4
8
  It is mainly purposed for ActiveRecord collections, but you can also use it with any enumerable.
5
9
 
@@ -40,11 +44,11 @@ Each filter is defined by calling `filters` method inside class body.
40
44
 
41
45
  `filters` accepts two arguments:
42
46
  - `key` - a filter name, used as identifier;
43
- - `block` - a block or proc/lambda with filter logic, that returns filtered collection and itself accepts two arguments:
47
+ - `block` - a block with filter logic, that returns filtered collection and itself accepts two arguments:
44
48
  - `scope` - a collection that should be filtered;
45
49
  - `value` - a value for filtering.
46
50
 
47
- When you perform filtering, provided keys indicate filters `key`s and provided `value`s are passed to corresponding filters `block`s.
51
+ When you perform filtering, provided key indicate filter `key` and provided value is passed to `value` param in corresponding filter `block`.
48
52
  `scope`s receive collections in a pipeline manner:
49
53
  _first_ executed filter receives _original collection_,
50
54
  _second and further_ receive the _return collection_ of the previous filter.
@@ -58,7 +62,7 @@ class UserFilter < ApplicationFilter
58
62
  end
59
63
 
60
64
  UserFilter.filter(User, name: "John", surname: "Doe")
61
- # It performs like this inside:
65
+ # Which is equivalent to:
62
66
  # User.where(first_name: "John").where(last_name: "Doe")
63
67
  ```
64
68
 
@@ -68,14 +72,14 @@ It guarantees that scope behaves the same way as in other filters in this class.
68
72
  ```ruby
69
73
  filters(:title) { |scope, value| scope.where("title ILIKE ?", value) }
70
74
 
71
- # bad - scope is meant to be ActiveRecord collection, but the return value is an array.
75
+ # bad - scope is an ActiveRecord collection, but the return value is an array.
72
76
  filters(:from) { |scope, value| scope.select { |e| e.created_at >= value } }
73
77
 
74
78
  # good - scope and return value are both ActiveRecord collections.
75
79
  filters(:from) { |scope, value| scope.where("created_at >= ?", value) }
76
80
  ```
77
81
 
78
- So, if the initial scope for filtering is ActiveRecord collection,
82
+ Thus if the initial scope for filtering is an ActiveRecord collection,
79
83
  it is a bad practice for filter to return not an ActiveRecord collection.
80
84
  Otherwise you can face errors depending on the provided options order.
81
85
 
@@ -117,7 +121,7 @@ class My::Class < ApplicationRecord
117
121
  end
118
122
  ```
119
123
 
120
- ### Using with Plain objects
124
+ ## Using with Plain objects
121
125
 
122
126
  You can use filters with non-ActiveRecord collections like so:
123
127
 
@@ -11,7 +11,7 @@ module TinyFilter
11
11
 
12
12
  def create_filter
13
13
  template_file = File.join("app/filters", class_path, filter_file_name)
14
- template "filter.rb.erb", template_file
14
+ template "filter.rb.tt", template_file
15
15
  end
16
16
 
17
17
  private
@@ -19,7 +19,6 @@ module TinyFilter
19
19
  def filter_file_name
20
20
  "#{file_name}_filter.rb"
21
21
  end
22
-
23
22
  end
24
23
  end
25
24
  end
@@ -8,9 +8,8 @@ module TinyFilter
8
8
  desc "This generator creates an application filter"
9
9
 
10
10
  def copy_application_filter
11
- template "application_filter.rb", "app/filters/application_filter.rb"
11
+ template "application_filter.rb.tt", "app/filters/application_filter.rb"
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
@@ -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 = key.to_sym
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
- private
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TinyFilter
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/tiny_filter.rb CHANGED
@@ -11,5 +11,4 @@ require "tiny_filter/concern"
11
11
  module TinyFilter
12
12
  class Error < StandardError; end
13
13
  class NotDefinedError < Error; end
14
- class AlreadyDefinedError < Error; end
15
14
  end
data/tiny_filter.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["kirill.usanov.dev@gmail.com"]
10
10
 
11
11
  spec.summary = "Tiny filtering for Rails."
12
- spec.description = "Simple filtering for your Rails models and enumerables."
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"
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.1.0
4
+ version: 0.2.0
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: 2022-12-11 00:00:00.000000000 Z
12
+ date: 2022-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -197,7 +197,7 @@ dependencies:
197
197
  - - ">="
198
198
  - !ruby/object:Gem::Version
199
199
  version: '0'
200
- description: Simple filtering for your Rails models and enumerables.
200
+ description: Simple filtering for ActiveRecord and enumerables.
201
201
  email:
202
202
  - kirill.usanov.dev@gmail.com
203
203
  executables: []
@@ -212,16 +212,16 @@ files:
212
212
  - LICENSE
213
213
  - README.md
214
214
  - Rakefile
215
+ - lib/generators/tiny_filter/filter/USAGE
216
+ - lib/generators/tiny_filter/filter/filter_generator.rb
217
+ - lib/generators/tiny_filter/filter/templates/filter.rb.tt
218
+ - lib/generators/tiny_filter/install/USAGE
219
+ - lib/generators/tiny_filter/install/install_generator.rb
220
+ - lib/generators/tiny_filter/install/templates/application_filter.rb.tt
215
221
  - lib/tiny_filter.rb
216
222
  - lib/tiny_filter/base.rb
217
223
  - lib/tiny_filter/concern.rb
218
224
  - lib/tiny_filter/filter_finder.rb
219
- - lib/tiny_filter/generators/tiny_filter/filter/USAGE
220
- - lib/tiny_filter/generators/tiny_filter/filter/filter_generator.rb
221
- - lib/tiny_filter/generators/tiny_filter/filter/templates/filter.rb.erb
222
- - lib/tiny_filter/generators/tiny_filter/install/USAGE
223
- - lib/tiny_filter/generators/tiny_filter/install/install_generator.rb
224
- - lib/tiny_filter/generators/tiny_filter/install/templates/application_filter.rb
225
225
  - lib/tiny_filter/version.rb
226
226
  - sig/tiny_filter.rbs
227
227
  - tiny_filter.gemspec
@@ -247,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  - !ruby/object:Gem::Version
248
248
  version: '0'
249
249
  requirements: []
250
- rubygems_version: 3.3.7
250
+ rubygems_version: 3.3.26
251
251
  signing_key:
252
252
  specification_version: 4
253
253
  summary: Tiny filtering for Rails.