class CouchbaseModelLogging::Logger
Attributes
client[RW]
key_separator[RW]
prefix[RW]
string_separator[RW]
Public Class Methods
new(client, prefix = nil, options = { })
click to toggle source
# 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
Public Instance Methods
add(key, hash = { })
click to toggle source
# 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)
click to toggle source
# File lib/couchbase_model_logging/logger.rb, line 55 def all(key) decode get(key) end
decode(str)
click to toggle source
# 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)
click to toggle source
# File lib/couchbase_model_logging/logger.rb, line 59 def delete(key) client.delete prefixed_key_for(key) end
encode(hash)
click to toggle source
# 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)
click to toggle source
# File lib/couchbase_model_logging/logger.rb, line 46 def get(key) client.get(prefixed_key_for(key), :format => :plain) end
key?(key)
click to toggle source
# File lib/couchbase_model_logging/logger.rb, line 16 def key?(key) !get(key).nil? end
prefixed_key_for(key)
click to toggle source
# 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)
click to toggle source
# 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