Class: Hold::File::HashRepository
- Inherits:
-
Object
- Object
- Hold::File::HashRepository
- Includes:
- HashRepository
- Defined in:
- lib/hold/file/hash_repository.rb
Overview
A simple HashRepository (ie key/value store implementation) which stores each key in a separate file.
-
Keys must be suitable pathnames
-
Values must be strings
-
base_path should end with a /, or keys should start with a /, one or the other
-
subdirectories will be created as required if the keys contain path separators
-
watch out for per-directory file limits.
NB: Not threadsafe for writes
Instance Method Summary collapse
- #can_get_class?(klass) ⇒ Boolean
- #can_set_class?(klass) ⇒ Boolean
- #clear_key(key) ⇒ Object
- #get_with_key(key) ⇒ Object
-
#initialize(base_path) ⇒ HashRepository
constructor
A new instance of HashRepository.
- #key?(key) ⇒ Boolean (also: #has_key?)
- #path_to_key(key) ⇒ Object
- #set_with_key(key, value) ⇒ Object
Methods included from HashRepository
#get_many_with_keys, #key_cell
Constructor Details
#initialize(base_path) ⇒ HashRepository
Returns a new instance of HashRepository.
29 30 31 |
# File 'lib/hold/file/hash_repository.rb', line 29 def initialize(base_path) @base_path = base_path end |
Instance Method Details
#can_get_class?(klass) ⇒ Boolean
21 22 23 |
# File 'lib/hold/file/hash_repository.rb', line 21 def can_get_class?(klass) klass == String end |
#can_set_class?(klass) ⇒ Boolean
25 26 27 |
# File 'lib/hold/file/hash_repository.rb', line 25 def can_set_class?(klass) klass == String end |
#clear_key(key) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/hold/file/hash_repository.rb', line 52 def clear_key(key) path = path_to_key(key) begin ::File.unlink(path) rescue Errno::ENOENT end end |
#get_with_key(key) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/hold/file/hash_repository.rb', line 44 def get_with_key(key) path = path_to_key(key) begin ::File.read(path) rescue Errno::ENOENT end end |
#key?(key) ⇒ Boolean Also known as: has_key?
60 61 62 |
# File 'lib/hold/file/hash_repository.rb', line 60 def key?(key) ::File.exist?(path_to_key(key)) end |
#path_to_key(key) ⇒ Object
33 34 35 |
# File 'lib/hold/file/hash_repository.rb', line 33 def path_to_key(key) "#{@base_path}#{key}" end |
#set_with_key(key, value) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/hold/file/hash_repository.rb', line 37 def set_with_key(key, value) path = path_to_key(key) FileUtils.mkdir_p(::File.dirname(path)) ::File.open(path, 'wb') { |file| file.write(value.to_s) } value end |