Class: CouchbaseModelLogging::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase_model_logging/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, prefix = nil, options = { }) ⇒ Logger

Returns a new instance of Logger.



9
10
11
12
13
14
# File 'lib/couchbase_model_logging/logger.rb', line 9

def initialize(client, prefix = nil, options = { })
  self.client           = client
  self.prefix           = prefix
  self.key_separator    = ":"
  self.string_separator = options[:string_separator] || '[SEP]'
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/couchbase_model_logging/logger.rb', line 7

def client
  @client
end

#key_separatorObject

Returns the value of attribute key_separator.



7
8
9
# File 'lib/couchbase_model_logging/logger.rb', line 7

def key_separator
  @key_separator
end

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'lib/couchbase_model_logging/logger.rb', line 7

def prefix
  @prefix
end

#string_separatorObject

Returns the value of attribute string_separator.



7
8
9
# File 'lib/couchbase_model_logging/logger.rb', line 7

def string_separator
  @string_separator
end

Instance Method Details

#add(key, hash = { }) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/couchbase_model_logging/logger.rb', line 26

def add(key, hash = { })
  yaml     = encode hash
  pref_key = prefixed_key_for key
  begin
    client.append pref_key, yaml, :format => :plain
  rescue ::Couchbase::Error::NotStored
    begin
      client.add pref_key, yaml, :format => :plain
    rescue ::Couchbase::Error::KeyExists
      client.append pref_key, yaml, :format => :plain
    end
  end
end

#all(key) ⇒ Object



55
56
57
# File 'lib/couchbase_model_logging/logger.rb', line 55

def all(key)
  decode get(key)
end

#decode(str) ⇒ Object



50
51
52
53
# File 'lib/couchbase_model_logging/logger.rb', line 50

def decode(str)
  return [] if str.nil? || str.empty?
  str.split(string_separator).map { |yaml| YAML.load yaml }
end

#delete(key) ⇒ Object



59
60
61
# File 'lib/couchbase_model_logging/logger.rb', line 59

def delete(key)
  client.delete prefixed_key_for(key)
end

#encode(hash) ⇒ Object



20
21
22
23
24
# File 'lib/couchbase_model_logging/logger.rb', line 20

def encode(hash)
  yaml = hash.to_yaml
  raise CouchbaseModelLogging::ReplacementError, "Found separator [#{string_separator}] in YAML string" if yaml.index(string_separator)
  yaml + string_separator
end

#get(key) ⇒ Object



46
47
48
# File 'lib/couchbase_model_logging/logger.rb', line 46

def get(key)
  client.get(prefixed_key_for(key), :format => :plain)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/couchbase_model_logging/logger.rb', line 16

def key?(key)
  !get(key).nil?
end

#prefixed_key_for(key) ⇒ Object



63
64
65
# File 'lib/couchbase_model_logging/logger.rb', line 63

def prefixed_key_for(key)
  prefix.nil? ? key.to_s : "#{prefix}#{key_separator}#{key}"
end

#set(key, hashes) ⇒ Object



40
41
42
43
44
# File 'lib/couchbase_model_logging/logger.rb', line 40

def set(key, hashes)
  yaml     = hashes.map { |hash| encode hash }.join("")
  pref_key = prefixed_key_for key
  client.set pref_key, yaml, :format => :plain
end