philter 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 872214969624e598ec099b000b4ada64ac9837f3
4
- data.tar.gz: ba1d3f800772f91b3a8d8b08b06d87a68229d517
3
+ metadata.gz: 9183df24657072d824e87e57a0e1b107bdc08811
4
+ data.tar.gz: c5a611ae4aedb57e1109cee5eb1d6b50f27742af
5
5
  SHA512:
6
- metadata.gz: dc93c8a4a407b0227b0e6795dcd060d01855b83a6f4b8896a1ca2957ae480288eeabecd0d71f8f3f5e254b9cc4cb86fb2a287417856784a899ab6c24ed168a6a
7
- data.tar.gz: 64bc6efbc153e5e41ad99e0eb493046a1f9be0dcd859aa522f11ab423da36877058c2203de70034b42a291300cff791887a3dc5ccff4b1c0d54141c179cf8095
6
+ metadata.gz: 0f0f4ef91025e5e799355c1a18191437a292920d8e19918cb95db3bf328ee8bbbc820bd3694128dbd83f51409897aab3bb6d7cb070eb74f976539b42395a0d6c
7
+ data.tar.gz: a7bbd89b07fe880eee3e47235e7a9c9dd0ae548330c24244bb95fe1375c74a7c6f013b16bdf7e620564e5ab7406f8d3c8a1a88e74d3b54d507161287e35ddfae
@@ -1,3 +1,8 @@
1
+ v1.2.0 [☰](https://github.com/marcomd/Philter/compare/v1.1.0...v1.2.0) March 29th, 2017
2
+ ------------------------------
3
+ * Fixed the behaviour when passed a "get" argument to get an attribute which is also an item attribute. For example: with "get: :size" was returned the size of the item but if that item had an attribute "size" you want to get that attribute in the most cases so now it takes precedence.
4
+ This is not considered a bug so i released a new version.
5
+
1
6
  v1.1.0 [☰](https://github.com/marcomd/Philter/compare/v1.0.0...v1.1.0) May 26th, 2016
2
7
  ------------------------------
3
8
  * Added debug mode to unit tests
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # Phil Ter
1
+ ![](/assets/logo.png)
2
+
2
3
  ## It helps your life and without weighing too much on global warming
3
4
  ### Sometimes it help you to filter some arrays
4
5
 
@@ -6,30 +7,35 @@
6
7
  [![Travis CI ](http://img.shields.io/travis/marcomd/Philter/master.svg) ](https://travis-ci.org/marcomd/Philter)
7
8
  [![Quality ](http://img.shields.io/codeclimate/github/marcomd/Philter.svg)](https://codeclimate.com/github/marcomd/Philter)
8
9
 
9
- This gem let you to filter any kind of arrays to get the item or attributes of selected items.
10
- It's short and dynamic which helps to increase the readability.
11
- Its trace is a usefull tool for teachers.
12
- Performance it's not its field, see the section below.
10
+ Filter any kind of array with the power of ruby, with ease and :smile:
13
11
 
12
+ It's short, dynamic and readable. Its trace is a usefull tool for teachers.
14
13
 
15
- ![](/assets/logo.png)
14
+ The performance is inversely proportional to the comfort although efficiency is improved in the latest versions. See the section below.
16
15
 
17
16
  ```ruby
18
17
  require 'philter'
19
18
 
20
- # Simple arrays
19
+ # You can use everything to filter
20
+ # ...single value
21
21
  [1,2,3].philter 1
22
22
  => [1]
23
23
 
24
+ # ...array of values
24
25
  [1,2,3].philter [2,3]
25
26
  => [2,3]
26
27
 
28
+ # ...operators
27
29
  [1,2,3].philter '<= 2'
28
30
  => [1,2]
29
31
 
30
32
  [1,2,3].philter '!= 2'
31
33
  => [1,3]
32
34
 
35
+ # ...range
36
+ [1,2,3,4,5].philter 2..4
37
+ => [2, 3, 4]
38
+
33
39
  %w[red green blue].philter 'red'
34
40
  => ["red"]
35
41
 
@@ -37,88 +43,92 @@ require 'philter'
37
43
  => ["red", "blue"]
38
44
 
39
45
  # You can pass a block
40
- [1,2,3].philter([1,2]){|e|e*2}
46
+ [1,2,3].philter([1,2]) { |e| e*2 }
41
47
  => [2, 4]
48
+ ```
49
+
50
+ Things get more interesting with array of hashes or objects :yum:
51
+
52
+ ```ruby
53
+ people = [{ id: 1, name: 'Mark', email: 'mark@gmail.com' },
54
+ { id: 2, name: 'Larry', email: 'larry@gmail.com' },
55
+ { id: 3, name: 'Bill', email: 'bill@live.com' }
56
+ ]
57
+
58
+ people.philter id: 1
59
+ => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}]
60
+
61
+ people.philter id: [1,3]
62
+ => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}, {:id=>3, :name=>"Bill", :email=>"bill@live.com"}]
42
63
 
43
- # Array of hashes
44
- [
45
- {id: 1, name: 'Mark' },
46
- {id: 2, name: 'Larry' }
47
- ].philter id: 1
48
- => [{:id=>1, :name=>"Mark"}]
49
-
50
- [
51
- {id: 1, name: 'Mark' },
52
- {id: 2, name: 'Larry' },
53
- {id: 3, name: 'Bill' }
54
- ].philter id: [1,3]
55
- => [{:id=>1, :name=>"Mark"}, {:id=>3, :name=>"Bill"}]
56
-
57
- [
58
- {id: 1, name: 'Mark' },
59
- {id: 2, name: 'Larry' },
60
- {id: 3, name: 'Bill' }
61
- ].philter id: '>2'
62
- => [{:id=>3, :name=>"Bill"}]
64
+ people.philter id: '>2'
65
+ => [{:id=>3, :name=>"Bill", :email=>"bill@live.com"}]
63
66
 
64
67
  # Regular expression
65
- [
66
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
67
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
68
- {id: 3, name: 'Bill', email: 'bill@live.com' }
69
- ].philter email: /@gmail/
70
- => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}, {:id=>2, :name=>"Larry",:email=>"larry@gmail.com"}]
68
+ people.philter email: /@gmail/
69
+ => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}, {:id=>2, :name=>"Larry", :email=>"larry@gmail.com"}]
71
70
 
72
71
  # Select attributes
73
- [
74
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
75
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
76
- {id: 3, name: 'Bill', email: 'bill@live.com' }
77
- ].philter({email: /@gmail/}, get: :name)
72
+ people.philter({ email: /@gmail/ }, get: :name)
78
73
  => ["Mark", "Larry"]
79
74
 
80
75
  # Philter with more attributes -and-
81
- [
82
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
83
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
84
- {id: 3, name: 'Bill', email: 'bill@live.com' }
85
- ].philter name: /M.+/, email: /@gmail/
76
+ people.philter name: /M.+/, email: /@gmail/
86
77
  => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}]
87
78
 
88
79
  # Philter with more attributes -or-
89
- [
90
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
91
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
92
- {id: 3, name: 'Bill', email: 'bill@live.com' }
93
- ].philter({name: /M.+/, email: /@live/}, or: true)
80
+ people.philter({ name: /M.+/, email: /@live/ }, or: true)
94
81
  => [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}, {:id=>3, :name=>"Bill", :email=>"bill@live.com"}]
82
+ ```
83
+
84
+ To me the power! :godmode:
95
85
 
96
- # A bit of magic
86
+ ```ruby
97
87
  # Select and update attributes
98
- [
99
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
100
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
101
- {id: 3, name: 'Bill', email: 'bill@live.com' }
102
- ].philter({email: /@gmail/}){|e| e[:name] << ' use gmail!'}
88
+ regexp = /gmail/
89
+ people.philter(email: regexp) { |e| "#{e[:name]} use #{e[:email].match(regexp)}!"}
103
90
  => ["Mark use gmail!", "Larry use gmail!"]
104
91
 
105
92
  # Add attributes
106
- [
107
- {id: 1, name: 'Mark', email: 'mark@gmail.com' },
108
- {id: 2, name: 'Larry', email: 'larry@gmail.com' },
109
- {id: 3, name: 'Bill', email: 'bill@live.com' }
110
- ].philter({email: /@gmail/}){|e| e[:surname] = 'unknown';e }
93
+ people.philter(email: /@gmail/) do |e|
94
+ e[:filtered] = true
95
+ e
96
+ end
111
97
  => :try_yourself
112
98
  ```
113
99
 
100
+
101
+ Remember how to pass arguments:
102
+
103
+ ```ruby
104
+ people.philter { ...filters... }, { ...options... }
105
+
106
+ # YES
107
+ [1,2,3].philter '<= 2', debug: true
108
+ people.philter({ id: 1 }, debug: true)
109
+
110
+ # NO (debug is not an attribute to filter but an option)
111
+ people.philter(id: 1, debug: true)
112
+ ```
113
+
114
+
114
115
  Get the trace with the option `debug: true`
115
116
 
116
117
  ```ruby
117
- [{id: 1, name: 'Mark', email: 'mark@gmail.com'},
118
- {id: 2, name: 'Bill', email: 'bill@live.com'},
119
- {id: 3, name: 'Larry', email: 'larry@gmail.com'}
120
- ].philter({name: 'Mark', email: /\A.+gmail/}, debug: true)
118
+ [1,2,3].philter '<= 2', debug: true
119
+ --------------- Start debugging philter 1.1.0 -------------
120
+ Search by String: <= 2 with operator
121
+ item Fixnum 1
122
+ item <= value | 1 <= 2 => x
123
+ item Fixnum 2
124
+ item <= value | 2 <= 2 => x
125
+ item Fixnum 3
126
+ item <= value | 3 <= 2
127
+ --------------- End debugging philter 1.1.0 ---------------
128
+ 2 item(s) found
129
+ => [1, 2]
121
130
 
131
+ people.philter({ name: 'Mark', email: /\A.+gmail/ }, debug: true)
122
132
  --------------- Start debugging philter 1.1.0 ---------------
123
133
  Search by Hash:
124
134
 
@@ -152,7 +162,7 @@ Get the trace with the option `debug: true`
152
162
 
153
163
  ### Rails
154
164
 
155
- Rails return relation objects that must be turned to array
165
+ Rails return relation objects that must be turned to array
156
166
 
157
167
  ```ruby
158
168
  cities = City.all.to_a
@@ -166,8 +176,8 @@ cities.philter code: 'PA'
166
176
  => [#<City id: 4, name: "Palermo", code: "PA", region: "Sicilia", created_at: "2016-05-10 09:08:13", updated_at: "2016-05-10 09:08:13">]
167
177
 
168
178
  # Pass a block to select, update or change the result
169
- cities.philter(region: /\Alomb/i){|city| "#{city.name}-#{city.code}"}
170
- => ["Milano-MI", "Lecco-LC", "Pavia-PV", "Piacenza-PC"]
179
+ cities.philter(region: /\Alomb/i) { |city| "#{city.name}-#{city.code}" }
180
+ => ["Milano-MI", "Lecco-LC", "Pavia-PV", "Piacenza-PC", ... [cut]
171
181
 
172
182
  ```
173
183
 
@@ -180,7 +190,7 @@ Ruby 2.2.3p173 on windows 7 with i5 3570K Ivy Bridge @4200 Mhz Ram 16Gb 10-10-10
180
190
  require 'benchmark'
181
191
  require 'philter'
182
192
 
183
- ar_test = 100.times.map{|n| n}
193
+ ar_test = 100.times.map { |n| n }
184
194
  Benchmark.bmbm do |x|
185
195
  x.report("philter: ") { 10_000.times { ar_test.philter 1 } }
186
196
  x.report("grep: ") { 10_000.times { ar_test.grep 1 } }
@@ -220,7 +230,7 @@ grep: 0.172000 0.000000 0.172000 ( 0.182490)
220
230
  ar_search = [1,3,5,7]
221
231
  Benchmark.bmbm do |x|
222
232
  x.report("philter: ") { 10_000.times { ar_test.philter ar_search } }
223
- x.report("select: ") { 10_000.times { ar_test.select{|item| ar_search.include? item } } }
233
+ x.report("select: ") { 10_000.times { ar_test.select { |item| ar_search.include?(item) } } }
224
234
  end
225
235
 
226
236
  #version 1.0.0
@@ -236,8 +246,8 @@ select: 0.078000 0.000000 0.078000 ( 0.073341)
236
246
 
237
247
  ```ruby
238
248
  Benchmark.bmbm do |x|
239
- x.report("philter: ") { 1_000.times { ar_test.philter '< 50' } }
240
- x.report("select: ") { 1_000.times { ar_test.select {|item| item < 50} } }
249
+ x.report("philter: ") { 1_000.times { ar_test.philter '< 50' } }
250
+ x.report("select: ") { 1_000.times { ar_test.select { |item| item < 50 } } }
241
251
  end
242
252
 
243
253
  #version 1.0.0
@@ -251,14 +261,37 @@ philter: 3.744000 0.000000 3.744000 ( 3.746851)
251
261
  select: 0.016000 0.000000 0.016000 ( 0.004338)
252
262
  ```
253
263
 
264
+ Strings
265
+
254
266
  ```ruby
255
- ar_test = [ {id: 1, name: 'Mark', email: 'mark@gmail.com' },
256
- {id: 2, name: 'Bill', email: 'bill@live.com' },
257
- {id: 3, name: 'Larry', email: 'larry@gmail.com' }]
267
+ require 'benchmark'
268
+ require 'philter'
269
+ ar_test = %w(black white grey red green blue yellow orange pink purple violet)
270
+ ar_search = %w(red green blue)
271
+ Benchmark.bmbm do |x|
272
+ x.report("philter: ") { 10_000.times { ar_test.philter ar_search } }
273
+ x.report("select: ") { 10_000.times { ar_test.select { |item| ar_search.include? item } } }
274
+ end
275
+
276
+ #version 1.0.0
277
+ user system total real
278
+ philter: 0.063000 0.000000 0.063000 ( 0.062890)
279
+ select: 0.015000 0.000000 0.015000 ( 0.011407)
280
+
281
+ #version 0.7.0
282
+ user system total real
283
+ philter: 7.363000 0.000000 7.363000 ( 7.359162)
284
+ select: 0.000000 0.000000 0.000000 ( 0.011539)
285
+ ```
286
+
287
+ ```ruby
288
+ ar_test = [ { id: 1, name: 'Mark', email: 'mark@gmail.com' },
289
+ { id: 2, name: 'Bill', email: 'bill@live.com' },
290
+ { id: 3, name: 'Larry', email: 'larry@gmail.com' }]
258
291
  regexp = /\A.+gmail/
259
292
  Benchmark.bmbm do |x|
260
293
  x.report("philter: ") { 10_000.times { ar_test.philter email: regexp } }
261
- x.report("select: ") { 10_000.times { ar_test.select {|item| item[:email] =~ regexp} } }
294
+ x.report("select: ") { 10_000.times { ar_test.select { |item| item[:email] =~ regexp } } }
262
295
  end
263
296
 
264
297
  #version 1.0.0
@@ -272,9 +305,13 @@ philter: 0.468000 0.000000 0.468000 ( 0.473782)
272
305
  select: 0.000000 0.000000 0.000000 ( 0.003429)
273
306
  ```
274
307
 
308
+
309
+
275
310
  ## Compatibility
276
311
 
277
- Ruby `1.9+`
312
+ Ruby `2.1+`
313
+
314
+ Ruby `1.9+` has been tested up to `v1.2.0`
278
315
 
279
316
  ## Install
280
317
 
@@ -286,7 +323,7 @@ To use it in a bundle, add to gem file `gem 'philter'` and run `bundle install`
286
323
 
287
324
  - [x] Add boolean operator to chain of conditions `v1.0.0`
288
325
  - [x] Improve performance keeping the operations's trace `v1.0.0`
289
- - [x] Add block `v1.0.0`
326
+ - [x] Add blocks `v1.0.0`
290
327
  - [ ] Increase performance further
291
328
 
292
329
  ## Contributing
@@ -299,7 +336,7 @@ To use it in a bundle, add to gem file `gem 'philter'` and run `bundle install`
299
336
 
300
337
  ## Testing
301
338
 
302
- Wide coverage with `37 unit tests` and `137 assertions`
339
+ Wide coverage with `39 unit tests` and `155 assertions`
303
340
 
304
341
  To test locally install the development requirements
305
342
 
@@ -307,11 +344,11 @@ To test locally install the development requirements
307
344
 
308
345
  Then execute
309
346
 
310
- bundle exec ruby test\unit_test.rb
347
+ bundle exec ruby test/unit_test.rb
311
348
 
312
349
  Performance tests are calibrated to not exceed 1.2 seconds on my pc with a tolerance become 2 seconds:
313
350
 
314
- bundle exec ruby test\performance_test.rb
351
+ bundle exec ruby test/performance_test.rb
315
352
 
316
353
  ```
317
354
  Loaded suite test/performance_test
@@ -330,7 +367,7 @@ Finished in 8.505 seconds.
330
367
 
331
368
  If you have a very slow pc it could not pass. In this case you can pass a higher tolerance value as argument, for example 3 seconds:
332
369
 
333
- bundle exec ruby test\performance_test.rb 3.0
370
+ bundle exec ruby test/performance_test.rb 3.0
334
371
 
335
372
  ## Found a bug?
336
373
 
File without changes
File without changes
File without changes
File without changes
@@ -92,10 +92,10 @@ module Philter
92
92
 
93
93
  next unless selected
94
94
  results <<
95
- if options[:get] && item.respond_to?(options[:get])
96
- item.send options[:get]
97
- elsif options[:get] && item.respond_to?('[]')
95
+ if options[:get] && item.respond_to?('[]')
98
96
  item[options[:get]]
97
+ elsif options[:get] && item.respond_to?(options[:get])
98
+ item.send options[:get]
99
99
  else
100
100
  item
101
101
  end
@@ -1,5 +1,5 @@
1
1
  module Philter
2
2
  def self.version
3
- "1.1.0"
3
+ "1.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Mastrodonato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-27 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 1.9.3
56
+ version: '2.1'
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - The brave to dare
64
64
  rubyforge_project:
65
- rubygems_version: 2.4.5.1
65
+ rubygems_version: 2.6.10
66
66
  signing_key:
67
67
  specification_version: 4
68
68
  summary: Filter arrays with pleasure