diff --git a/examples/shared/provider_example.rb b/examples/shared/provider_example.rb index 18090f0..64941f0 100755 --- a/examples/shared/provider_example.rb +++ b/examples/shared/provider_example.rb @@ -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}" } diff --git a/lib/simplefeed/activity/multi_user.rb b/lib/simplefeed/activity/multi_user.rb index 0840484..b3455f5 100644 --- a/lib/simplefeed/activity/multi_user.rb +++ b/lib/simplefeed/activity/multi_user.rb @@ -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 diff --git a/lib/simplefeed/dsl/activities.rb b/lib/simplefeed/dsl/activities.rb index f755d26..1877557 100644 --- a/lib/simplefeed/dsl/activities.rb +++ b/lib/simplefeed/dsl/activities.rb @@ -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) @@ -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 diff --git a/lib/simplefeed/feed.rb b/lib/simplefeed/feed.rb index 8f5567c..86cac9b 100644 --- a/lib/simplefeed/feed.rb +++ b/lib/simplefeed/feed.rb @@ -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) diff --git a/lib/simplefeed/providers/redis/provider.rb b/lib/simplefeed/providers/redis/provider.rb index 5602161..f39b1c7 100644 --- a/lib/simplefeed/providers/redis/provider.rb +++ b/lib/simplefeed/providers/redis/provider.rb @@ -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 diff --git a/spec/support/shared_examples_for_providers.rb b/spec/support/shared_examples_for_providers.rb index 1b30d4b..43b4221 100644 --- a/spec/support/shared_examples_for_providers.rb +++ b/spec/support/shared_examples_for_providers.rb @@ -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) } @@ -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