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


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 67

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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 52

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


107
108
109
110
111
112
113
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 107

def delete(path)
  begin
    File.delete(path_to(path))
  rescue Errno::ENOENT
    raise DataNotFoundError.new(path)
  end
end

#delete_dir(path, *options) ⇒ Object


115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 115

def delete_dir(path, *options)
  if options.include?(:recursive)
    if !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)

138
139
140
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 138

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

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)

142
143
144
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 142

def exists_dir?(path)
  File.exists?(path_to(path))
end

#get(path, request = nil) ⇒ Object


82
83
84
85
86
87
88
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 82

def get(path, request=nil)
  begin
    return IO.read(path_to(path))
  rescue Errno::ENOENT
    raise DataNotFoundError.new(path)
  end
end

#list(path) ⇒ Object


130
131
132
133
134
135
136
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 130

def list(path)
  begin
    Dir.entries(path_to(path)).select { |entry| entry != '.' && entry != '..' }.to_a
  rescue Errno::ENOENT
    raise DataNotFoundError.new(path)
  end
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


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef_zero/data_store/raw_file_store.rb', line 90

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