Class: YamlFileStore
- Inherits:
-
Object
- Object
- YamlFileStore
- Defined in:
- lib/detom/yaml_file_store.rb
Constant Summary collapse
- DEFAULT_APP_DIRECTORY =
File.join Dir.home, ".detom"
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each ⇒ Object
-
#initialize(filepath = DEFAULT_APP_DIRECTORY) ⇒ YamlFileStore
constructor
A new instance of YamlFileStore.
- #path ⇒ Object
- #prepare! ⇒ Object
- #save! ⇒ Object
Constructor Details
#initialize(filepath = DEFAULT_APP_DIRECTORY) ⇒ YamlFileStore
Returns a new instance of YamlFileStore.
6 7 8 9 10 |
# File 'lib/detom/yaml_file_store.rb', line 6 def initialize(filepath=DEFAULT_APP_DIRECTORY) @store = {} @filepath = filepath prepare! end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/detom/yaml_file_store.rb', line 29 def [](key) @store[key] end |
#[]=(key, value) ⇒ Object
33 34 35 |
# File 'lib/detom/yaml_file_store.rb', line 33 def []=(key, value) @store[key] = value end |
#each ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/detom/yaml_file_store.rb', line 12 def each enumerator = @store.sort.enum_for loop do case current = enumerator.next when current.kind_of?(Array) then yield **current else yield current end end self end |
#path ⇒ Object
25 26 27 |
# File 'lib/detom/yaml_file_store.rb', line 25 def path @filepath end |
#prepare! ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/detom/yaml_file_store.rb', line 45 def prepare! if root_directory_exists? # load files Dir.chdir(@filepath) do Dir["*"].each do |filename| @store[filename] = YAML.load File.read(File.join(@filepath, filename)) end end else create_root_directory end end |
#save! ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/detom/yaml_file_store.rb', line 37 def save! Dir.chdir(@filepath) do @store.each do |key, value| File.open(File.join(@filepath, key), "w") {|f| YAML.dump(@store[key], f) } end end end |