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
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 48

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

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

  if parent.has_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.has_key?(name)
    if !options.include?(:recursive)
      raise DataAlreadyExistsError.new(path + [name])
    end
  else
    parent[name] = {}
  end
end

#delete(path) ⇒ Object


83
84
85
86
87
88
89
90
91
92
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 83

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

#delete_dir(path, *options) ⇒ Object


94
95
96
97
98
99
100
101
102
103
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 94

def delete_dir(path, *options)
  parent = _get(path[0,path.length-1])
  if !parent.has_key?(path[-1])
    raise DataNotFoundError.new(path)
  end
  if !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)

113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 113

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

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)

125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 125

def exists_dir?(path)
  begin
    dir = _get(path)
    if !dir.is_a? Hash
      raise "exists_dir? only works with directories (#{path} = #{dir.class})"
    end
    return true
  rescue DataNotFoundError
    return false
  end
end

#get(path, request = nil) ⇒ Object


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

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


105
106
107
108
109
110
111
# File 'lib/chef_zero/data_store/memory_store_v2.rb', line 105

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

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


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

def set(path, data, *options)
  if !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