Skip to content

Commit c18118c

Browse files
committed
added support to ex and not_ex
1 parent 022c02a commit c18118c

File tree

7 files changed

+86
-13
lines changed

7 files changed

+86
-13
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ Without configuration, because we use only Ruby. ❤️
6767
| ----------- | ----------- | ----------- |
6868
| Equal | eq | Anywhere |
6969
| NotEqual | noteq | Anywhere |
70+
| Exists | ex | Anywhere |
71+
| NotExists | not_ex | Anywhere |
7072
| Contains | cont | Anywhere |
71-
| NotContains | notcont | Anywhere |
73+
| NotContains | notcont, not_cont | Anywhere |
7274
| Included | in | Anywhere |
73-
| NotIncluded | notin | Anywhere |
74-
| Start | start | Anywhere |
75-
| NotStart | notstart | Anywhere |
75+
| NotIncluded | notin, not_in | Anywhere |
76+
| Start | start, st | Anywhere |
77+
| NotStart | notstart, notst, not_start, not_st | Anywhere |
7678
| End | end | Anywhere |
77-
| NotEnd | notend | Anywhere |
79+
| NotEnd | notend, not_end | Anywhere |
7880
| LessThan | lt | Anywhere |
7981
| LessThanEqual | lteq | Anywhere |
8082
| GreaterThan | gt | Anywhere |

lib/revector.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
require 'set'
44

55
require_relative 'revector/utility'
6+
require_relative 'revector/predicate/equal'
7+
require_relative 'revector/predicate/exists'
68
require_relative 'revector/predicate/startify'
79
require_relative 'revector/predicate/endify'
8-
require_relative 'revector/predicate/equal'
910
require_relative 'revector/predicate/contains'
1011
require_relative 'revector/predicate/in'
1112
require_relative 'revector/predicate/less_than'
@@ -54,6 +55,7 @@ def find_by_hash(key, pair_condition, row)
5455

5556
case right
5657
in [*]
58+
right = [''] unless right.any?
5759
Array(right).any? { |o| predicatable.compare(o, valueable) }
5860
else
5961
predicatable.compare(right, valueable)
@@ -67,7 +69,7 @@ def find_by_other(key, value, row)
6769
predicate = Array(parts[-2..]).filter do |pkey|
6870
next unless PREDICATES.include? pkey
6971

70-
parts = parts - [pkey]
72+
parts -= [pkey]
7173

7274
pkey
7375
end&.last || :eq
@@ -103,21 +105,25 @@ def find_by_other(key, value, row)
103105
not_end: NotEndify,
104106
in: Included,
105107
notin: NotIncluded,
106-
not_in: NotIncluded
108+
not_in: NotIncluded,
109+
ex: Exists,
110+
not_ex: NotExists
107111
}[named.to_sym || :eq]
108112
end
109113
private_constant :Predicate
110114

111115
AFFIRMATIVES = %w[
112-
eq
116+
eq ex
113117
in cont
114118
lt lteq
115119
gt gteq
116-
st start end
120+
st start
121+
end
117122
].freeze
118123

119124
NEGATIVES = %w[
120125
noteq not_eq
126+
not_ex
121127
notcont not_cont
122128
notstart notst not_start not_st
123129
notend not_end

lib/revector/predicate/equal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.check!(item, iteratee, value)
1717
end
1818

1919
def self.compare(value, expected_value)
20-
!Equal.compare(value, expected_value)
20+
!Exists.compare(value) && !Equal.compare(value, expected_value)
2121
end
2222
end
2323
end

lib/revector/predicate/exists.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
class Revector
4+
module Exists
5+
def self.check!(item, iteratee)
6+
compare(Utility::TryFetchOrBlank[item, iteratee])
7+
end
8+
9+
def self.compare(value, other = false)
10+
case value
11+
in [*] | String
12+
value.size.positive?.to_s == other.to_s
13+
else
14+
!!value.to_s == other.to_s
15+
end
16+
end
17+
end
18+
19+
module NotExists
20+
def self.check!(item, iteratee)
21+
!Exists.check!(item, iteratee)
22+
end
23+
24+
def self.compare(value, other = nil)
25+
!Exists.compare(value, other)
26+
end
27+
end
28+
end

lib/revector/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
class Revector
4-
VERSION = '0.1.0'
4+
VERSION = '0.1.1'
55
end

spec/lib/revector/version_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
RSpec.describe Revector do
66
it 'must have a version number' do
7-
expect(described_class::VERSION).to eq('0.1.0')
7+
expect(described_class::VERSION).to eq('0.1.1')
88
end
99
end

spec/lib/revector_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
id: 1,
1010
name: 'Test #1',
1111
12+
contact_id: '1',
1213
schedule: { all_day: true, opened: true },
1314
numbers: %w[1 2],
1415
phones: [
@@ -22,6 +23,7 @@
2223
id: 2,
2324
name: 'Test #2',
2425
26+
contact_id: '2',
2527
schedule: { all_day: false, opened: false },
2628
numbers: %w[3 4],
2729
phones: [],
@@ -32,6 +34,7 @@
3234
id: 3,
3335
name: 'Test #3',
3436
37+
contact_id: '',
3538
schedule: { all_day: false, opened: false },
3639
numbers: %w[5 6],
3740
phones: [
@@ -44,6 +47,40 @@
4447
end
4548

4649
context 'Deep values' do
50+
context 'ex' do
51+
it 'if the value is not empty' do
52+
filters = { 'phones.number': { ex: 'true' } }
53+
54+
collection = described_class.swap(data, filters)
55+
56+
expect(collection.size).to eq(2)
57+
end
58+
59+
it 'if the value is not empty' do
60+
filters = { 'phones.number': { not_ex: 'true' } }
61+
62+
collection = described_class.swap(data, filters)
63+
64+
expect(collection.size).to eq(1)
65+
end
66+
67+
it 'if the value is not empty' do
68+
filters = { contact_id: { ex: 'true' } }
69+
70+
collection = described_class.swap(data, filters)
71+
72+
expect(collection.size).to eq(2)
73+
end
74+
75+
it 'if the value is empty' do
76+
filters = { contact_id: { not_ex: 'true' } }
77+
78+
collection = described_class.swap(data, filters)
79+
80+
expect(collection.size).to eq(1)
81+
end
82+
end
83+
4784
context 'eq' do
4885
context 'array of hash' do
4986
it 'match array values in deep structure' do

0 commit comments

Comments
 (0)