syto 0.2.0 → 0.3.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -6
  3. data/lib/syto/version.rb +1 -1
  4. data/lib/syto.rb +36 -24
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af0ab05e31639381af798d997f30b3ae2a9501eedea8e4dd957664470f220b34
4
- data.tar.gz: 306a9a88c49031e7b7c51c35f3ad6be9cfa4db3ac2596b01d33a65380ad98893
3
+ metadata.gz: a2bfcad4813be0e102392f0d0a672df0c51afd81b28698851ccca8570ca97e8e
4
+ data.tar.gz: 779c5ff077f7a176957808f5e8676dba03639e2801b8093fa9e4a6b1f3027656
5
5
  SHA512:
6
- metadata.gz: edb0033f9f3757b4269d0dd0b3ffdb638748c565c01727784df40041e6defa07658c3766ea98078233d8f32bdea6f9d2a5f88d1739bbea7d59f346c7411d842f
7
- data.tar.gz: ad7e6e1dc71ff96a682c1196e4eb41bd18756b5e0e8b5c7961ad55eff53f36b897bf46939830f30a66d96f3a20aa91fba70c7a4aa99d1d44864d6b43285f969a
6
+ metadata.gz: eda3ccca8097c36c48b47dd7e0ea03e7650eb255f1ac79a71293213fa45bc5cb7c3c3203e16a17f3ce46fd29c3a5436556cb8d40cd93a512ed753264daa80603
7
+ data.tar.gz: c48ee3f1cb9416e1e5d6a9d2c06b55f3e50ee523bd7413745c88758925dca2f220ef5337fc18be2025742c519b412bab07fe94dfc911d04e9ef7335135766580
data/README.md CHANGED
@@ -28,10 +28,12 @@ Define filters for attributes `country`, `area_id` and `rate`
28
28
  # app/model/user.rb
29
29
  class User < ActiveModel
30
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
31
+ syto_attrs_map :active, # filter by users.active
32
+ country: { case_insensitive: true }, # allows to filter by 'users.country'
33
+ region: { field: :area_id }, # allows to filter by 'users.area_id' with "region" key in params
34
+ rate: { type: :range }, # allows to filter by 'users.rate' with "rate_from" and "rate_to" keys in params
35
+ date: { type: :range, field: :created_at,
36
+ key_from: :start_date, key_to: :end_date } # filter by 'users.created_at'
35
37
  end
36
38
  ```
37
39
 
@@ -51,25 +53,35 @@ Tip: There are 3 methods available in Syto for use in `extended_filters`:
51
53
  ```ruby
52
54
  # app/models/concerns/post_filter.rb
53
55
  class PostFilters < Syto
56
+ # map for converting params { author: 52, strat_date: '2020-01-01', end_date: '2021-12-31' }
57
+ # to query like WHERE user_id = 52 AND created_at BETWEEN '2020-01-01' AND '2021-12-31'
58
+ filters_attrs_map author: :user_id,
59
+ date: { field: :created_at, type: :range, key_from: :start_date, key_to: :end_date }
60
+
54
61
  def extended_filters
55
62
  # base_class contains Post
56
63
  return if params[:published].blank?
57
64
 
58
65
  self.result = result.where(published: params[:published])
59
- filter_by_range(:published_at, key_from: :pub_from, key_to: :pub_to)
66
+ filter_by_range(:published, field: :published_at, key_from: :pub_from, key_to: :pub_to)
60
67
  end
61
68
  end
62
69
  ```
63
70
 
64
71
  Use in code:
65
72
 
73
+ ```ruby
74
+ params = { author: 21, start_date: '2022-01-01' }
75
+ User.filter_by(params) # where user_id = 21 and created_at >= '2022-01-01'
76
+ ```
77
+
66
78
  ```ruby
67
79
  params = { country: 'UA', rate_from: 2, rate_to: 3 }
68
80
  User.filter_by(params)
