tiny_filter 0.1.1 → 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: 70649a100b15254ca35dc3949cbbd549f87a34e41f85d87d600c42fc8d3401f0
4
- data.tar.gz: af5850f2ee780906e08c24f67510224543ef46e873c342faeaa05a56e8f3c08f
3
+ metadata.gz: b25430ea35a61c7a2ce2bc81b38596990385179c11b4b2797c441df5e1813725
4
+ data.tar.gz: ec073eef8a8faf00b171e9ba92fa356a76fa3947b76f314cab3e6997855a618c
5
5
  SHA512:
6
- metadata.gz: d2bab5a83fb731582136d1fd219906dc0a1a44ac0a3dd83c24d0317fc5cc66f3303cca654944aae0ab23d680dbc6c1af23ccc8afd19fdd97173fc374398a3f61
7
- data.tar.gz: bfc8bc3fcf4ea121c403e61c27bf9ffa516d7f94401b38cd4d7edc0f2ee3b14b42229500bd9c0da0b0c05ca79e08f61e9f31f52afa471688eb1b29bb3160c5f4
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,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-12-17
4
+
5
+ - Filters inheritance
6
+
3
7
  ## [0.1.1] - 2022-12-12
4
8
 
5
9
  - Fixed generators
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # TinyFilter
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/tiny_filter.svg)](https://badge.fury.io/rb/tiny_filter)
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)
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 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:
46
48
  - `scope` - a collection that should be filtered;
47
49
  - `value` - a value for filtering.
48
50
 
49
- 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`.
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
- # It performs like this inside:
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 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.
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
- So, if the initial scope for filtering is ActiveRecord collection,
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
- ### Using with Plain objects
124
+ ## Using with Plain objects
123
125
 
124
126
  You can use filters with non-ActiveRecord collections like so:
125
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.1"
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.1
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: []
@@ -214,10 +214,10 @@ files:
214
214
  - Rakefile
215
215
  - lib/generators/tiny_filter/filter/USAGE
216
216
  - lib/generators/tiny_filter/filter/filter_generator.rb
217
- - lib/generators/tiny_filter/filter/templates/filter.rb.erb
217
+ - lib/generators/tiny_filter/filter/templates/filter.rb.tt
218
218
  - lib/generators/tiny_filter/install/USAGE
219
219
  - lib/generators/tiny_filter/install/install_generator.rb
220
- - lib/generators/tiny_filter/install/templates/application_filter.rb
220
+ - lib/generators/tiny_filter/install/templates/application_filter.rb.tt
221
221
  - lib/tiny_filter.rb
222
222
  - lib/tiny_filter/base.rb
223
223
  - lib/tiny_filter/concern.rb
@@ -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.