philter 0.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 +7 -0
- data/CHANGELOG.md +8 -0
- data/LICENSE +15 -0
- data/README.md +135 -0
- data/lib/array.rb +137 -0
- data/lib/philter.rb +5 -0
- data/lib/version.rb +5 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28df2d4a59af11ab48898f5a79ae0af51fcb6433
|
4
|
+
data.tar.gz: d1637407fca0282aaae477558c3070a82277a7b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0ae3a03853aeb05c68b16003e8c348284f28d5efee717ce344d0e5b19eafc6973a258d07671cd6e8a62dc82a87f20611d5dcdce0fd631ca7b41df1e475c96ea
|
7
|
+
data.tar.gz: 4eb0a010060ee387efaa84f8afc12d277b7870429ff10700d0abfb0b5f97baf6ace5b60c21d0460932ff5b5f3a65bf09c9792c62d4c91ea5bf93c8886ddf20fb
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
v0.2.0 [☰](https://github.com/marcomd/Philter/compare/v0.1.0...v0.2.0) May 19th, 2016
|
2
|
+
------------------------------
|
3
|
+
* Added options `:get` to select attributes from hashes and objects
|
4
|
+
* Improved documentation
|
5
|
+
|
6
|
+
v0.1.0 May 19th, 2016
|
7
|
+
------------------------------
|
8
|
+
* Starting project
|
data/LICENSE
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Philter
|
2
|
+
|
3
|
+
Copyright (C) 2016 Marco Mastrodonato, m.mastrodonato@gmail.com
|
4
|
+
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
6
|
+
it under the terms of the GNU General Public License as published by
|
7
|
+
the Free Software Foundation, either version 3 of the License.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
data/README.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# Phil Ter
|
2
|
+
## It helps your life and without weighing too much on global warming
|
3
|
+
### Sometimes it help you to filter some arrays
|
4
|
+
|
5
|
+
[ ](https://rubygems.org/gems/philter)
|
6
|
+
[ ](https://travis-ci.org/marcomd/philter)
|
7
|
+
[](https://codeclimate.com/github/marcomd/philter)
|
8
|
+
|
9
|
+
This gem let you to filter any kind of arrays to get the item or attributes of selected items
|
10
|
+
|
11
|
+
|
12
|
+

|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require 'philter'
|
16
|
+
|
17
|
+
# Simple arrays
|
18
|
+
[1,2,3].philter 1
|
19
|
+
=> [1]
|
20
|
+
|
21
|
+
[1,2,3].philter [2,3]
|
22
|
+
=> [2,3]
|
23
|
+
|
24
|
+
%w[red green blue].philter 'red'
|
25
|
+
=> ["red"]
|
26
|
+
|
27
|
+
%w[red green blue].philter %w(red blue)
|
28
|
+
=> ["red", "blue"]
|
29
|
+
|
30
|
+
# Array of hashes
|
31
|
+
[
|
32
|
+
{id: 1, name: 'Mark' },
|
33
|
+
{id: 2, name: 'Larry' }
|
34
|
+
].philter id: 1
|
35
|
+
=> [{:id=>1, :name=>"Mark"}]
|
36
|
+
|
37
|
+
[
|
38
|
+
{id: 1, name: 'Mark' },
|
39
|
+
{id: 2, name: 'Larry' },
|
40
|
+
{id: 3, name: 'Bill' }
|
41
|
+
].philter id: [1,3]
|
42
|
+
=> [{:id=>1, :name=>"Mark"}, {:id=>3, :name=>"Bill"}]
|
43
|
+
|
44
|
+
# Regular expression
|
45
|
+
[
|
46
|
+
{id: 1, name: 'Mark', email: 'mark@gmail.com' },
|
47
|
+
{id: 2, name: 'Larry', email: 'larry@gmail.com' },
|
48
|
+
{id: 3, name: 'Bill', email: 'bill@live.com' }
|
49
|
+
].philter email: /@gmail/
|
50
|
+
=> [{:id=>1, :name=>"Mark", :email=>"mark@gmail.com"}, {:id=>2, :name=>"Larry",:email=>"larry@gmail.com"}]
|
51
|
+
|
52
|
+
# Select attributes
|
53
|
+
[
|
54
|
+
{id: 1, name: 'Mark', email: 'mark@gmail.com' },
|
55
|
+
{id: 2, name: 'Larry', email: 'larry@gmail.com' },
|
56
|
+
{id: 3, name: 'Bill', email: 'bill@live.com' }
|
57
|
+
].philter({email: /@gmail/}, get: :name)
|
58
|
+
=> ["Mark", "Larry"]
|
59
|
+
|
60
|
+
# Debug mode
|
61
|
+
[
|
62
|
+
{id: 1, name: 'Mark' },
|
63
|
+
{id: 2, name: 'Larry' },
|
64
|
+
{id: 3, name: 'Bill' }
|
65
|
+
].philter({id: [1,3]}, debug: true)
|
66
|
+
|
67
|
+
item Hash {:id=>1, :name=>"Mark"}
|
68
|
+
a. search: Array [:id, [1, 3]]
|
69
|
+
1.y label: Symbol .2 Hash[:id] == value | 1 == 1 => X
|
70
|
+
1.y label: Symbol .2 Hash[:id] == value | 1 == 3
|
71
|
+
item Hash {:id=>2, :name=>"Larry"}
|
72
|
+
a. search: Array [:id, [1, 3]]
|
73
|
+
1.y label: Symbol .2 Hash[:id] == value | 2 == 1
|
74
|
+
1.y label: Symbol .2 Hash[:id] == value | 2 == 3
|
75
|
+
item Hash {:id=>3, :name=>"Bill"}
|
76
|
+
a. search: Array [:id, [1, 3]]
|
77
|
+
1.y label: Symbol .2 Hash[:id] == value | 3 == 1
|
78
|
+
1.y label: Symbol .2 Hash[:id] == value | 3 == 3 => X
|
79
|
+
------------------
|
80
|
+
2 items found
|
81
|
+
=> [{:id=>1, :name=>"Mark"}, {:id=>3, :name=>"Bill"}]
|
82
|
+
```
|
83
|
+
|
84
|
+
### Rails
|
85
|
+
|
86
|
+
Rails return relation objects that must be turned to array
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
cities = City.all.to_a
|
90
|
+
City Load (1.0ms) SELECT "cities".* FROM "cities"
|
91
|
+
=> ... [cut]
|
92
|
+
|
93
|
+
cities.philter id: 1
|
94
|
+
=> [#<City id: 1, name: "Milano", code: "MI", region: "Lombardia", created_at: "2016-05-10 09:07:22", updated_at: "2016-05-10 09:07:22">]
|
95
|
+
|
96
|
+
cities.philter code: 'PA'
|
97
|
+
=> [#<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">]
|
98
|
+
|
99
|
+
cities.philter(region: /\Alomb/i).size
|
100
|
+
=> 4
|
101
|
+
```
|
102
|
+
|
103
|
+
## Compatibility
|
104
|
+
|
105
|
+
Ruby `1.9+`
|
106
|
+
|
107
|
+
## Install
|
108
|
+
|
109
|
+
gem install philter
|
110
|
+
|
111
|
+
To use it in a rails project, add to gem file `gem 'philter'` and run `bundle install`
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
1. Fork it
|
116
|
+
2. Create your feature branch (`git checkout -b my-feature`)
|
117
|
+
3. Commit your changes (`git commit -am 'I made extensive use of all my creativity'`)
|
118
|
+
4. Push to the branch (`git push origin my-feature`)
|
119
|
+
5. Create new Pull Request
|
120
|
+
|
121
|
+
## Testing
|
122
|
+
|
123
|
+
bundle install
|
124
|
+
|
125
|
+
bundle exec ruby test\philter_test.rb
|
126
|
+
|
127
|
+
## Found a bug?
|
128
|
+
|
129
|
+
Please open an issue.
|
130
|
+
|
131
|
+
|
132
|
+
## License
|
133
|
+
|
134
|
+
The GNU Lesser General Public License, version 3.0 (LGPL-3.0)
|
135
|
+
See LICENSE file
|
data/lib/array.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
class Array
|
2
|
+
def philter search=nil, options={}
|
3
|
+
options = {
|
4
|
+
get: nil,
|
5
|
+
debug: false
|
6
|
+
}.merge(options)
|
7
|
+
help = <<-HELP.gsub(/^ /, '')
|
8
|
+
*************************************************************************
|
9
|
+
[].philter 'search', {options}
|
10
|
+
Examples:
|
11
|
+
[1,2,3].philter 1 => [1]
|
12
|
+
[1,2,3].philter [2,3] => [2, 3]
|
13
|
+
[{id: 1, name: 'Mark'},{id: 2, name: 'Bill'}].philter id: 1
|
14
|
+
Articles.philter id: 1
|
15
|
+
People.philter name: 'Mario'
|
16
|
+
People.philter email: /\A.+@gmail/
|
17
|
+
Use option get: to select an attribute
|
18
|
+
People.philter {id: 1}, get: :surname
|
19
|
+
Use option debug: to watch the selection
|
20
|
+
Articles.philter {id: 1}, debug: true
|
21
|
+
*************************************************************************
|
22
|
+
HELP
|
23
|
+
unless search
|
24
|
+
puts help if options[:debug]
|
25
|
+
raise "Specify search parameter!"
|
26
|
+
end
|
27
|
+
|
28
|
+
results = []
|
29
|
+
self.each do |item|
|
30
|
+
puts "item #{item.class.name} #{item}" if options[:debug]
|
31
|
+
condition = {all: nil, at_least_one: nil}
|
32
|
+
if search.respond_to? :each
|
33
|
+
# search: {} or []
|
34
|
+
search.each do |value|
|
35
|
+
# Every item must match with search options to be selected
|
36
|
+
puts " a. search: #{value.class.name} #{value}" if options[:debug]
|
37
|
+
if value.is_a?(Array)
|
38
|
+
# Example search: {code: 1} or search: {code: [1,2]}
|
39
|
+
label, values = value
|
40
|
+
values = [values] unless values.is_a?(Array)
|
41
|
+
values.each do |value|
|
42
|
+
selected = nil
|
43
|
+
if item.respond_to?(label)
|
44
|
+
print " 1.x " if options[:debug]
|
45
|
+
if value.is_a?(Regexp)
|
46
|
+
print " .1 #{item.class.name}.#{label} =~ value " if options[:debug]
|
47
|
+
print "| #{item.send(label)} =~ #{value}" if options[:debug]
|
48
|
+
selected = item.send(label) =~ value
|
49
|
+
else
|
50
|
+
print " .2 #{item.class.name}.#{label} == value " if options[:debug]
|
51
|
+
print "| #{item.send(label)} == #{value}" if options[:debug]
|
52
|
+
selected = item.send(label) == value
|
53
|
+
end
|
54
|
+
elsif item.respond_to? '[]'
|
55
|
+
print " 1.y label: #{label.class.name}" if options[:debug]
|
56
|
+
if value.is_a?(Regexp)
|
57
|
+
# search: {email: /@gmail/}
|
58
|
+
print " .1 value === item " if options[:debug]
|
59
|
+
print "| #{value} === '#{item[label]}'" if options[:debug]
|
60
|
+
selected = value === item[label].to_s
|
61
|
+
elsif item.is_a?(Hash) && !label.is_a?(Fixnum)
|
62
|
+
# search: {id: 1} or {'name' => 'Mark'}
|
63
|
+
print " .2 #{item.class.name}[:#{label}] == value " if options[:debug]
|
64
|
+
print "| #{item[label]} == #{value}" if options[:debug]
|
65
|
+
selected = item[label] == value
|
66
|
+
else
|
67
|
+
print " .3 no action for #{value} !!!" if options[:debug]
|
68
|
+
end
|
69
|
+
else
|
70
|
+
print " 1.z selector not present !!!" if options[:debug]
|
71
|
+
end
|
72
|
+
puts " #{'=> X' if selected}" if options[:debug]
|
73
|
+
condition[:at_least_one] ||= selected
|
74
|
+
end
|
75
|
+
elsif value.is_a?(Regexp)
|
76
|
+
# search: {email: /@gmail/}
|
77
|
+
print " 2. value === item " if options[:debug]
|
78
|
+
print " | #{value} === '#{item}'" if options[:debug]
|
79
|
+
selected = value === item.to_s
|
80
|
+
puts " #{'=> X' if selected}" if options[:debug]
|
81
|
+
condition[:at_least_one] ||= selected
|
82
|
+
else
|
83
|
+
# Example search: [3] or search: [3, 4]
|
84
|
+
print " 3. item == value " if options[:debug]
|
85
|
+
print "| #{item} == #{value}" if options[:debug]
|
86
|
+
selected = item == value
|
87
|
+
puts " #{'=> X' if selected}" if options[:debug]
|
88
|
+
condition[:at_least_one] ||= selected
|
89
|
+
end
|
90
|
+
end
|
91
|
+
elsif search.is_a?(Regexp)
|
92
|
+
# Search has one item
|
93
|
+
# search: 3 or search: 'a'
|
94
|
+
print " b. search === item " if options[:debug]
|
95
|
+
print " | #{search} === '#{item}'" if options[:debug]
|
96
|
+
selected = search === item.to_s
|
97
|
+
puts " #{'=> X' if selected}" if options[:debug]
|
98
|
+
condition[:at_least_one] ||= selected
|
99
|
+
else
|
100
|
+
# Search has one item
|
101
|
+
# search: 3 or search: 'a'
|
102
|
+
print " c. item == search " if options[:debug]
|
103
|
+
print "| #{item} == #{search}" if options[:debug]
|
104
|
+
selected = item == search
|
105
|
+
puts " #{'=> X' if selected}" if options[:debug]
|
106
|
+
condition[:at_least_one] ||= selected
|
107
|
+
end
|
108
|
+
if condition[:at_least_one] || condition[:all]
|
109
|
+
tmp_result = if options[:get]
|
110
|
+
item.respond_to?(options[:get]) ? item.send(options[:get]) : item[options[:get]]
|
111
|
+
else
|
112
|
+
item
|
113
|
+
end
|
114
|
+
results << tmp_result
|
115
|
+
end
|
116
|
+
end
|
117
|
+
puts "------------------" if options[:debug]
|
118
|
+
puts " #{results ? results.size : 'No'} item#{results && results.size == 1 ? '' : 's'} found" if options[:debug]
|
119
|
+
results
|
120
|
+
end
|
121
|
+
|
122
|
+
def phelect arg_fields
|
123
|
+
fields = arg_fields.split(',').flatten if arg_fields.respond_to?('split')
|
124
|
+
self.map do |item|
|
125
|
+
record = {}
|
126
|
+
fields.each do |field|
|
127
|
+
if item.respond_to?(field)
|
128
|
+
record[field.to_sym] = item.send field
|
129
|
+
elsif item.respond_to?'[]'
|
130
|
+
record[field.to_sym] = (item[field] || item[field.to_sym])
|
131
|
+
else
|
132
|
+
# How do i get the data ?
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/lib/philter.rb
ADDED
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: philter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marco Mastrodonato
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description: This gem let you to filter any kind of arrays to get the item or attributes
|
28
|
+
of selected items
|
29
|
+
email:
|
30
|
+
- m.mastrodonato@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- CHANGELOG.md
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- lib/array.rb
|
39
|
+
- lib/philter.rb
|
40
|
+
- lib/version.rb
|
41
|
+
homepage: https://github.com/marcomd/philter
|
42
|
+
licenses:
|
43
|
+
- LGPL-3.0
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.9.3
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements:
|
60
|
+
- The brave to dare
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.4.5.1
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Filter arrays with pleasure
|
66
|
+
test_files: []
|