Class: Ramdo::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/ramdo/store.rb

Constant Summary collapse

NAME_PATTERN =
/^ramdo_([a-z0-9]+)_([0-9]+)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Store

Returns a new instance of Store.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ramdo/store.rb', line 7

def initialize(opts = {})
  list = DiskInstance.list
  disk = nil
  if list.length <= 0
    disk = DiskInstance.create
  else
    disk = list.first
  end

  # Every time a new store is created we check if any other store is out of date
  Cleaner.clean_up(disk)

  ext = opts[:extension] ? opts[:extension].sub('.', '') : 'bin'
  uuid = SecureRandom.hex(4)
  timestamp = Time.now.utc.to_i
  @dir = File.join(disk.path, "ramdo_#{uuid}_#{timestamp}")
  @file = File.join(@dir, "store.#{ext}")

  Dir.mkdir(@dir)

  if opts.has_key?(:data)
    self.data = opts[:data]
  elsif opts.has_key?(:file)
    FileUtils.cp opts[:file], @file
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



5
6
7
# File 'lib/ramdo/store.rb', line 5

def dir
  @dir
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/ramdo/store.rb', line 5

def file
  @file
end

Instance Method Details

#dataObject



38
39
40
# File 'lib/ramdo/store.rb', line 38

def data
  IO.binread(@file)
end

#data=(data) ⇒ Object



34
35
36
# File 'lib/ramdo/store.rb', line 34

def data=(data)
  IO.binwrite(@file, data)
end

#truncateObject



42
43
44
45
# File 'lib/ramdo/store.rb', line 42

def truncate
  return if @dir.empty? || @dir == File::SEPARATOR # Safety net
  FileUtils.rm_r @dir, :force => true
end