Class: ChefZero::DataStore::RawFileStore

Inherits:
InterfaceV2 show all
Defined in:
lib/chef_zero/data_store/raw_file_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from InterfaceV2

#interface_version

Methods inherited from InterfaceV1

#interface_version

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

#destructibleObject (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

#rootObject (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

#clearObject



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, *options)
  if options.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, *options)
  real_path = path_to(path, name)
  if options.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, *options)
  if options.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

Returns:

  • (Boolean)


134
135
136
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 134

def exists?(path, options = {})
  File.exist?(path_to(path))
end

#exists_dir?(path) ⇒ Boolean

Returns:

  • (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, *options)
  if options.include?(:create_dir)
    FileUtils.mkdir_p(path_to(path[0..-2]))
  end
  begin
    mode = File::WRONLY | File::TRUNC | File::BINARY
    if options.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