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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70649a100b15254ca35dc3949cbbd549f87a34e41f85d87d600c42fc8d3401f0
4
- data.tar.gz: af5850f2ee780906e08c24f67510224543ef46e873c342faeaa05a56e8f3c08f
3
+ metadata.gz: 5cf60cc543019e4d02a3c8ffda90cd0e27d224feefdd0517f1b359c9e598b7f2
4
+ data.tar.gz: fc7bceb2b9ec9265d4535fcacbf65c207e2df6b689b3d3117a839ccffdba6524
5
5
  SHA512:
6
- metadata.gz: d2bab5a83fb731582136d1fd219906dc0a1a44ac0a3dd83c24d0317fc5cc66f3303cca654944aae0ab23d680dbc6c1af23ccc8afd19fdd97173fc374398a3f61
7
- data.tar.gz: bfc8bc3fcf4ea121c403e61c27bf9ffa516d7f94401b38cd4d7edc0f2ee3b14b42229500bd9c0da0b0c05ca79e08f61e9f31f52afa471688eb1b29bb3160c5f4
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
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2023-10-20
4
+
5
+ - Do not fix max dependencies versions
6
+ - Improved generators
7
+
8
+ ## [0.2.0] - 2022-12-17
9
+
10
+ - Filters inheritance
11
+
3
12
  ## [0.1.1] - 2022-12-12
4
13
 
5
14
  - Fixed generators
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.usanov.dev@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
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://badge.fury.io/rb/tiny_filter.svg)](https://badge.fury.io/rb/tiny_filter)
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 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
 
@@ -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 `rake spec`/`rspec` to run the tests.
136
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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 `bundle exec rake install`.
139
- To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
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,12 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
@@ -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.erb", template_file
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,5 @@
1
1
  Description:
2
- Generates application filter - base class for all your filters.
2
+ Generates an application filter - a base class for all your filters.
3
3
 
4
4
  Examples:
5
5
  `bin/rails generate tiny_filter:install`
@@ -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
@@ -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.1"
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
@@ -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 = ["kirill.usanov.dev@gmail.com"]
9
+ spec.email = "kirill@lassoid.ru"
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"
@@ -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", "< 7.1"
34
- spec.add_development_dependency "activerecord", ">= 6.0", "< 7.1"
35
- spec.add_development_dependency "railties", ">= 6.0", "< 7.1"
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.1.1
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: 2022-12-11 00:00:00.000000000 Z
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 your Rails models and enumerables.
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.erb
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.3.7
231
+ rubygems_version: 3.4.17
251
232
  signing_key:
252
233
  specification_version: 4
253
234
  summary: Tiny filtering for Rails.