Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/shared/provider_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ def p(*args)
store('value four') { p 'storing', 'value four' }

color_dump
puts
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }

header 'deleting "value three" via delete_if'

header 'deleting'
delete_block = ->(event, _consumer) {
event&.value == 'value three'
}

delete('value three') { p 'deleting', 'value three' }
delete_if(activity_block: delete_block)

total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }

hr

header 'deleting "value four"'
delete('value four') { p 'deleting', 'value four' }
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }
Expand Down
2 changes: 1 addition & 1 deletion lib/simplefeed/activity/multi_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MultiUser < Base
opts.delete(:event)
end
response = instance.feed.send(method, *args, **opts, &block)
block&.call(response)
block&.call(response) unless method.to_sym == :delete_if
raise StandardError, "Nil response from provider #{instance.feed.provider&.provider&.class}, method #{method}(#{opts})" unless response

response
Expand Down
9 changes: 7 additions & 2 deletions lib/simplefeed/dsl/activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ def initialize(activity, **opts)
# activity.store(**opts)
# end
# etc...
SimpleFeed::Providers.define_provider_methods(self) do |instance, method, *args, **opts, &block|
SimpleFeed::Providers.define_provider_methods(self) do |instance,
method,
*args,
activity_block: nil,
**opts,
&block|
if args&.first
arg1 = args.shift
if arg1.is_a?(SimpleFeed::Event)
Expand All @@ -42,7 +47,7 @@ def initialize(activity, **opts)

response = instance.instance_eval do
print_debug_info(method, **opts) do
activity.send(method, *args, **opts)
activity.send(method, *args, **opts, &activity_block)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/simplefeed/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Feed

attr_reader :name

SimpleFeed::Providers.define_provider_methods(self) do |feed, method, opts, &block|
feed.provider.send(method, **opts, &block)
SimpleFeed::Providers.define_provider_methods(self) do |feed, method, *args, **opts, &block|
feed.provider.send(method, *args, **opts, &block)
end

def initialize(name)
Expand Down
6 changes: 3 additions & 3 deletions lib/simplefeed/providers/redis/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def delete(user_ids:, value:, **)
end
end

def delete_if(user_ids:)
raise ArgumentError, '#delete_if must be called with a block that receives (user_id, event) as arguments.' unless block_given?
def delete_if(user_ids:, &block)
raise ArgumentError, '#delete_if must be called with a block that receives (event_id, user_id) as arguments.' if block.nil?

with_response_batched(user_ids) do |key|
fetch(user_ids: [key.consumer])[key.consumer].map do |event|
with_redis do |redis|
if yield(event, key.consumer)
if block.call(event, key.consumer)
redis.zrem(key.data, event.value) ? event : nil
end
end
Expand Down
21 changes: 12 additions & 9 deletions spec/support/shared_examples_for_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ def ensure_descending(r)
end

context '#delete' do
it('with event as an argument') do
it 'with event as an argument' do
with_activity(activity, events: events) do
delete(events.first) { |r| expect(r).to eq(true) }
total_count { |r| expect(r).to eq(1) }
end
end
it('with event value as an argument') do

it 'with event value as an argument' do
with_activity(activity, events: events) do
delete(events.first.value) { |r| expect(r).to eq(true) }
total_count { |r| expect(r).to eq(1) }
Expand All @@ -100,25 +101,27 @@ def ensure_descending(r)

context '#delete_if' do
let(:activity) { feed.activity(user_id) }

it 'should delete events that match' do
before do
activity.wipe
events.each do |event|
expect(activity.store(event: event)).to eq(true)
end
expect(activity.total_count).to eq(3)
end

it 'should delete events that match' do
deleted_events = activity.delete_if do |event_to_delete, *|
event_to_delete == events.first
event_to_delete == events.last
end
expect(activity.total_count).to eq(2)
expect(deleted_events).to eq([events.first])
expect(activity.fetch).to include(events.last)
expect(activity.fetch).not_to include(events.first)
expect(deleted_events).to eq([events.last])
expect(activity.fetch).to include(events.first)
expect(activity.fetch).not_to include(events.last)
end
end

context 'hitting #max_size of the feed' do
it('pushes the oldest one out') do
it 'pushes the oldest one out' do
with_activity(activity, events: events) do
wipe
# The next one resets the time
Expand Down