Class: Kayvee::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/kayvee/key.rb

Overview

Represents a key that can hold a value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, value = nil) ⇒ Key

Returns a new instance of Key.

Parameters:



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

#pathObject (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

Returns:

  • (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

#readString\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

Returns:

  • (String\nil)

    the value of the key or nil if key does not exist



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

#urlObject

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

Parameters:

  • contents (String)

    the contents to set in the key

Returns:

  • the modified or created 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