Class: Persistable::FSAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/persistable/fs_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FSAdapter

Returns a new instance of FSAdapter.



6
7
8
# File 'lib/persistable/fs_adapter.rb', line 6

def initialize(options = {})
  @storage_location = options[:storage_location] || "/tmp"
end

Instance Attribute Details

#storage_locationObject (readonly)

Returns the value of attribute storage_location.



4
5
6
# File 'lib/persistable/fs_adapter.rb', line 4

def storage_location
  @storage_location
end

Instance Method Details

#delete(persistable) ⇒ Object



22
23
24
25
26
# File 'lib/persistable/fs_adapter.rb', line 22

def delete(persistable)
  if File.exists?(path = "#{storage_location}/#{persistable.persistence_key}")
    File.delete("#{storage_location}/#{persistable.persistence_key}")
  end
end

#read(persistable) ⇒ Object



16
17
18
19
20
# File 'lib/persistable/fs_adapter.rb', line 16

def read(persistable)
  if File.exists?(path = "#{storage_location}/#{persistable.persistence_key}")
    persistable.persistence_data = File.open(path)
  end
end

#write(persistable) ⇒ Object



10
11
12
13
14
# File 'lib/persistable/fs_adapter.rb', line 10

def write(persistable)
  File.open("#{storage_location}/#{persistable.persistence_key}", "w") do |file|
    file.write persistable.persistence_data.read
  end
end