class ChefZero::DataStore::MemoryStoreV2

Attributes

data[R]

Public Class Methods

new() click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 26
def initialize
  clear
end

Public Instance Methods

clear() click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 32
def clear
  @data = {}

  create_dir([], 'organizations')
end
create(path, name, data, *options) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 73
def create(path, name, data, *options)
  if !data.is_a?(String)
    raise "set only works with strings"
  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) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 61
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
    _create_dir(path, parent, name)
  end
end
create_org() click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 38
def create_org
  org = {
    'clients' => {
      'chef-validator' => '{ "validator": true }',
      'chef-webui' => '{ "admin": true }'
    },
    'cookbooks' => {},
    'data' => {},
    'environments' => {
      '_default' => '{ "description": "The default Chef environment" }'
    },
    'file_store' => {
      'checksums' => {}
    },
    'nodes' => {},
    'roles' => {},
    'sandboxes' => {},
    'users' => {
      'admin' => '{ "admin": "true" }'
    }
  }
end
delete(path) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 108
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) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 119
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 = {}) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 138
def exists?(path, options = {})
  begin
    value = _get(path)
    if value.is_a?(Hash) && !options[:allow_dirs]
      raise "exists? does not work with directories (#{path} = #{dir.class})"
    end
    return true
  rescue DataNotFoundError
    return false
  end
end
exists_dir?(path) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 150
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) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 86
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) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 130
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) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 94
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

Private Instance Methods

_create_dir(parent_path, parent, name) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 179
def _create_dir(parent_path, parent, name)
  if parent_path == [ 'organizations' ]
    parent[name] = create_org
  else
    parent[name] = {}
  end
end
_get(path, create_dir=false) click to toggle source
# File lib/chef_zero/data_store/memory_store_v2.rb, line 164
def _get(path, create_dir=false)
  value = @data
  path.each_with_index do |path_part, index|
    if !value.has_key?(path_part)
      if create_dir
        _create_dir(path[0,index], value, path_part)
      else
        raise DataNotFoundError.new(path[0,index+1])
      end
    end
    value = value[path_part]
  end
  value
end