Sha256: 8fb96661fd5f70399ec48064d576d051030b5ae7ec238ee22416a47fb8922ffe

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

class Kredis::Types::CallbacksProxy
  attr_reader :type
  delegate :to_s, to: :type

  AFTER_CHANGE_OPERATIONS = {
    Kredis::Types::Counter => %i[ increment decrement reset ],
    Kredis::Types::Cycle => %i[ next reset ],
    Kredis::Types::Enum => %i[ value= reset ],
    Kredis::Types::Flag => %i[ mark remove ],
    Kredis::Types::Hash => %i[ update delete []= remove ],
    Kredis::Types::List => %i[ remove prepend append << ],
    Kredis::Types::Scalar => %i[ value= clear ],
    Kredis::Types::Set => %i[ add << remove replace take clear ],
    Kredis::Types::Slots => %i[ reserve release reset ],
    Kredis::Types::UniqueList => %i[ remove prepend append << ]
  }

  def initialize(type, callback)
    @type, @callback = type, callback
  end

  def method_missing(method, *args, **kwargs, &block)
    result = type.send(method, *args, **kwargs, &block)
    invoke_suitable_after_change_callback_for method
    result
  end

  private
    def invoke_suitable_after_change_callback_for(method)
      @callback.call(type) if AFTER_CHANGE_OPERATIONS[type.class]&.include? method
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kredis-1.7.0 lib/kredis/types/callbacks_proxy.rb
kredis-1.6.1 lib/kredis/types/callbacks_proxy.rb
kredis-1.6.0 lib/kredis/types/callbacks_proxy.rb