Class: SadieStorageMechanismFile
- Inherits:
-
SadieStorageMechanism
- Object
- SadieStorageMechanism
- SadieStorageMechanismFile
- Defined in:
- lib/storage/file.rb
Overview
require ‘marshal’
Instance Attribute Summary collapse
-
#key_storage_dirpath ⇒ Object
Returns the value of attribute key_storage_dirpath.
Instance Method Summary collapse
- #get(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #has_metadata?(key) ⇒ Boolean
-
#initialize(params = nil) ⇒ SadieStorageMechanismFile
constructor
A new instance of SadieStorageMechanismFile.
- #metadata(key) ⇒ Object
- #set(key, value, params = nil) ⇒ Object
- #unset(key) ⇒ Object
Constructor Details
#initialize(params = nil) ⇒ SadieStorageMechanismFile
Returns a new instance of SadieStorageMechanismFile.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/storage/file.rb', line 7 def initialize( params=nil ) self.key_storage_dirpath = "/tmp/sadie-key-storage" unless params.nil? if params.is_a?( Hash ) if params.has_key?( :key_storage_dirpath ) && ! params[:key_storage_dirpath].nil? self.key_storage_dirpath = params[:key_storage_dirpath] end end end end |
Instance Attribute Details
#key_storage_dirpath ⇒ Object
Returns the value of attribute key_storage_dirpath.
5 6 7 |
# File 'lib/storage/file.rb', line 5 def key_storage_dirpath @key_storage_dirpath end |
Instance Method Details
#get(key) ⇒ Object
38 39 40 41 42 |
# File 'lib/storage/file.rb', line 38 def get( key ) _validate_keystorage_directory value = File.open(_keyvalue_filepath(key), 'rb') { |f| f.read } value end |
#has_key?(key) ⇒ Boolean
49 50 51 52 |
# File 'lib/storage/file.rb', line 49 def has_key?( key ) _validate_keystorage_directory File.exists?(_keyvalue_filepath(key)) end |
#has_metadata?(key) ⇒ Boolean
34 35 36 |
# File 'lib/storage/file.rb', line 34 def ( key ) File.exists? ( key ) end |
#metadata(key) ⇒ Object
28 29 30 31 32 |
# File 'lib/storage/file.rb', line 28 def ( key ) contents = File.open( ( key ), 'rb') { |f| f.read } # puts "contents: #{contents}" Marshal.load( contents ) if ( key ) end |
#set(key, value, params = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/storage/file.rb', line 18 def set( key, value, params=nil ) _validate_keystorage_directory File.open(_keyvalue_filepath(key), 'wb') { |file| file.write(value) } unless params.nil? if params.has_key? :metadata ( key, params[:metadata] ) end end end |
#unset(key) ⇒ Object
44 45 46 47 |
# File 'lib/storage/file.rb', line 44 def unset( key ) _validate_keystorage_directory File.delete(_keyvalue_filepath(key)) end |