class Deltacloud::Drivers::Mock::Client

Public Class Methods

new(storage_root) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 25
def initialize(storage_root)
  @storage_root = storage_root
  @collections = []

  if ! File::directory?(@storage_root)
    FileUtils::rm_rf(@storage_root)
    FileUtils::mkdir_p(@storage_root, :mode => 0750)
    data = Dir[File::join(File::dirname(__FILE__), "data", "*")]
    FileUtils::cp_r(data, @storage_root)
  end
end

Public Instance Methods

build(klass, id) click to toggle source

Return the object with id id of class klass from the collection derived from the classes name

# File lib/deltacloud/drivers/mock/mock_client.rb, line 75
def build(klass, id)
  klass.new(load_collection(collection_name(klass), id))
end
build_all(klass) click to toggle source

Return an array of model objects of the resources in the collection corresponding to class. The name of the collection is derived from the name of the class

# File lib/deltacloud/drivers/mock/mock_client.rb, line 87
def build_all(klass)
  load_all(collection_name(klass)).map { |hash| klass.new(hash) }
end
cimi_dir(collection) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 109
def cimi_dir(collection)
  File::join(@storage_root, "cimi", collection.to_s)
end
cimi_file(collection, id) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 105
def cimi_file(collection, id)
  File::join(cimi_dir(collection), "#{id}.json")
end
destroy(collection, id) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 91
def destroy(collection, id)
  fname = file(collection, id)
  FileUtils.rm(fname) if File::exists?(fname)
end
dir(collection) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 37
def dir(collection)
  result = File::join(@storage_root, collection.to_s)
  unless @collections.include?(collection)
    FileUtils::mkdir_p(result, :mode => 0750) unless File::directory?(result)
    @collections << collection
  end
  result
end
file(collection, id) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 46
def file(collection, id)
  File::join(dir(collection), "#{id}.yml")
end
files(collection) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 50
def files(collection)
  Dir[File::join(dir(collection), "*.yml")]
end
load_all(collection) click to toggle source

Return an array of hashes of all the resources in the collection

# File lib/deltacloud/drivers/mock/mock_client.rb, line 80
def load_all(collection)
  members(collection).map { |id| load_collection(collection, id) }
end
load_all_cimi(model_name) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 96
def load_all_cimi(model_name)
    model_files = Dir[File::join(cimi_dir(model_name), "*.json")]
    model_files.map{|f| File.read(f)}
end
load_cimi(model_name, id) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 101
def load_cimi(model_name, id)
    File.read(cimi_file(model_name, id))
end
load_collection(collection, id) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 59
def load_collection(collection, id)
  fname = file(collection, id)
  begin
    YAML.load_file(fname)
  rescue Errno::ENOENT
    nil
  end
end
members(collection) click to toggle source

Return the ID's of all members of collection

# File lib/deltacloud/drivers/mock/mock_client.rb, line 55
def members(collection)
  files(collection).map { |f| File::basename(f, ".yml") }
end
store(collection, obj) click to toggle source
# File lib/deltacloud/drivers/mock/mock_client.rb, line 68
def store(collection, obj)
  raise "Why no obj[:id] ?" unless obj[:id]
  File::open(file(collection, obj[:id]), "w") { |f| YAML.dump(obj, f) }
end