Class: RedisCounters::BaseCounter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/redis_counters/base_counter.rb

Overview

Базовый класс счетчика на основе Redis.

Direct Known Subclasses

HashCounter, UniqueValuesLists::Base

Constant Summary collapse

KEY_DELIMITER =
':'.freeze
VALUE_DELIMITER =
':'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, opts) ⇒ BaseCounter

Public: Конструктор.

см. self.create.

Returns RedisCounters::BaseCounter.



40
41
42
43
44
# File 'lib/redis_counters/base_counter.rb', line 40

def initialize(redis, opts)
  @redis = redis
  @options = opts
  init
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/redis_counters/base_counter.rb', line 13

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/redis_counters/base_counter.rb', line 14

def params
  @params
end

#redisObject (readonly)

Returns the value of attribute redis.



12
13
14
# File 'lib/redis_counters/base_counter.rb', line 12

def redis
  @redis
end

Class Method Details

.create(redis, opts) ⇒ Object

Public: Фабричный метод создания счетчика заданного класса.

redis - Redis - экземпляр redis - клиента. opts - Hash - хеш опций счетчика:

counter_name - Symbol/String - идентификатор счетчика.
key_delimiter - String - разделитель ключа (опционально).
value_delimiter - Array[String] или String, разделитель значений. Если это массив, то первый элемент будет
                  считатся новым разделителем, а второй старым. Все данные будут записываться, используя
                  новый. Старый будет использоваться только для старых данных. Так что если надо сменить
                  делимитр у счётчика, например с ':' на '�', то сперва надо будет установить его на ['�',
                  ':'], а после дампа старых данных в БД, на '�'.

Returns RedisCounters::BaseCounter.



29
30
31
32
# File 'lib/redis_counters/base_counter.rb', line 29

def self.create(redis, opts)
  counter_class = opts.fetch(:counter_class).to_s.constantize
  counter_class.new(redis, opts)
end

Instance Method Details

#nameObject Also known as: id



57
58
59
# File 'lib/redis_counters/base_counter.rb', line 57

def name
  options[:counter_name]
end

#process(params = {}, &block) ⇒ Object

Public: Метод производит обработку события.

params - Hash - хеш параметров события.

Returns process_value result.



52
53
54
55
# File 'lib/redis_counters/base_counter.rb', line 52

def process(params = {}, &block)
  @params = params.with_indifferent_access
  process_value(&block)
end