Skip to content

Commit

Permalink
added support to ex and not_ex
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Dec 12, 2024
1 parent 022c02a commit c18118c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ Without configuration, because we use only Ruby. ❤️
| ----------- | ----------- | ----------- |
| Equal | eq | Anywhere |
| NotEqual | noteq | Anywhere |
| Exists | ex | Anywhere |
| NotExists | not_ex | Anywhere |
| Contains | cont | Anywhere |
| NotContains | notcont | Anywhere |
| NotContains | notcont, not_cont | Anywhere |
| Included | in | Anywhere |
| NotIncluded | notin | Anywhere |
| Start | start | Anywhere |
| NotStart | notstart | Anywhere |
| NotIncluded | notin, not_in | Anywhere |
| Start | start, st | Anywhere |
| NotStart | notstart, notst, not_start, not_st | Anywhere |
| End | end | Anywhere |
| NotEnd | notend | Anywhere |
| NotEnd | notend, not_end | Anywhere |
| LessThan | lt | Anywhere |
| LessThanEqual | lteq | Anywhere |
| GreaterThan | gt | Anywhere |
Expand Down
16 changes: 11 additions & 5 deletions lib/revector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
require 'set'

require_relative 'revector/utility'
require_relative 'revector/predicate/equal'
require_relative 'revector/predicate/exists'
require_relative 'revector/predicate/startify'
require_relative 'revector/predicate/endify'
require_relative 'revector/predicate/equal'
require_relative 'revector/predicate/contains'
require_relative 'revector/predicate/in'
require_relative 'revector/predicate/less_than'
Expand Down Expand Up @@ -54,6 +55,7 @@ def find_by_hash(key, pair_condition, row)

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

parts = parts - [pkey]
parts -= [pkey]

pkey
end&.last || :eq
Expand Down Expand Up @@ -103,21 +105,25 @@ def find_by_other(key, value, row)
not_end: NotEndify,
in: Included,
notin: NotIncluded,
not_in: NotIncluded
not_in: NotIncluded,
ex: Exists,
not_ex: NotExists
}[named.to_sym || :eq]
end
private_constant :Predicate

AFFIRMATIVES = %w[
eq
eq ex
in cont
lt lteq
gt gteq
st start end
st start
end
].freeze

NEGATIVES = %w[
noteq not_eq
not_ex
notcont not_cont
notstart notst not_start not_st
notend not_end
Expand Down
2 changes: 1 addition & 1 deletion lib/revector/predicate/equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.check!(item, iteratee, value)
end

def self.compare(value, expected_value)
!Equal.compare(value, expected_value)
!Exists.compare(value) && !Equal.compare(value, expected_value)
end
end
end
28 changes: 28 additions & 0 deletions lib/revector/predicate/exists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class Revector
module Exists
def self.check!(item, iteratee)
compare(Utility::TryFetchOrBlank[item, iteratee])
end

def self.compare(value, other = false)
case value
in [*] | String
value.size.positive?.to_s == other.to_s
else
!!value.to_s == other.to_s
end
end
end

module NotExists
def self.check!(item, iteratee)
!Exists.check!(item, iteratee)
end

def self.compare(value, other = nil)
!Exists.compare(value, other)
end
end
end
2 changes: 1 addition & 1 deletion lib/revector/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Revector
VERSION = '0.1.0'
VERSION = '0.1.1'
end
2 changes: 1 addition & 1 deletion spec/lib/revector/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

RSpec.describe Revector do
it 'must have a version number' do
expect(described_class::VERSION).to eq('0.1.0')
expect(described_class::VERSION).to eq('0.1.1')
end
end
37 changes: 37 additions & 0 deletions spec/lib/revector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
id: 1,
name: 'Test #1',
email: '[email protected]',
contact_id: '1',
schedule: { all_day: true, opened: true },
numbers: %w[1 2],
phones: [
Expand All @@ -22,6 +23,7 @@
id: 2,
name: 'Test #2',
email: '[email protected]',
contact_id: '2',
schedule: { all_day: false, opened: false },
numbers: %w[3 4],
phones: [],
Expand All @@ -32,6 +34,7 @@
id: 3,
name: 'Test #3',
email: '[email protected]',
contact_id: '',
schedule: { all_day: false, opened: false },
numbers: %w[5 6],
phones: [
Expand All @@ -44,6 +47,40 @@
end

context 'Deep values' do
context 'ex' do
it 'if the value is not empty' do
filters = { 'phones.number': { ex: 'true' } }

collection = described_class.swap(data, filters)

expect(collection.size).to eq(2)
end

it 'if the value is not empty' do
filters = { 'phones.number': { not_ex: 'true' } }

collection = described_class.swap(data, filters)

expect(collection.size).to eq(1)
end

it 'if the value is not empty' do
filters = { contact_id: { ex: 'true' } }

collection = described_class.swap(data, filters)

expect(collection.size).to eq(2)
end

it 'if the value is empty' do
filters = { contact_id: { not_ex: 'true' } }

collection = described_class.swap(data, filters)

expect(collection.size).to eq(1)
end
end

context 'eq' do
context 'array of hash' do
it 'match array values in deep structure' do
Expand Down

0 comments on commit c18118c

Please sign in to comment.