Class: CouchbaseModelLogging::Logger
- Inherits:
-
Object
- Object
- CouchbaseModelLogging::Logger
- Defined in:
- lib/couchbase_model_logging/logger.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#key_separator ⇒ Object
Returns the value of attribute key_separator.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#string_separator ⇒ Object
Returns the value of attribute string_separator.
Instance Method Summary collapse
- #add(key, hash = { }) ⇒ Object
- #all(key) ⇒ Object
- #decode(str) ⇒ Object
- #delete(key) ⇒ Object
- #encode(hash) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(client, prefix = nil, options = { }) ⇒ Logger
constructor
A new instance of Logger.
- #key?(key) ⇒ Boolean
- #prefixed_key_for(key) ⇒ Object
- #set(key, hashes) ⇒ Object
Constructor Details
permalink #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, = { }) self.client = client self.prefix = prefix self.key_separator = ":" self.string_separator = [:string_separator] || '[SEP]' end |
Instance Attribute Details
permalink #client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/couchbase_model_logging/logger.rb', line 7 def client @client end |
permalink #key_separator ⇒ Object
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 |
permalink #prefix ⇒ Object
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/couchbase_model_logging/logger.rb', line 7 def prefix @prefix end |
permalink #string_separator ⇒ Object
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
permalink #add(key, hash = { }) ⇒ Object
[View source]
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 |
permalink #all(key) ⇒ Object
[View source]
55 56 57 |
# File 'lib/couchbase_model_logging/logger.rb', line 55 def all(key) decode get(key) end |
permalink #decode(str) ⇒ Object
[View source]
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 |
permalink #delete(key) ⇒ Object
[View source]
59 60 61 |
# File 'lib/couchbase_model_logging/logger.rb', line 59 def delete(key) client.delete prefixed_key_for(key) end |
permalink #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 |
permalink #get(key) ⇒ Object
[View source]
46 47 48 |
# File 'lib/couchbase_model_logging/logger.rb', line 46 def get(key) client.get(prefixed_key_for(key), :format => :plain) end |
permalink #key?(key) ⇒ Boolean
16 17 18 |
# File 'lib/couchbase_model_logging/logger.rb', line 16 def key?(key) !get(key).nil? end |
permalink #prefixed_key_for(key) ⇒ Object
[View source]
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 |
permalink #set(key, hashes) ⇒ Object
[View source]
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 |