Class: Kayvee::Key
- Inherits:
-
Object
- Object
- Kayvee::Key
- Defined in:
- lib/kayvee/key.rb
Overview
Represents a key that can hold a value.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
The path of the key.
Instance Method Summary collapse
- #exists? ⇒ Boolean
-
#initialize(client, path, value = nil) ⇒ Key
constructor
A new instance of Key.
-
#read ⇒ String\nil
Reads the value of the key, if the value is been read it returns it.
-
#url ⇒ Object
Returns a public url for the given path.
-
#write(contents) ⇒ Object
Writes a contents string to the key.
Constructor Details
#initialize(client, path, value = nil) ⇒ Key
Returns a new instance of Key.
11 12 13 14 15 16 |
# File 'lib/kayvee/key.rb', line 11 def initialize(client, path, value = nil) @client = client @path = path @value = value @mutex = Mutex.new end |
Instance Attribute Details
#path ⇒ Object (readonly)
TODO:
this should be named key, probably
The path of the key
6 7 8 |
# File 'lib/kayvee/key.rb', line 6 def path @path end |
Instance Method Details
#exists? ⇒ Boolean
18 19 20 21 22 23 24 |
# File 'lib/kayvee/key.rb', line 18 def exists? begin @client.exists?(@path) rescue ::S3::Error::NoSuchKey => e nil end end |
#read ⇒ String\nil
Reads the value of the key, if the value is been read it returns it. If the value has not yet been read it reaches out to the client
35 36 37 38 39 40 |
# File 'lib/kayvee/key.rb', line 35 def read @mutex.synchronize do @value ||= @client.read(@path) @value end end |
#url ⇒ Object
Returns a public url for the given path
27 28 29 |
# File 'lib/kayvee/key.rb', line 27 def url @mutex.synchronize { @client.url(@path) } end |
#write(contents) ⇒ Object
Writes a contents string to the key
47 48 49 50 51 52 53 |
# File 'lib/kayvee/key.rb', line 47 def write(contents) @mutex.synchronize do @value = contents @client.write(@path, @value) self end end |