philter 0.3.0 → 0.4.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: a82097224d3bcea151e212c83dcd4736f0ed43bd
4
- data.tar.gz: 54f2e0976d20d9367f954ea333c2649b83517426
3
+ metadata.gz: 1245b87b0b4e2ca1f90789b959f544411aaa7202
4
+ data.tar.gz: df95daab4acadb6fb075b7d21b8618c0f281ab76
5
5
  SHA512:
6
- metadata.gz: 5321992c7fe69306f65960b8cac88f4506e1eba6cc1a91a82653305c8e4d3172f6c3b81b465f0a5516e3e4087c2050fc13f414e0501bbfe6e1a2bbd362bfa3c5
7
- data.tar.gz: 1935bc23a2fbdb11b77b18f4d31a97b935304b48238d54e73e5df161970f3c6f982037a174ae36edca67ad40cffc09fdcf186d0ce8dfb6550af3c04507003ace
6
+ metadata.gz: ab0e2ffe2422d1babca9c368d33b076eefc7b27d33ac94c76a71917a13edd68737bbd2d4c8850c34bde622f96883e2ff7bdb123b11cc7b60aa9c77978fdbcdb6
7
+ data.tar.gz: 3e0cfffe99f31f60fc7456e3dd81b9344f65c3b1a07b354d4df00726d908c241f3f61d2554bcdd8cfbf4d6da743cc7490f53bd7caf696a0a151f9d04438914db
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ v0.4.0 [☰](https://github.com/marcomd/Philter/compare/v0.3.0...v0.4.0) May 23th, 2016
2
+ ------------------------------
3
+ * Improved code quality
4
+
1
5
  v0.3.0 [☰](https://github.com/marcomd/Philter/compare/v0.2.0...v0.3.0) May 19th, 2016
2
6
  ------------------------------
3
7
  * Added operators to work with fixnum: id: '<3' or any other operator
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  ### Sometimes it help you to filter some arrays
4
4
 
5
5
  [![Version ](https://badge.fury.io/rb/philter.svg) ](https://rubygems.org/gems/philter)
6
- [![Travis CI ](http://img.shields.io/travis/marcomd/philter/master.svg) ](https://travis-ci.org/marcomd/philter)
7
- [![Quality ](http://img.shields.io/codeclimate/github/marcomd/philter.svg)](https://codeclimate.com/github/marcomd/philter)
6
+ [![Travis CI ](http://img.shields.io/travis/marcomd/Philter/master.svg) ](https://travis-ci.org/marcomd/Philter)
7
+ [![Quality ](http://img.shields.io/codeclimate/github/marcomd/Philter.svg)](https://codeclimate.com/github/marcomd/Philter)
8
8
 
9
9
  This gem let you to filter any kind of arrays to get the item or attributes of selected items
10
10
 
data/lib/array.rb CHANGED
@@ -1,151 +1,101 @@
1
- class Array
2
- include Philter::Base
3
- def philter search=nil, options={}
4
- options = {
5
- get: nil,
6
- debug: false
7
- }.merge(options)
8
- help = <<-HELP.gsub(/^ /, '')
9
- *************************************************************************
10
- [].philter 'search', {options}
11
- Examples:
12
- [1,2,3].philter 1 => [1]
13
- [1,2,3].philter [2,3] => [2, 3]
14
- [{id: 1, name: 'Mark'},{id: 2, name: 'Bill'}].philter id: 1
15
- Articles.philter id: 1
16
- People.philter name: 'Mario'
17
- People.philter email: /\A.+@gmail/
18
- Use option get: to select an attribute
19
- People.philter {id: 1}, get: :surname
20
- Use option debug: to watch the selection
21
- Articles.philter {id: 1}, debug: true
22
- *************************************************************************
23
- HELP
24
- unless search
25
- puts help if options[:debug]
26
- raise "Specify search parameter!"
27
- end
28
- default_operator = '=='
29
- results = []
30
- self.each do |item|
31
- puts "item #{item.class.name} #{item}" if options[:debug]
32
- condition = {all: nil, at_least_one: nil}
33
- if search.respond_to? :each
34
- # search: {} or []
35
- search.each do |value|
36
- # Every item must match with search options to be selected
37
- puts " a. search: #{value.class.name} #{value}" if options[:debug]
38
- if value.is_a?(Array)
39
- # Example search: {code: 1} or search: {code: [1,2]}
40
- label, values = value
41
- values = [values] unless values.is_a?(Array)
42
- values.each do |value|
43
- selected = nil
44
- if item.respond_to?(label)
45
- print " 1.x " if options[:debug]
46
- if value.is_a?(Regexp)
47
- print " .1 #{item.class.name}.#{label} =~ value " if options[:debug]
48
- print "| #{item.send(label)} =~ #{value}" if options[:debug]
49
- selected = item.send(label) =~ value
50
- else
51
- # search: {id: 2}
52
- # search: {id: '<3'} or any other operator
53
- operator = get_operator value
54
- tmp_value = operator ? value.gsub(operator,'').to_i : value
55
- operator ||= default_operator
56
- print " .2 #{item.class.name}.#{label} #{operator} value " if options[:debug]
57
- print "| #{item.send(label)} #{operator} #{tmp_value}" if options[:debug]
58
- selected = eval("item.send(label) #{operator} tmp_value")
59
- end
60
- elsif item.respond_to? '[]'
61
- print " 1.y label: #{label.class.name}" if options[:debug]
62
- if value.is_a?(Regexp)
63
- # search: {email: /@gmail/}
64
- print " .1 value === item " if options[:debug]
65
- print "| #{value} === '#{item[label]}'" if options[:debug]
66
- selected = value === item[label].to_s
67
- elsif item.is_a?(Hash) && !label.is_a?(Fixnum)
68
- # search: {id: 1} or {'name' => 'Mark'}
69
- # search: {id: '<3'} or any other operator
70
- operator = get_operator value
71
- tmp_value = operator ? value.gsub(operator,'').to_i : value
72
- operator ||= default_operator
73
- print " .2 #{item.class.name}[:#{label}] #{operator} value " if options[:debug]
74
- print "| #{item[label]} #{operator} #{tmp_value}" if options[:debug]
75
- selected = eval("item[label] #{operator} tmp_value")
76
- else
77
- print " .3 no action for #{value} !!!" if options[:debug]
78
- end
79
- else
80
- print " 1.z selector not present !!!" if options[:debug]
81
- end
82
- puts " #{'=> X' if selected}" if options[:debug]
83
- condition[:at_least_one] ||= selected
84
- end
85
- elsif value.is_a?(Regexp)
86
- # search: {email: /@gmail/}
87
- print " 2. value === item " if options[:debug]
88
- print " | #{value} === '#{item}'" if options[:debug]
89
- selected = value === item.to_s
90
- puts " #{'=> X' if selected}" if options[:debug]
91
- condition[:at_least_one] ||= selected
92
- else
93
- # Example search: [3] or search: [3, 4]
94
- print " 3. item == value " if options[:debug]
95
- print "| #{item} == #{value}" if options[:debug]
96
- selected = item == value
97
- puts " #{'=> X' if selected}" if options[:debug]
98
- condition[:at_least_one] ||= selected
99
- end
100
- end
101
- elsif search.is_a?(Regexp)
102
- # Search has one item
103
- # search: 3 or search: 'a'
104
- print " b. search === item " if options[:debug]
105
- print " | #{search} === '#{item}'" if options[:debug]
106
- selected = search === item.to_s
107
- puts " #{'=> X' if selected}" if options[:debug]
108
- condition[:at_least_one] ||= selected
109
- else
110
- # Search has one item
111
- # search: 3 or search: 'a'
112
- # search: '<3' or any other operator
113
- operator = get_operator search
114
- tmp_search = operator ? search.gsub(operator,'').to_i : search
115
- operator ||= default_operator
116
- print " c. item #{operator} search " if options[:debug]
117
- print "| #{item} #{operator} #{tmp_search}" if options[:debug]
118
- selected = eval("item #{operator} tmp_search")
119
- puts " #{'=> X' if selected}" if options[:debug]
120
- condition[:at_least_one] ||= selected
121
- end
122
- if condition[:at_least_one] || condition[:all]
123
- tmp_result = if options[:get]
124
- item.respond_to?(options[:get]) ? item.send(options[:get]) : item[options[:get]]
125
- else
126
- item
127
- end
128
- results << tmp_result
129
- end
130
- end
131
- puts "------------------" if options[:debug]
132
- puts " #{results ? results.size : 'No'} item#{results && results.size == 1 ? '' : 's'} found" if options[:debug]
133
- results
134
- end
135
-
136
- def phelect arg_fields
137
- fields = arg_fields.split(',').flatten if arg_fields.respond_to?('split')
138
- self.map do |item|
139
- record = {}
140
- fields.each do |field|
141
- if item.respond_to?(field)
142
- record[field.to_sym] = item.send field
143
- elsif item.respond_to?'[]'
144
- record[field.to_sym] = (item[field] || item[field.to_sym])
145
- else
146
- # How do i get the data ?
147
- end
148
- end
149
- end
150
- end
151
- end
1
+ class Array
2
+ include Philter::Base
3
+ def philter search=nil, options={}
4
+ options = {
5
+ get: nil,
6
+ debug: false
7
+ }.merge(options)
8
+ unless search
9
+ puts philter_help if options[:debug]
10
+ raise "Specify search parameter!"
11
+ end
12
+ results = []
13
+ self.each do |item|
14
+ puts "item #{item.class.name} #{item}" if options[:debug]
15
+ h_selected = {all: nil, at_least_one: nil}
16
+ if search.respond_to? :each
17
+ # search: {} or []
18
+ search.each do |value|
19
+ # Every item must match with search options to be selected
20
+ puts " a. search: #{value.class.name} #{value}" if options[:debug]
21
+ if value.is_a?(Array)
22
+ # Example search: {code: 1} or search: {code: [1,2]}
23
+ label, values = value
24
+ values = [values] unless values.is_a?(Array)
25
+ values.each do |value|
26
+ selected = nil
27
+ if item.respond_to?(label)
28
+ print " 1.x " if options[:debug]
29
+ if value.is_a?(Regexp)
30
+ print " .1 " if options[:debug]
31
+ selected = phil_evaluate item, label, value, :method, options.merge(operator: '=~')
32
+ else
33
+ # search: {id: 2}
34
+ # search: {id: '<3'} or any other operator
35
+ print " .2 " if options[:debug]
36
+ selected = phil_evaluate item, label, value, :method, options.merge(operator: '==')
37
+ end
38
+ elsif item.respond_to? '[]'
39
+ print " 1.y label: #{label.class.name}" if options[:debug]
40
+ if value.is_a?(Regexp)
41
+ # search: {email: /@gmail/}
42
+ print " .1 " if options[:debug]
43
+ selected = phil_evaluate item, label, value, :hash, options.merge(operator: '=~')
44
+ elsif item.is_a?(Hash) && !label.is_a?(Fixnum)
45
+ # search: {id: 1} or {'name' => 'Mark'}
46
+ # search: {id: '<3'} or any other operator
47
+ print " .2 " if options[:debug]
48
+ selected = phil_evaluate item, label, value, :hash, options.merge(operator: '==')
49
+ else
50
+ print " .3 no action for #{value} !!!" if options[:debug]
51
+ end
52
+ else
53
+ print " 1.z selector not present !!!" if options[:debug]
54
+ end
55
+ puts " #{'=> X' if selected}" if options[:debug]
56
+ h_selected[:at_least_one] ||= selected
57
+ end
58
+ elsif value.is_a?(Regexp)
59
+ # search: {email: /@gmail/}
60
+ print " 2. " if options[:debug]
61
+ selected = phil_evaluate item, nil, value, :simple, options.merge(operator: '=~')
62
+ puts " #{'=> X' if selected}" if options[:debug]
63
+ h_selected[:at_least_one] ||= selected
64
+ else
65
+ # Example search: [3] or search: [3, 4]
66
+ print " 3. " if options[:debug]
67
+ selected = phil_evaluate item, nil, value, :simple, options.merge(operator: '==')
68
+ puts " #{'=> X' if selected}" if options[:debug]
69
+ h_selected[:at_least_one] ||= selected
70
+ end
71
+ end
72
+ elsif search.is_a?(Regexp)
73
+ # Search has one item
74
+ # search: 3 or search: 'a'
75
+ print " b. " if options[:debug]
76
+ selected = phil_evaluate item, nil, search, :simple, options.merge(operator: '=~')
77
+ puts " #{'=> X' if selected}" if options[:debug]
78
+ h_selected[:at_least_one] ||= selected
79
+ else
80
+ # Search has one item
81
+ # search: 3 or search: 'a'
82
+ # search: '<3' or any other operator
83
+ print " c. " if options[:debug]
84
+ selected = phil_evaluate item, nil, search, :simple, options.merge(operator: '==')
85
+ puts " #{'=> X' if selected}" if options[:debug]
86
+ h_selected[:at_least_one] ||= selected
87
+ end
88
+ if h_selected[:at_least_one] || h_selected[:all]
89
+ tmp_result = if options[:get]
90
+ item.respond_to?(options[:get]) ? item.send(options[:get]) : item[options[:get]]
91
+ else
92
+ item
93
+ end
94
+ results << tmp_result
95
+ end
96
+ end
97
+ puts "------------------" if options[:debug]
98
+ puts " #{results ? results.size : 'No'} item#{results && results.size == 1 ? '' : 's'} found" if options[:debug]
99
+ results
100
+ end
101
+ end
data/lib/base.rb CHANGED
@@ -1,13 +1,61 @@
1
- module Philter
2
- module Base
3
- private
4
- def get_operator exp
5
- regexp = "(?:<=?|>=?|!=|==|===|=~)"
6
- if exp =~ /#{regexp}/
7
- exp.match(/#{regexp}/).to_s
8
- else
9
- nil
10
- end
11
- end
12
- end
13
- end
1
+ module Philter
2
+ module Base
3
+ private
4
+ def philter_help
5
+ <<-HELP.gsub(/^ /, '')
6
+ *************************************************************************
7
+ Philter version #{Philter.version}
8
+ [].philter 'search', {options}
9
+ Examples:
10
+ [1,2,3].philter 1 => [1]
11
+ [1,2,3].philter [2,3] => [2, 3]
12
+ [{id: 1, name: 'Mark'},{id: 2, name: 'Bill'}].philter id: 1
13
+ Articles.philter id: 1
14
+ People.philter name: 'Mario'
15
+ People.philter email: /\A.+@gmail/
16
+ Use option get: to select an attribute
17
+ People.philter {id: 1}, get: :surname
18
+ Use option debug: to watch the selection
19
+ Articles.philter {id: 1}, debug: true
20
+ *************************************************************************
21
+ HELP
22
+ end
23
+
24
+ def get_operator exp
25
+ return unless exp.is_a? String
26
+ regexp = "(?:<=?|>=?|!=|==|===|=~)"
27
+ if exp =~ /#{regexp}/
28
+ exp.match(/#{regexp}/).to_s
29
+ else
30
+ nil
31
+ end
32
+ end
33
+
34
+ def phil_evaluate item, label, value, evaluation, options={}
35
+ options = {
36
+ operator: '==',
37
+ debug: false
38
+ }.merge(options)
39
+ operator = get_operator value
40
+ value = operator ? value.gsub(operator,'').to_i : value
41
+ operator ||= options[:operator]
42
+ item_to_s = operator == '=~' ? '.to_s' : ''
43
+ case evaluation
44
+ when :method
45
+ print " #{item.class.name}.#{label} #{operator} value " if options[:debug]
46
+ print "| #{item.send(label)} #{operator} #{value}" if options[:debug]
47
+ eval("item.send(label) #{operator} value")
48
+ when :hash
49
+ print " #{item.class.name}[:#{label}] #{operator} value" if options[:debug]
50
+ print "| #{item[label]} #{operator} #{value} " if options[:debug]
51
+ eval("item[label]#{item_to_s} #{operator} value")
52
+ when :simple
53
+ print " item #{operator} value" if options[:debug]
54
+ print " | #{item} #{operator} #{value}" if options[:debug]
55
+ eval("item#{item_to_s} #{operator} value")
56
+ else
57
+ raise "phil_evaluate: evaluation #{evaluation ? "#{evaluation} not valid!" : "blank!"}"
58
+ end
59
+ end
60
+ end
61
+ end
data/lib/philter.rb CHANGED
@@ -1,6 +1,6 @@
1
- require_relative 'version'
2
- require_relative 'base'
3
- require_relative 'array'
4
-
5
- module Philter
6
- end
1
+ require_relative 'version'
2
+ require_relative 'base'
3
+ require_relative 'array'
4
+
5
+ module Philter
6
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
- module Philter
2
- def self.version
3
- "0.3.0"
4
- end
5
- end
1
+ module Philter
2
+ def self.version
3
+ "0.4.0"
4
+ end
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: 0.3.0
4
+ version: 0.4.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-19 00:00:00.000000000 Z
11
+ date: 2016-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit