recollect-array 0.1.2 → 0.1.4

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: b0e4b341fd0128d11eebe856242f5a58e4fe2fde5d52a1b32e5ab7677cb87eb0
4
- data.tar.gz: 9dd9dfb0d30ea7d4a5005f9abda3e2123a6716f52bc7e8b65b4ab4e1b7f2909d
3
+ metadata.gz: 86bf4480af01a28e888309fd260dc30b70d511315fdf5e1f99b76134d55df721
4
+ data.tar.gz: 3cf74cc206a43dfe434d2493ac68c4d4f6da3b78dcb2fbc4ac86472229136c58
5
5
  SHA512:
6
- metadata.gz: f456880a49863da46af5ddf3776a6f3997141207b42b4f327235f9498ce78ad65d056eb20794f8bb690ee6f86a3e4084c947cf186319fc2e3b7d2a5f5eb87127
7
- data.tar.gz: 790347aba973043fd253fb00eff346421c7eb263c0672d3e991b33eaf03ec5a04ea0ea1d0d24a87fb54caf11683bdc698fb395e96718a2130c2ef149fd581607
6
+ metadata.gz: c2b52884d19f896f1b9a82fbc09fbdc0f8a2e3d658d9099c1203dc0222daa29501dd2b0db1092e64ad1e6bccd09d00c37c9e9b09ae09fa5bb4d2c38692991448
7
+ data.tar.gz: ce7d90aab0236d2cd8069d69d0623ff0062b70a1d223dc17f7f122164e48f61ef2e5838f1424b5aabd36a5d00163b7302246f2d712d053f788720e53c85074bb
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.2
1
+ 3.1.2
data/README.md CHANGED
@@ -196,6 +196,16 @@ filters = {
196
196
  collection = Recollect::Array.filter(data, filters)
197
197
  ```
198
198
 
199
+ Using default Equal predicate.
200
+
201
+ ```ruby
202
+ Recollect::Array.filter(data, numbers: 3)
203
+
204
+ Recollect::Array.filter(data, active: true)
205
+
206
+ Recollect::Array.filter(data, id: 3)
207
+ ```
208
+
199
209
  If array, you can navigate into self, using `property.NUMBER.property`
200
210
 
201
211
  ```ruby
@@ -361,7 +371,7 @@ filters = { numbers_in: ['1'] }
361
371
 
362
372
  collection = Recollect::Array.filter(data, filters)
363
373
 
364
- expect(collection.result.size).to eq(1)
374
+ expect(collection.size).to eq(1)
365
375
  ```
366
376
 
367
377
  **Combine conditions**
@@ -5,10 +5,33 @@ module Recollect
5
5
  class Filterable
6
6
  def self.call(data, filters)
7
7
  instance = new(data, filters)
8
+
8
9
  instance.call!
9
- instance
10
+ instance.result
10
11
  end
11
12
 
13
+ PREDICATES = %w[
14
+ eq
15
+ noteq
16
+ not_eq
17
+ cont
18
+ notcont
19
+ not_cont
20
+ lt
21
+ lteq
22
+ gt
23
+ gteq
24
+ start
25
+ notstart
26
+ not_start
27
+ end
28
+ notend
29
+ not_end
30
+ in
31
+ notin
32
+ not_in
33
+ ].freeze
34
+
12
35
  # Available filter
13
36
  attr_accessor :filters
14
37
 
@@ -30,21 +53,28 @@ module Recollect
30
53
 
31
54
  case value
32
55
  when ::Hash
33
- value.each do |predicate, item_value|
56
+ value.each do |predicate, hash_value|
34
57
  klass = Predicate.call(predicate)
35
58
 
36
59
  @result.filter! do |item|
37
- case item_value
60
+ case hash_value
38
61
  when Proc, Module
39
- klass.check!(item, key, item_value.call)
62
+ klass.check!(item, key, hash_value.call)
40
63
  else
41
- klass.check!(item, key, item_value)
64
+ klass.check!(item, key, hash_value)
42
65
  end
43
66
  end
44
67
  end
45
68
  else
46
69
  parts = key.to_s.split('_')
47
- predicate = parts.size == 1 ? :eq : parts.pop
70
+
71
+ predicate = Array(parts[-2..]).filter do |pkey|
72
+ next unless PREDICATES.include? pkey
73
+
74
+ parts = parts - [pkey]
75
+ pkey
76
+ end&.last || :eq
77
+
48
78
  iteratee = parts.join('_')
49
79
  klass = Predicate.call(predicate)
50
80
 
@@ -61,13 +91,25 @@ module Recollect
61
91
 
62
92
  Predicate = lambda do |named|
63
93
  {
64
- eq: Equal, noteq: NotEqual, not_eq: NotEqual,
65
- cont: Contains, notcont: NotContains, not_cont: NotContains,
66
- lt: LessThan, lteq: LessThanEqual,
67
- gt: GreaterThan, gteq: GreaterThanEqual,
68
- start: Startify, notstart: NotStartify, not_start: NotStartify,
69
- end: Endify, notend: NotEndify, not_end: NotEndify,
70
- in: Included, notin: NotIncluded, not_in: NotIncluded
94
+ eq: Equal,
95
+ noteq: NotEqual,
96
+ not_eq: NotEqual,
97
+ cont: Contains,
98
+ notcont: NotContains,
99
+ not_cont: NotContains,
100
+ lt: LessThan,
101
+ lteq: LessThanEqual,
102
+ gt: GreaterThan,
103
+ gteq: GreaterThanEqual,
104
+ start: Startify,
105
+ notstart: NotStartify,
106
+ not_start: NotStartify,
107
+ end: Endify,
108
+ notend: NotEndify,
109
+ not_end: NotEndify,
110
+ in: Included,
111
+ notin: NotIncluded,
112
+ not_in: NotIncluded
71
113
  }[named.to_sym || :eq]
72
114
  end
73
115
  private_constant :Predicate
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Recollect
4
4
  module Array
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.4'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recollect-array
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thadeu Esteves
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2023-02-28 00:00:00.000000000 Z
@@ -131,7 +131,7 @@ homepage: https://github.com/thadeu/recollect-array
131
131
  licenses:
132
132
  - MIT
133
133
  metadata: {}
134
- post_install_message:
134
+ post_install_message:
135
135
  rdoc_options: []
136
136
  require_paths:
137
137
  - lib
@@ -146,8 +146,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubygems_version: 3.1.4
150
- signing_key:
149
+ rubygems_version: 3.3.7
150
+ signing_key:
151
151
  specification_version: 4
152
152
  summary: Simple wrapper to filter array using Ruby and simple predicate conditions
153
153
  test_files: []