Class: ChefZero::DataStore::MemoryStoreV2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from InterfaceV2

#interface_version

Methods inherited from InterfaceV1

#interface_version

Constructor Details

#initializeMemoryStoreV2

Returns a new instance of MemoryStoreV2.



26
27
28
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 26

def initialize
  clear
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



30
31
32
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 30

def data
  @data
end

Instance Method Details

#clearObject



32
33
34
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 32

def clear
  @data = {}
end

#create(path, name, data, *options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 48

def create(path, name, data, *options)
  unless data.is_a?(String)
    raise "set only works with strings (given data: #{data.inspect})"
  end

  parent = _get(path, options.include?(:create_dir))

  if parent.key?(name)
    raise DataAlreadyExistsError.new(path + [name])
  end

  parent[name] = data
end

#create_dir(path, name, *options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 36

def create_dir(path, name, *options)
  parent = _get(path, options.include?(:recursive))

  if parent.key?(name)
    unless options.include?(:recursive)
      raise DataAlreadyExistsError.new(path + [name])
    end
  else
    parent[name] = {}
  end
end

#delete(path) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 86

def delete(path)
  parent = _get(path[0, path.length - 1])
  unless parent.key?(path[-1])
    raise DataNotFoundError.new(path)
  end
  unless parent[path[-1]].is_a?(String)
    raise "delete only works with strings: #{path}"
  end

  parent.delete(path[-1])
end

#delete_dir(path, *options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 98

def delete_dir(path, *options)
  parent = _get(path[0, path.length - 1])
  unless parent.key?(path[-1])
    raise DataNotFoundError.new(path)
  end
  unless parent[path[-1]].is_a?(Hash)
    raise "delete_dir only works with directories: #{path}"
  end

  parent.delete(path[-1])
end

#exists?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 119

def exists?(path, options = {})
  value = _get(path)
  if value.is_a?(Hash) && !options[:allow_dirs]
    raise "exists? does not work with directories (#{path} = #{value.class})"
  end

  true
rescue DataNotFoundError
  false
end

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 130

def exists_dir?(path)
  dir = _get(path)
  unless dir.is_a? Hash
    raise "exists_dir? only works with directories (#{path} = #{dir.class})"
  end

  true
rescue DataNotFoundError
  false
end

#get(path, request = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 62

def get(path, request = nil)
  value = _get(path)
  if value.is_a?(Hash)
    raise "get() called on directory #{path.join("/")}"
  end

  value
end

#list(path) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 110

def list(path)
  dir = _get(path)
  unless dir.is_a? Hash
    raise "list only works with directories (#{path} = #{dir.class})"
  end

  dir.keys.sort
end

#set(path, data, *options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 71

def set(path, data, *options)
  unless data.is_a?(String)
    raise "set only works with strings: #{path} = #{data.inspect}"
  end

  # Get the parent
  parent = _get(path[0..-2], options.include?(:create_dir))

  if !options.include?(:create) && !parent[path[-1]]
    raise DataNotFoundError.new(path)
  end

  parent[path[-1]] = data
end