Skip to content

Commit

Permalink
added suppor to Callable value
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Feb 24, 2023
1 parent 760bab6 commit 8ff51ed
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,49 @@ collection = Recollect::Array.filter(data, filters)
# [{ schedules: [{ opened: true, all_day: true }, { opened: false, all_day: true }] }]
```

Amazing, you can pass a Callable value as value, like this.

```ruby
filters = {
'schedules.[0].opened': {
eq: -> { true }
}
}
# OR a Module
module ActiveTruthy
def self.call = true
end
module NumbersAvailable
def self.call = %w[1 2]
end
filters = {
'schedules.[0].opened': {
eq: ActiveTruthy
},
numbers: {
in: NumbersAvailable
}
}
# OR a Class
class ActiveFalsey
def self.call = false
end
filters = {
'schedules.[0].opened': {
eq: ActiveFalsey
}
}
collection = Recollect::Array.filter(data, filters)
```

**Combine conditions**

Yes, you can combine one or multiple predicates to filter you array.
Expand Down
7 changes: 6 additions & 1 deletion lib/recollect/filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def __apply_filter_callback
klass = Predicate.call(predicate)

@result.filter! do |item|
klass.check!(item, key, item_value)
case item_value
when Proc, Module
klass.check!(item, key, item_value.call)
else
klass.check!(item, key, item_value)
end
end
end
else
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/recollect/filterable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@
end

context 'when value is Hash contains predicates' do
context 'and hash value is Callable' do
it 'returns only filters items' do
filters = {
active: {
eq: -> { true }
}
}

collection = Recollect::Filterable.call(data, filters)

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

it 'returns only filters items' do
filters = {
active: { eq: ActiveTruthy },
numbers: { in: NumbersAvailable }
}

collection = Recollect::Filterable.call(data, filters)

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

it 'returns only filters items' do
filters = {
active: {
eq: ActiveFalsey
}
}

collection = Recollect::Filterable.call(data, filters)

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

it 'returns only filters items' do
filters = { active: { eq: true } }

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'bundler/setup'
require 'recollect'

Dir["#{File.expand_path(__dir__)}/support/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand Down
13 changes: 13 additions & 0 deletions spec/support/callable/filters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module ActiveTruthy
def self.call = true
end

module ActiveFalsey
def self.call = false
end

module NumbersAvailable
def self.call = %w[1 2]
end

0 comments on commit 8ff51ed

Please sign in to comment.