Class: CouchClient::ConsistentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/couch-client/consistent_hash.rb

Overview

ConsistentHash allows indifferent access with either with symbols or strings while also converting symbol values in Arrays or Hashes into strings values.

ConsistentHash only overides methods that already exist in the Hash class; see the documentation in the Ruby core API for clarification and examples.

This code was is heavily influenced by ActiveSupport::HashWithIndifferentAccess.

Direct Known Subclasses

Attachment, AttachmentList, Document, Row

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ConsistentHash

ConsistentHash is constructed with either a Hash or a default value. When a Hash is given, a new ConsistentHash is constructed with the same default value. When any other value is given it will be set as the default value.



13
14
15
16
17
18
19
20
21
22
# File 'lib/couch-client/consistent_hash.rb', line 13

def initialize(hash = {})
  if hash.is_a?(Hash)
    super()
    update(hash).tap do |new_hash|
      new_hash.default = hash.default
    end
  else
    super(hash)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



35
36
37
# File 'lib/couch-client/consistent_hash.rb', line 35

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/couch-client/consistent_hash.rb', line 24

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object



72
73
74
# File 'lib/couch-client/consistent_hash.rb', line 72

def delete(key)
  super(convert_key(key))
end

#dupObject



64
65
66
# File 'lib/couch-client/consistent_hash.rb', line 64

def dup
  ConsistentHash.new(self)
end

#fetch(key, *extras) ⇒ Object



56
57
58
# File 'lib/couch-client/consistent_hash.rb', line 56

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


48
49
50
# File 'lib/couch-client/consistent_hash.rb', line 48

def key?(key)
  super(convert_key(key))
end

#merge(hash) ⇒ Object



68
69
70
# File 'lib/couch-client/consistent_hash.rb', line 68

def merge(hash)
  dup.update(hash)
end

#regular_updateObject



33
# File 'lib/couch-client/consistent_hash.rb', line 33

alias_method :regular_update, :update

#regular_writerObject



32
# File 'lib/couch-client/consistent_hash.rb', line 32

alias_method :regular_writer, :[]=

#to_hashObject



76
77
78
# File 'lib/couch-client/consistent_hash.rb', line 76

def to_hash
  Hash.new(default).merge!(self)
end

#update(other_hash) ⇒ Object Also known as: merge!



39
40
41
42
43
44
# File 'lib/couch-client/consistent_hash.rb', line 39

def update(other_hash)
  other_hash.each_pair do |key, value| 
    regular_writer(convert_key(key), convert_value(value))
  end
  self
end

#values_at(*indices) ⇒ Object



60
61
62
# File 'lib/couch-client/consistent_hash.rb', line 60

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end