Class: RedisCounters::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_counters/bucket.rb

Direct Known Subclasses

Cluster, Partition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counter, bucket_params) ⇒ Bucket

Returns a new instance of Bucket.



10
11
12
13
14
15
16
17
# File 'lib/redis_counters/bucket.rb', line 10

def initialize(counter, bucket_params)
  @counter = counter
  @bucket_params = bucket_params.with_indifferent_access

  if bucket_keys.present? && bucket_params.blank? && required?
    raise ArgumentError, "You must specify a #{self.class.name}"
  end
end

Instance Attribute Details

#bucket_paramsObject (readonly)

Returns the value of attribute bucket_params.



20
21
22
# File 'lib/redis_counters/bucket.rb', line 20

def bucket_params
  @bucket_params
end

#counterObject (readonly)

Returns the value of attribute counter.



19
20
21
# File 'lib/redis_counters/bucket.rb', line 19

def counter
  @counter
end

Class Method Details

.default_optionsObject



6
7
8
# File 'lib/redis_counters/bucket.rb', line 6

def self.default_options
  {:only_leaf => false}
end

Instance Method Details

#params(options = {}) ⇒ Object

Protected: Возвращает букет в виде массива параметров, однозначно его идентифицирующих.

cluster - Hash - хеш параметров, определяющий букет. options - Hash - хеш опций:

:only_leaf - Boolean - 

Метод генерирует исключение ArgumentError, если переданы параметры не верно идентифицирующие букет. Например: ключи группировки счетчика :param2, :param3, а переданы :param3. Метод генерирует исключение ArgumentError, ‘You must specify a cluster’, если букет передан в виде пустого хеша, но группировка используется в счетчике.

Returns Array.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redis_counters/bucket.rb', line 37

def params(options = {})
  options.reverse_merge!(self.class.default_options)

  bucket_keys.inject(Array.new) do |result, key|
    param = (options[:only_leaf] ? bucket_params.fetch(key) : bucket_params[key])
    next result unless bucket_params.has_key?(key)
    next result << param if result.size >= bucket_keys.index(key)

    raise ArgumentError, 'An incorrectly specified %s %s' % [self.class.name, bucket_params]
  end
end