class ChefZero::DataStore::V1ToV2Adapter

Constants

ORG_DEFAULTS

Attributes

real_store[R]
single_org[R]

Public Class Methods

new(real_store, single_org, options = {}) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 6
def initialize(real_store, single_org, options = {})
  @real_store = real_store
  @single_org = single_org
  @options = options
  clear
end

Public Instance Methods

clear() click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 29
def clear
  if @options[:org_defaults]
    @defaults = { 'organizations' => { @single_org => @options[:org_defaults] }}
  else
    @defaults = {}
  end
  real_store.clear if real_store.respond_to?(:clear)
end
create(path, name, data, *options) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 48
def create(path, name, data, *options)
  return nil if skip_organizations?(path, name)
  if using_default?(path, name)
    raise DataAlreadyExistsError.new(path + [name])
  end
  remove_default(path, name)

  fix_exceptions do
    real_store.create(path[2..-1], name, data, *options)
  end
end
create_dir(path, name, *options) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 38
def create_dir(path, name, *options)
  return nil if skip_organizations?(path, name)
  if using_default?(path, name)
    raise DataAlreadyExistsError.new(path + [name])
  end
  fix_exceptions do
    real_store.create_dir(path[2..-1], name, *options)
  end
end
delete(path) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 79
def delete(path)
  return nil if skip_organizations?(path)
  remove_default(path)
  fix_exceptions do
    real_store.delete(path[2..-1])
  end
end
delete_dir(path, *options) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 87
def delete_dir(path, *options)
  return nil if skip_organizations?(path)
  fix_exceptions do
    real_store.delete_dir(path[2..-1], *options)
  end
end
exists?(path) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 108
def exists?(path)
  return nil if skip_organizations?(path)
  if using_default?(path)
    true
  else
    fix_exceptions do
      real_store.exists?(path[2..-1])
    end
  end
end
exists_dir?(path) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 119
def exists_dir?(path)
  return nil if skip_organizations?(path)
  if using_default?(path)
    true
  else
    fix_exceptions do
      real_store.exists_dir?(path[2..-1])
    end
  end
end
get(path, request=nil) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 60
def get(path, request=nil)
  return nil if skip_organizations?(path)
  if using_default?(path)
    get_default(path)
  else
    fix_exceptions do
      real_store.get(path[2..-1], request)
    end
  end
end
list(path) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 94
def list(path)
  return nil if skip_organizations?(path)
  fix_exceptions do
    result = real_store.list(path[2..-1])
    if using_default?(path)
      result ||= []
      get_default(path).keys.each do |value|
        result << value if !result.include?(value)
      end
    end
    result
  end
end
set(path, data, *options) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 71
def set(path, data, *options)
  return nil if skip_organizations?(path)
  remove_default(path)
  fix_exceptions do
    real_store.set(path[2..-1], data, *options)
  end
end

Private Instance Methods

fix_exceptions() { || ... } click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 166
def fix_exceptions
  begin
    yield
  rescue DataAlreadyExistsError => e
    raise DataAlreadyExistsError.new([ 'organizations', single_org ] + e.path, e)
  rescue DataNotFoundError => e
    raise DataNotFoundError.new([ 'organizations', single_org ] + e.path, e)
  end
end
get_default(path, name = nil) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 142
def get_default(path, name = nil)
  path = path + [name] if name
  result = @defaults
  path.each do |part|
    return nil if !result.has_key?(part)
    result = result[part]
  end
  result
end
remove_default(path, name = nil) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 152
def remove_default(path, name = nil)
  dir = name ? path[0..-2] : path
  default = @defaults
  dir.each do |part|
    return if !default.has_key?(part)
    default = default[part]
  end

  name = name || path.last
  if name
    default.delete(name)
  end
end
skip_organizations?(path, name = nil) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 176
def skip_organizations?(path, name = nil)
  if path == []
    raise "" if name == nil || name != 'organizations'
    true
  elsif path == ['organizations']
    raise "" if name == nil || name != single_org
    true
  else
    raise "Path #{path} must start with /organizations/#{single_org}" if path[0..1] != [ 'organizations', single_org ]
    if !name
      raise "Path #{path} must start with /organizations/#{single_org}/<something>" if path.size <= 2
    end
    false
  end
end
using_default?(path, name = nil) click to toggle source
# File lib/chef_zero/data_store/v1_to_v2_adapter.rb, line 132
def using_default?(path, name = nil)
  path = path + [name] if name
  result = @defaults
  path.each do |part|
    return false if !result.has_key?(part)
    result = result[part]
  end
  !result.nil?
end