Class: ChefZero::DataStore::RawFileStore
- Inherits:
-
InterfaceV2
- Object
- InterfaceV1
- InterfaceV2
- ChefZero::DataStore::RawFileStore
- Defined in:
- lib/chef_zero/data_store/raw_file_store.rb
Instance Attribute Summary collapse
-
#destructible ⇒ Object
readonly
Returns the value of attribute destructible.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #clear ⇒ Object
- #create(path, name, data, *options) ⇒ Object
- #create_dir(path, name, *options) ⇒ Object
- #delete(path) ⇒ Object
- #delete_dir(path, *options) ⇒ Object
- #exists?(path, options = {}) ⇒ Boolean
- #exists_dir?(path) ⇒ Boolean
- #get(path, request = nil) ⇒ Object
-
#initialize(root, destructible = false) ⇒ RawFileStore
constructor
A new instance of RawFileStore.
- #list(path) ⇒ Object
- #path_to(path, name = nil) ⇒ Object
- #set(path, data, *options) ⇒ Object
Methods inherited from InterfaceV2
Methods inherited from InterfaceV1
Constructor Details
#initialize(root, destructible = false) ⇒ RawFileStore
Returns a new instance of RawFileStore.
27 28 29 30 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 27 def initialize(root, destructible = false) @root = root @destructible = destructible end |
Instance Attribute Details
#destructible ⇒ Object (readonly)
Returns the value of attribute destructible.
33 34 35 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 33 def destructible @destructible end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
32 33 34 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 32 def root @root end |
Instance Method Details
#clear ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 43 def clear if destructible Dir.entries(root).each do |entry| next if entry == "." || entry == ".." FileUtils.rm_rf(Path.join(root, entry)) end end end |
#create(path, name, data, *options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 68 def create(path, name, data, *) if .include?(:create_dir) FileUtils.mkdir_p(path_to(path)) end begin File.open(path_to(path, name), File::WRONLY | File::CREAT | File::EXCL | File::BINARY, internal_encoding: nil) do |file| file.write data end rescue Errno::ENOENT raise DataNotFoundError.new(path) rescue Errno::EEXIST raise DataAlreadyExistsError.new(path + [name]) end end |
#create_dir(path, name, *options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 53 def create_dir(path, name, *) real_path = path_to(path, name) if .include?(:recursive) FileUtils.mkdir_p(real_path) else begin Dir.mkdir(File.join(path, name)) rescue Errno::ENOENT raise DataNotFoundError.new(path) rescue Errno::EEXIST raise DataAlreadyExistsError.new(path + [name]) end end end |
#delete(path) ⇒ Object
106 107 108 109 110 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 106 def delete(path) File.delete(path_to(path)) rescue Errno::ENOENT raise DataNotFoundError.new(path) end |
#delete_dir(path, *options) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 112 def delete_dir(path, *) if .include?(:recursive) unless File.exist?(path_to(path)) raise DataNotFoundError.new(path) end FileUtils.rm_rf(path_to(path)) else begin Dir.rmdir(path_to(path)) rescue Errno::ENOENT raise DataNotFoundError.new(path) end end end |
#exists?(path, options = {}) ⇒ Boolean
134 135 136 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 134 def exists?(path, = {}) File.exist?(path_to(path)) end |
#exists_dir?(path) ⇒ Boolean
138 139 140 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 138 def exists_dir?(path) File.exist?(path_to(path)) end |
#get(path, request = nil) ⇒ Object
83 84 85 86 87 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 83 def get(path, request = nil) IO.read(path_to(path)) rescue Errno::ENOENT raise DataNotFoundError.new(path) end |
#list(path) ⇒ Object
128 129 130 131 132 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 128 def list(path) Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a rescue Errno::ENOENT raise DataNotFoundError.new(path) end |
#path_to(path, name = nil) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 35 def path_to(path, name = nil) if name File.join(root, *path, name) else File.join(root, *path) end end |
#set(path, data, *options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 89 def set(path, data, *) if .include?(:create_dir) FileUtils.mkdir_p(path_to(path[0..-2])) end begin mode = File::WRONLY | File::TRUNC | File::BINARY if .include?(:create) mode |= File::CREAT end File.open(path_to(path), mode, internal_encoding: nil) do |file| file.write data end rescue Errno::ENOENT raise DataNotFoundError.new(path) end end |