Class: ActiveRemote::Cached::ArgumentKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/active_remote/cached/argument_keys.rb

Constant Summary collapse

REMOVE_CHARACTERS =
/[[:space:]+=><{}\[\];:\-,]/
REPLACE_MAP =

Covers the same characters as REMOVE_CHARACTERS. A tab or a newline left in a key breaks the Memcached protocol.

{
  ' ' => 'SP',
  "\t" => 'TB',
  "\n" => 'NL',
  "\r" => 'CR',
  "\f" => 'FF',
  "\v" => 'VT',
  '+' => 'PL',
  '=' => 'EQ',
  '>' => 'GT',
  '<' => 'LT',
  '{' => 'LB',
  '}' => 'RB',
  '[' => 'LB2',
  ']' => 'RB2',
  ';' => 'SC',
  ':' => 'CO',
  '-' => 'DA',
  ',' => 'COM'
}.freeze
REPLACE_CHARACTERS =
::Regexp.union(REPLACE_MAP.keys)
FIELD_SEPARATOR =

The separators below are absent from both REMOVE_CHARACTERS and REPLACE_MAP, so they survive either option. escape_value/1 escapes them inside a value, which makes the key one-to-one with the arguments.

'.'
PAIR_SEPARATOR =
'/'
ESCAPE_MAP =
{ '%' => '%25', FIELD_SEPARATOR => '%2E', PAIR_SEPARATOR => '%2F' }.freeze
ESCAPE_CHARACTERS =
%r{[%./]}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments, options) ⇒ ArgumentKeys

Returns a new instance of ArgumentKeys.



65
66
67
68
69
# File 'lib/active_remote/cached/argument_keys.rb', line 65

def initialize(*arguments, options)
  @options = options
  @arguments = arguments.flatten.compact
  @argument_string = @arguments.join
end

Instance Attribute Details

#argument_stringObject (readonly)

Returns the value of attribute argument_string.



6
7
8
# File 'lib/active_remote/cached/argument_keys.rb', line 6

def argument_string
  @argument_string
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



6
7
8
# File 'lib/active_remote/cached/argument_keys.rb', line 6

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/active_remote/cached/argument_keys.rb', line 6

def options
  @options
end

Class Method Details

.for_fields(field_names, values, options) ⇒ Object

Build a key that names each field, so that two finders with the same values do not share one cache entry.

for_fields([:alpha, :beta], ['x', 'y'], {}).cache_key
# => "alpha.x/beta.y"


47
48
49
50
51
52
53
# File 'lib/active_remote/cached/argument_keys.rb', line 47

def self.for_fields(field_names, values, options)
  pairs = field_names.each_with_index.map do |field_name, index|
    "#{field_name}#{FIELD_SEPARATOR}#{normalize_value(values[index])}"
  end

  new(pairs.join(PAIR_SEPARATOR), options)
end

Instance Method Details

#cache_keyObject



71
72
73
74
75
76
77
# File 'lib/active_remote/cached/argument_keys.rb', line 71

def cache_key
  return @argument_string.gsub(REMOVE_CHARACTERS, '') if remove_characters?
  return @argument_string unless replace_characters?

  # One pass, rather than one gsub for each entry in the map.
  @argument_string.gsub(REPLACE_CHARACTERS, REPLACE_MAP)
end

#to_sObject



79
80
81
# File 'lib/active_remote/cached/argument_keys.rb', line 79

def to_s
  cache_key
end