Class: Tamarillo::Storage
- Inherits:
-
Object
- Object
- Tamarillo::Storage
- Defined in:
- lib/tamarillo/storage.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns: the Config for this storage.
-
#path ⇒ Object
readonly
Returns: the String path to the storage directory.
Instance Method Summary collapse
-
#clear_monitor ⇒ Object
Public: Removes monitor pid-file.
-
#initialize(path, config = nil) ⇒ Storage
constructor
Public: Initialize a new storage object.
-
#latest ⇒ Object
Public: Returns a Tomato instance if one exists.
-
#read_monitor ⇒ Object
Public: Returns the pid of monitor.
-
#read_tomato(path) ⇒ Object
Public: Read a tomato from the filesystem.
-
#write_config ⇒ Object
Public: Write the config to the filesystem.
-
#write_monitor(monitor) ⇒ Object
Public: Writes a monitor pid.
-
#write_tomato(tomato) ⇒ Object
Public: Write a tomato to the filesystem.
Constructor Details
#initialize(path, config = nil) ⇒ Storage
Public: Initialize a new storage object.
22 23 24 25 26 |
# File 'lib/tamarillo/storage.rb', line 22 def initialize(path, config = nil) @config = config || Tamarillo::Config.new @path = Pathname.new(path) FileUtils.mkdir_p(@path) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns: the Config for this storage. Used to set the duration when reading tomatoes in.
19 20 21 |
# File 'lib/tamarillo/storage.rb', line 19 def config @config end |
#path ⇒ Object (readonly)
Returns: the String path to the storage directory.
16 17 18 |
# File 'lib/tamarillo/storage.rb', line 16 def path @path end |
Instance Method Details
#clear_monitor ⇒ Object
Public: Removes monitor pid-file.
84 85 86 |
# File 'lib/tamarillo/storage.rb', line 84 def clear_monitor File.delete(monitor_path) if File.exists?(monitor_path) end |
#latest ⇒ Object
Public: Returns a Tomato instance if one exists.
89 90 91 92 93 94 95 96 97 |
# File 'lib/tamarillo/storage.rb', line 89 def latest return unless File.directory?(tomato_dir) # p Dir.glob(tomato_dir.join('*')) # XXX tomato_dir.to_s because FakeFS chokes on Pathname. if latest_name = Dir.glob(tomato_dir.join('*')).sort.last read_tomato(latest_name) end end |
#read_monitor ⇒ Object
Public: Returns the pid of monitor.
77 78 79 80 81 |
# File 'lib/tamarillo/storage.rb', line 77 def read_monitor if File.exists?(monitor_path) File.read(monitor_path).to_i end end |
#read_tomato(path) ⇒ Object
Public: Read a tomato from the filesystem.
path - A String path to a tomato file.
Returns a Tomato instance if found, nil if not found.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/tamarillo/storage.rb', line 61 def read_tomato(path) return unless File.exist?(path) data = File.readlines(path) start_time = Time.iso8601(data[0]).localtime state = data[2] clock = Clock.new(start_time) duration = config.duration_in_seconds tomato = Tomato.new(duration, clock) tomato.interrupt! if state == 'interrupted' tomato end |
#write_config ⇒ Object
Public: Write the config to the filesystem.
Returns the Pathname to the config that was written.
31 32 33 |
# File 'lib/tamarillo/storage.rb', line 31 def write_config File.open(config_path, 'w') { |f| f << @config.to_yaml } end |
#write_monitor(monitor) ⇒ Object
Public: Writes a monitor pid.
monitor - The monitor to write.
52 53 54 |
# File 'lib/tamarillo/storage.rb', line 52 def write_monitor(monitor) File.open(monitor_path, 'w') { |f| f << monitor.pid } end |
#write_tomato(tomato) ⇒ Object
Public: Write a tomato to the filesystem.
tomato - A Tomato instance.
Returns the Pathname to the tomato that was written.
40 41 42 43 44 45 46 47 |
# File 'lib/tamarillo/storage.rb', line 40 def write_tomato(tomato) tomato_file = TomatoFile.new(tomato) tomato_path = @path.join(tomato_file.path) FileUtils.mkdir_p(File.dirname(tomato_path)) File.open(tomato_path, 'w') { |f| f << tomato_file.content } tomato_path end |