philter 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +2 -2
- data/lib/array.rb +101 -151
- data/lib/base.rb +61 -13
- data/lib/philter.rb +6 -6
- data/lib/version.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1245b87b0b4e2ca1f90789b959f544411aaa7202
|
4
|
+
data.tar.gz: df95daab4acadb6fb075b7d21b8618c0f281ab76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
7
|
-
[![Quality ](http://img.shields.io/codeclimate/github/marcomd/
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
print " .
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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.
|
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.
|
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-
|
11
|
+
date: 2016-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|