Skip to content

Commit

Permalink
feature(all) introduce key and value delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
Napolskih committed Sep 12, 2013
1 parent addbb40 commit 0a176fa
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
16 changes: 16 additions & 0 deletions lib/redis_counters/base_counter.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# coding: utf-8
require 'forwardable'
require 'active_support/core_ext/class/attribute'

module RedisCounters

class BaseCounter
extend Forwardable

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

class_attribute :key_delimiter
class_attribute :value_delimiter

self.key_delimiter = KEY_DELIMITER
self.value_delimiter = VALUE_DELIMITER

attr_reader :redis
attr_reader :options
Expand Down Expand Up @@ -38,6 +46,14 @@ def counter_name
@counter_name ||= options.fetch(:counter_name)
end

def key_delimiter
@key_delimiter ||= options.fetch(:key_delimiter, self.class.key_delimiter)
end

def value_delimiter
@value_delimiter ||= options.fetch(:value_delimiter, self.class.value_delimiter)
end

def_delegator :redis, :multi, :transaction
end

Expand Down
4 changes: 2 additions & 2 deletions lib/redis_counters/hash_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def process_value
end

def key
[counter_name, partition].flatten.join(KEY_DELIMITER)
[counter_name, partition].flatten.join(key_delimiter)
end

def partition
Expand All @@ -31,7 +31,7 @@ def partition
def field
group_params = group_keys.map { |key| params.fetch(key) }
group_params << field_name if field_name.present?
group_params.join(KEY_DELIMITER)
group_params.join(value_delimiter)
end

def field_name
Expand Down
2 changes: 1 addition & 1 deletion lib/redis_counters/unique_hash_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def unique_values_list_options
end

def unique_values_list_name
[counter_name, UNIQUE_LIST_POSTFIX].join(KEY_DELIMITER)
[counter_name, UNIQUE_LIST_POSTFIX].join(key_delimiter)
end

def unique_values_list_class
Expand Down
4 changes: 2 additions & 2 deletions lib/redis_counters/unique_values_lists/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Base < RedisCounters::BaseCounter
protected

def key(partition = partition_params)
[counter_name, group_params, partition].flatten.compact.join(KEY_DELIMITER)
[counter_name, group_params, partition].flatten.compact.join(key_delimiter)
end

def group_params
Expand All @@ -23,7 +23,7 @@ def partition_params

def value
value_params = value_keys.map { |key| params.fetch(key) }
value_params.join(KEY_DELIMITER)
value_params.join(value_delimiter)
end

def use_partitions?
Expand Down
8 changes: 4 additions & 4 deletions lib/redis_counters/unique_values_lists/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def partitions
return (@partitions = [nil]) unless use_partitions?

@partitions = redis.smembers(partitions_list_key).map do |partition|
partition.split(KEY_DELIMITER)
partition.split(key_delimiter)
end
.delete_if(&:empty?)
end
Expand All @@ -73,15 +73,15 @@ def add_partition
end

def partitions_list_key
[counter_name, group_params, PARTITIONS_LIST_POSTFIX].flatten.join(KEY_DELIMITER)
[counter_name, group_params, PARTITIONS_LIST_POSTFIX].flatten.join(key_delimiter)
end

def current_partition
partition_params.flatten.join(KEY_DELIMITER)
partition_params.flatten.join(key_delimiter)
end

def new_partition?
!partitions.include?(current_partition.split(KEY_DELIMITER))
!partitions.include?(current_partition.split(key_delimiter))
end
end

Expand Down

0 comments on commit 0a176fa

Please sign in to comment.