syto 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +11 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/syto/version.rb +5 -0
- data/lib/syto.rb +194 -0
- data/syto.gemspec +48 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 680b0e8f2fc3d847fc27ff9e5b3f5891d5d5331f84661250b90774e018daacde
|
4
|
+
data.tar.gz: ca58ebde9c0c3d6ece12a1f6f0ea44dd69e4151ba437e5c0992583dc75254665
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cf3dc9333fa4c2b1be5bece3563f6e77c47ccf5503e3da299f5078b6801eb43d837790e58f670449bd183cb9665893531dde40c9907d217e9999d39a847a4b6
|
7
|
+
data.tar.gz: b84f56820dd009d84fe71310fed024e7a06fa4cadc5c50c24640fc2654b87413f2641f8134fe7c0672fe40ba8c48c4016f69d667f75e1c03b14e9464b09ffb0e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in syto.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'codecov', require: false
|
12
|
+
gem 'minitest'
|
13
|
+
gem 'minitest-reporters'
|
14
|
+
gem 'rubocop'
|
15
|
+
gem 'rubocop-minitest'
|
16
|
+
gem 'sqlite3'
|
17
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Vadym Lukavyi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Syto
|
2
|
+
|
3
|
+
Simple gem to filter data for ruby on rails models
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'syto'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install syto
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Initialize Syto in AR model
|
24
|
+
|
25
|
+
Define filters for attributes `country`, `area_id` and `rate`
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# app/model/user.rb
|
29
|
+
class User < ActiveModel
|
30
|
+
include Syto
|
31
|
+
syto_attrs_map :active,
|
32
|
+
country: { key: :country, case_insensitive: true }, # allows to filter by 'users.country'
|
33
|
+
area_id: { key: :region }, # allows to filter by 'users.area_id' as 'region'
|
34
|
+
rate: { key_from: :rate_from, key_to: :rate_to } # allows to filter by 'users.rate' with range
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Define custom filters in class
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# app/models/post.rb
|
42
|
+
class Post < ActiveModel
|
43
|
+
include Syto
|
44
|
+
syto_filters_class PostFilters
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
# app/models/concerns/post_filter.rb
|
50
|
+
class PostFilters < Syto
|
51
|
+
def extended_filters
|
52
|
+
self.result = result.where(published: params[:published])
|
53
|
+
filter_by_range(:published_at, key_from: :pub_from, key_to: :pub_to)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Use in code:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
params = { country: 'UA', rate_from: 2, rate_to: 3 }
|
62
|
+
User.filter_by(params)
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
params = { published: true, pub_from: '2021-01-01' }
|
67
|
+
Post.filter_by(params) # select published posts from 2021
|
68
|
+
```
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/p9436/syto.
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'syto'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/syto/version.rb
ADDED
data/lib/syto.rb
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'syto/version'
|
4
|
+
|
5
|
+
# Syto
|
6
|
+
module Syto
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Class methods for Syto
|
14
|
+
module ClassMethods
|
15
|
+
# Define class with custom filter implementation
|
16
|
+
#
|
17
|
+
# $ cat app/model/user.rb
|
18
|
+
# class User < ActiveModel
|
19
|
+
# include Syto
|
20
|
+
# syto_attrs_map :role_id, :country, rate: { key_from: rate_from, key_to: rate_to }
|
21
|
+
# end
|
22
|
+
|
23
|
+
# $ cat app/models/post.rb
|
24
|
+
# class Post < ActiveModel
|
25
|
+
# include Syto
|
26
|
+
# syto_filters_class PostFilter
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# $ cat app/models/concerns/post_filter.rb
|
30
|
+
# class PostFilter < Syto
|
31
|
+
# def define_filters
|
32
|
+
# self.result = result.where(published: params[:published])
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# Usage:
|
37
|
+
#
|
38
|
+
# params = { country: 'UA', rate_from: 2 }
|
39
|
+
# User.filter_by(params)
|
40
|
+
#
|
41
|
+
# params = { published: true }
|
42
|
+
# Post.syto(params)
|
43
|
+
#
|
44
|
+
|
45
|
+
# Set class with customized filter settings
|
46
|
+
#
|
47
|
+
# @param [Class] filter_klass
|
48
|
+
#
|
49
|
+
def syto_filters_class(filter_klass)
|
50
|
+
@filter_klass = filter_klass
|
51
|
+
end
|
52
|
+
|
53
|
+
# Define map of filterable attributes
|
54
|
+
#
|
55
|
+
# @param [Array] attrs_map
|
56
|
+
#
|
57
|
+
def syto_attrs_map(*attrs_map)
|
58
|
+
@syto_attrs_map = attrs_map
|
59
|
+
end
|
60
|
+
|
61
|
+
# Filtering method
|
62
|
+
#
|
63
|
+
# @param [Hash] params
|
64
|
+
#
|
65
|
+
# @return [ActiveRecord::Relation]
|
66
|
+
#
|
67
|
+
def filter_by(params)
|
68
|
+
return all if params.blank? || params.empty?
|
69
|
+
|
70
|
+
@filter_klass ||= Syto::Base
|
71
|
+
@filter_klass.new(all, params.symbolize_keys, @syto_attrs_map).perform
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Base class for filters implementation
|
76
|
+
#
|
77
|
+
class Base
|
78
|
+
attr_accessor :result, :params
|
79
|
+
|
80
|
+
# @param [ActiveRecord::Relation] result
|
81
|
+
# @param [Hash] params
|
82
|
+
# @param [Array] attrs_map
|
83
|
+
#
|
84
|
+
def initialize(result, params, attrs_map)
|
85
|
+
@result = result
|
86
|
+
@params = params
|
87
|
+
@attrs_map = attrs_map
|
88
|
+
end
|
89
|
+
|
90
|
+
def perform
|
91
|
+
filter_by_attrs_map
|
92
|
+
|
93
|
+
extended_filters
|
94
|
+
|
95
|
+
result
|
96
|
+
end
|
97
|
+
|
98
|
+
def extended_filters
|
99
|
+
puts '[WARNING] Filters not defined'
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def filter_by_attrs_map
|
105
|
+
return unless @attrs_map
|
106
|
+
|
107
|
+
@attrs_map.flatten.each do |filter|
|
108
|
+
case filter
|
109
|
+
when Symbol, String
|
110
|
+
scalar_filter(filter)
|
111
|
+
when Hash
|
112
|
+
hash_filter(filter)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Filter by attribute
|
118
|
+
#
|
119
|
+
# @param [String. Symbol] attr
|
120
|
+
# @param [Hash] params_key
|
121
|
+
#
|
122
|
+
def scalar_filter(attr, params_key = nil)
|
123
|
+
params_key ||= attr
|
124
|
+
params_key = params_key.to_sym
|
125
|
+
return unless @params[params_key]
|
126
|
+
|
127
|
+
@result = @result.where(attr => @params[params_key])
|
128
|
+
end
|
129
|
+
|
130
|
+
# Filter by attribute with options
|
131
|
+
#
|
132
|
+
# @param [Hash] attrs
|
133
|
+
#
|
134
|
+
def hash_filter(attrs)
|
135
|
+
attrs.each do |attr, options|
|
136
|
+
case options
|
137
|
+
when String, Symbol
|
138
|
+
scalar_filter attr, options
|
139
|
+
when Hash
|
140
|
+
filter_hash_options(attr, options)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def filter_hash_options(attr, options)
|
146
|
+
if options.key?(:key)
|
147
|
+
filter_by_value attr, options
|
148
|
+
else
|
149
|
+
filter_by_range attr, options
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Filter by single value
|
154
|
+
#
|
155
|
+
# @param [Symbol] field_name
|
156
|
+
# @param [Hash] options
|
157
|
+
#
|
158
|
+
def filter_by_value(field_name, options = {})
|
159
|
+
options ||= {}
|
160
|
+
|
161
|
+
key ||= options[:key] || field_name
|
162
|
+
return unless @params.key?(key)
|
163
|
+
|
164
|
+
value = @params[key]
|
165
|
+
|
166
|
+
@result = if options[:case_insensitive] && value.respond_to?(:downcase)
|
167
|
+
@result.where(result.arel_table[field_name].lower.in(value.downcase))
|
168
|
+
else
|
169
|
+
@result.where(field_name => value)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Filter by range
|
174
|
+
#
|
175
|
+
# @param [Symbol] field_name
|
176
|
+
# @param [Hash{Symbol->Object}] options
|
177
|
+
#
|
178
|
+
# options:
|
179
|
+
# - key_from,
|
180
|
+
# - key_to,
|
181
|
+
#
|
182
|
+
def filter_by_range(field_name, options = {})
|
183
|
+
key_from = options[:key_from] || :"#{field_name}_from"
|
184
|
+
key_to = options[:key_to] || :"#{field_name}_to"
|
185
|
+
|
186
|
+
value_from = @params[key_from]
|
187
|
+
value_to = @params[key_to]
|
188
|
+
|
189
|
+
return if value_from.blank? && value_to.blank?
|
190
|
+
|
191
|
+
@result = @result.where(field_name => (value_from..value_to))
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
data/syto.gemspec
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'syto/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'syto'
|
9
|
+
spec.version = Syto::VERSION
|
10
|
+
spec.authors = ['Vadym Lukavyi']
|
11
|
+
spec.email = ['vadym.lukavyi@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Filtering for ActiveRecord models'
|
14
|
+
spec.description = 'Simple filters for RubyOnRails'
|
15
|
+
spec.homepage = 'https://github.com/p9436/syto'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.required_ruby_version = '>= 2.7'
|
19
|
+
|
20
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
21
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
22
|
+
if spec.respond_to?(:metadata)
|
23
|
+
# spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
|
24
|
+
|
25
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
26
|
+
spec.metadata['source_code_uri'] = 'https://github.com/p9436/syto'
|
27
|
+
spec.metadata['changelog_uri'] = 'https://github.com/p9436/syto/CHANGELOG.md'
|
28
|
+
else
|
29
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
30
|
+
'public gem pushes.'
|
31
|
+
end
|
32
|
+
|
33
|
+
# Specify which files should be added to the gem when it is released.
|
34
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
35
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
36
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
37
|
+
end
|
38
|
+
spec.bindir = 'exe'
|
39
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
40
|
+
spec.require_paths = ['lib']
|
41
|
+
|
42
|
+
spec.add_dependency 'activerecord', '>= 5.0'
|
43
|
+
spec.add_dependency 'activesupport', '>= 5.0'
|
44
|
+
|
45
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
46
|
+
spec.add_development_dependency 'minitest-rails'
|
47
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: syto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vadym Lukavyi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description: Simple filters for RubyOnRails
|
84
|
+
email:
|
85
|
+
- vadym.lukavyi@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- lib/syto.rb
|
98
|
+
- lib/syto/version.rb
|
99
|
+
- syto.gemspec
|
100
|
+
homepage: https://github.com/p9436/syto
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata:
|
104
|
+
homepage_uri: https://github.com/p9436/syto
|
105
|
+
source_code_uri: https://github.com/p9436/syto
|
106
|
+
changelog_uri: https://github.com/p9436/syto/CHANGELOG.md
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '2.7'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubygems_version: 3.1.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Filtering for ActiveRecord models
|
126
|
+
test_files: []
|