69
81
  ```
70
82
 
71
83
  ```ruby
72
- params = { published: true, pub_from: '2021-01-01' }
84
+ params = { pub_from: '2021-01-01' }
73
85
  Post.filter_by(params) # select published posts from 2021
74
86
  ```
75
87
 
data/lib/syto/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Syto
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/syto.rb CHANGED
@@ -131,47 +131,56 @@ module Syto
131
131
 
132
132
  # Filter by attribute
133
133
  #
134
- # @param [String. Symbol] attr
135
- # @param [Hash] params_key
134
+ # @param [String, Symbol] key
135
+ # @param [String, Symbol, NilClass] field_name
136
136
  #
137
- def scalar_filter(attr, params_key = nil)
138
- params_key ||= attr
139
- params_key = params_key.to_sym
140
- return unless @params[params_key]
137
+ def scalar_filter(key, field_name = nil)
138
+ return unless @params[key]
141
139
 
142
- @result = @result.where(attr => @params[params_key])
140
+ field_name ||= key
141
+ @result = @result.where(field_name => @params[key])
143
142
  end
144
143
 
145
144
  # Filter by attribute with options
146
145
  #
147
- # @param [Hash] attrs
146
+ # @param [Hash] keys
148
147
  #
149
- def hash_filter(attrs)
150
- attrs.each do |attr, options|
148
+ def hash_filter(keys)
149
+ keys.each do |key, options|
151
150
  case options
152
151
  when String, Symbol
153
- scalar_filter attr, options
152
+ scalar_filter key, options
154
153
  when Hash
155
- filter_hash_options(attr, options)
154
+ filter_hash_options(key, options)
156
155
  end
157
156
  end
158
157
  end
159
158
 
160
- def filter_hash_options(attr, options)
161
- filter_by_value attr, options if options.key?(:key)
162
- filter_by_range attr, options if options.key?(:key_from) && options.key?(:key_to)
159
+ def filter_hash_options(key, options)
160
+ puts key
161
+ puts options
162
+ if options[:type] == :range
163
+ filter_by_range key, options
164
+ else
165
+ filter_by_value key, options
166
+ end
163
167
  end
164
168
 
165
169
  # Filter by single value
166
170
  #
167
- # @param [Symbol] field_name
171
+ # @param [Symbol] key
168
172
  # @param [Hash] options
169
173
  #
170
- def filter_by_value(field_name, options = {})
174
+ # available options:
175
+ # - field
176
+ # - case_insensitive
177
+ #
178
+ def filter_by_value(key, options = {})
179
+ return unless @params.key?(key)
180
+
171
181
  options ||= {}
172
182
 
173
- key ||= options[:key] || field_name
174
- return unless @params.key?(key)
183
+ field_name = options[:field] || key
175
184
 
176
185
  value = @params[key]
177
186
 
@@ -184,17 +193,20 @@ module Syto
184
193
 
185
194
  # Filter by range
186
195
  #
187
- # @param [Symbol] field_name
196
+ # @param [Symbol] key
188
197
  # @param [Hash{Symbol->Object}] options
189
198
  #
190
- # options:
199
+ # available options:
200
+ # - field
191
201
  # - key_from,
192
202
  # - key_to,
193
203
  #
194
- def filter_by_range(field_name, options = {})
195
- key_from = options[:key_from] || :"#{field_name}_from"
196
- key_to = options[:key_to] || :"#{field_name}_to"
204
+ def filter_by_range(key, options = {})
205
+ options ||= {}
197
206
 
207
+ key_from = options[:key_from] || :"#{key}_from"
208
+ key_to = options[:key_to] || :"#{key}_to"
209
+ field_name = options[:field] || key
198
210
  value_from = @params[key_from]
199
211
  value_to = @params[key_to]
200
212
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadym Lukavyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-12 00:00:00.000000000 Z
11
+ date: 2022-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord