philter 0.2.0 → 0.3.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: 28df2d4a59af11ab48898f5a79ae0af51fcb6433
4
- data.tar.gz: d1637407fca0282aaae477558c3070a82277a7b6
3
+ metadata.gz: a82097224d3bcea151e212c83dcd4736f0ed43bd
4
+ data.tar.gz: 54f2e0976d20d9367f954ea333c2649b83517426
5
5
  SHA512:
6
- metadata.gz: c0ae3a03853aeb05c68b16003e8c348284f28d5efee717ce344d0e5b19eafc6973a258d07671cd6e8a62dc82a87f20611d5dcdce0fd631ca7b41df1e475c96ea
7
- data.tar.gz: 4eb0a010060ee387efaa84f8afc12d277b7870429ff10700d0abfb0b5f97baf6ace5b60c21d0460932ff5b5f3a65bf09c9792c62d4c91ea5bf93c8886ddf20fb
6
+ metadata.gz: 5321992c7fe69306f65960b8cac88f4506e1eba6cc1a91a82653305c8e4d3172f6c3b81b465f0a5516e3e4087c2050fc13f414e0501bbfe6e1a2bbd362bfa3c5
7
+ data.tar.gz: 1935bc23a2fbdb11b77b18f4d31a97b935304b48238d54e73e5df161970f3c6f982037a174ae36edca67ad40cffc09fdcf186d0ce8dfb6550af3c04507003ace
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ v0.3.0 [☰](https://github.com/marcomd/Philter/compare/v0.2.0...v0.3.0) May 19th, 2016
2
+ ------------------------------
3
+ * Added operators to work with fixnum: id: '<3' or any other operator
4
+ * Improved documentation
5
+
1
6
  v0.2.0 [☰](https://github.com/marcomd/Philter/compare/v0.1.0...v0.2.0) May 19th, 2016
2
7
  ------------------------------
3
8
  * Added options `:get` to select attributes from hashes and objects
data/README.md CHANGED
@@ -41,6 +41,13 @@ require 'philter'
41
41
  ].philter id: [1,3]
42
42
  => [{:id=>1, :name=>"Mark"}, {:id=>3, :name=>"Bill"}]
43
43
 
44
+ [
45
+ {id: 1, name: 'Mark' },
46
+ {id: 2, name: 'Larry' },
47
+ {id: 3, name: 'Bill' }
48
+ ].philter id: '>2'
49
+ => [{:id=>3, :name=>"Bill"}]
50
+
44
51
  # Regular expression
45
52
  [
46
53
  {id: 1, name: 'Mark', email: 'mark@gmail.com' },
data/lib/array.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Array
2
+ include Philter::Base
2
3
  def philter search=nil, options={}
3
4
  options = {
4
5
  get: nil,
@@ -24,7 +25,7 @@ class Array
24
25
  puts help if options[:debug]
25
26
  raise "Specify search parameter!"
26
27
  end
27
-
28
+ default_operator = '=='
28
29
  results = []
29
30
  self.each do |item|
30
31
  puts "item #{item.class.name} #{item}" if options[:debug]
@@ -47,9 +48,14 @@ class Array
47
48
  print "| #{item.send(label)} =~ #{value}" if options[:debug]
48
49
  selected = item.send(label) =~ value
49
50
  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
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")
53
59
  end
54
60
  elsif item.respond_to? '[]'
55
61
  print " 1.y label: #{label.class.name}" if options[:debug]
@@ -60,9 +66,13 @@ class Array
60
66
  selected = value === item[label].to_s
61
67
  elsif item.is_a?(Hash) && !label.is_a?(Fixnum)
62
68
  # 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
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")
66
76
  else
67
77
  print " .3 no action for #{value} !!!" if options[:debug]
68
78
  end
@@ -99,9 +109,13 @@ class Array
99
109
  else
100
110
  # Search has one item
101
111
  # search: 3 or search: 'a'
102
- print " c. item == search " if options[:debug]
103
- print "| #{item} == #{search}" if options[:debug]
104
- selected = item == search
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")
105
119
  puts " #{'=> X' if selected}" if options[:debug]
106
120
  condition[:at_least_one] ||= selected
107
121
  end
data/lib/base.rb ADDED
@@ -0,0 +1,13 @@
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
data/lib/philter.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative 'version'
2
+ require_relative 'base'
2
3
  require_relative 'array'
3
4
 
4
5
  module Philter
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Philter
2
2
  def self.version
3
- "0.2.0"
3
+ "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Mastrodonato
@@ -36,6 +36,7 @@ files:
36
36
  - LICENSE
37
37
  - README.md
38
38
  - lib/array.rb
39
+ - lib/base.rb
39
40
  - lib/philter.rb
40
41
  - lib/version.rb
41
42
  homepage: https://github.com/marcomd/philter