class Kayvee::Key

Represents a key that can hold a value.

Attributes

path[R]

The path of the key @todo this should be named key, probably

Public Class Methods

new(client, path, value = nil) click to toggle source

@param [Kayvee::Clients::KeyValueStoreClient] client the client to use to back the key @param [String] path the path of the key @param [String] value the value of the key

# File lib/kayvee/key.rb, line 11
def initialize(client, path, value = nil)
  @client = client
  @path = path
  @value = value
  @mutex = Mutex.new
end

Public Instance Methods

exists?() click to toggle source
# File lib/kayvee/key.rb, line 18
def exists?
  begin
    @client.exists?(@path)
  rescue ::S3::Error::NoSuchKey => e
    nil
  end
end
read() click to toggle source

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

@return [Stringnil] the value of the key or nil if key does not exist

# File lib/kayvee/key.rb, line 35
def read
  @mutex.synchronize do
    @value ||= @client.read(@path)
    @value
  end
end
url() click to toggle source

Returns a public url for the given path

# File lib/kayvee/key.rb, line 27
def url
  @mutex.synchronize { @client.url(@path) }
end
write(contents) click to toggle source

Writes a contents string to the key

@param [String] contents the contents to set in the key

@return the modified or created key

# File lib/kayvee/key.rb, line 47
def write(contents)
  @mutex.synchronize do
    @value = contents
    @client.write(@path, @value)
    self
  end
end