Class: Deltacloud::Drivers::Mock::Client
- Inherits:
-
Object
- Object
- Deltacloud::Drivers::Mock::Client
- Defined in:
- lib/deltacloud/drivers/mock/mock_client.rb
Instance Method Summary collapse
-
#build(klass, id) ⇒ Object
Return the object with id
id
of classklass
from the collection derived from the classes name. -
#build_all(klass) ⇒ Object
Return an array of model objects of the resources in the collection corresponding to class.
- #cimi_dir(collection) ⇒ Object
- #cimi_file(collection, id) ⇒ Object
- #destroy(collection, id) ⇒ Object
- #dir(collection) ⇒ Object
- #file(collection, id) ⇒ Object
- #files(collection) ⇒ Object
-
#initialize(storage_root) ⇒ Client
constructor
A new instance of Client.
-
#load_all(collection) ⇒ Object
Return an array of hashes of all the resources in the collection.
- #load_all_cimi(model_name) ⇒ Object
- #load_cimi(model_name, id) ⇒ Object
- #load_collection(collection, id) ⇒ Object
-
#members(collection) ⇒ Object
Return the ID’s of all members of
collection
. - #store(collection, obj) ⇒ Object
Constructor Details
#initialize(storage_root) ⇒ Client
Returns a new instance of Client.
25 26 27 28 29 30 31 32 33 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 25 def initialize(storage_root) @storage_root = storage_root @collections = [] if ! File::directory?(File::join(@storage_root, "images")) data = Dir[File::join(File::dirname(__FILE__), "data", "*")] FileUtils::mkdir_p(@storage_root, :verbose => true) FileUtils::cp_r(data, @storage_root) end end |
Instance Method Details
#build(klass, id) ⇒ Object
Return the object with id id
of class klass
from the collection derived from the classes name
73 74 75 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 73 def build(klass, id) klass.new(load_collection(collection_name(klass), id)) end |
#build_all(klass) ⇒ Object
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
85 86 87 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 85 def build_all(klass) load_all(collection_name(klass)).map { |hash| klass.new(hash) } end |
#cimi_dir(collection) ⇒ Object
107 108 109 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 107 def cimi_dir(collection) File::join(@storage_root, "cimi", collection.to_s) end |
#cimi_file(collection, id) ⇒ Object
103 104 105 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 103 def cimi_file(collection, id) File::join(cimi_dir(collection), "#{id}.json") end |
#destroy(collection, id) ⇒ Object
89 90 91 92 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 89 def destroy(collection, id) fname = file(collection, id) FileUtils.rm(fname) if File::exists?(fname) end |
#dir(collection) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 35 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) ⇒ Object
44 45 46 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 44 def file(collection, id) File::join(dir(collection), "#{id}.yml") end |
#files(collection) ⇒ Object
48 49 50 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 48 def files(collection) Dir[File::join(dir(collection), "*.yml")] end |
#load_all(collection) ⇒ Object
Return an array of hashes of all the resources in the collection
78 79 80 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 78 def load_all(collection) members(collection).map { |id| load_collection(collection, id) } end |
#load_all_cimi(model_name) ⇒ Object
94 95 96 97 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 94 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) ⇒ Object
99 100 101 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 99 def load_cimi(model_name, id) File.read(cimi_file(model_name, id)) end |
#load_collection(collection, id) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 57 def load_collection(collection, id) fname = file(collection, id) begin YAML.load_file(fname) rescue Errno::ENOENT nil end end |
#members(collection) ⇒ Object
Return the ID’s of all members of collection
53 54 55 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 53 def members(collection) files(collection).map { |f| File::basename(f, ".yml") } end |
#store(collection, obj) ⇒ Object
66 67 68 69 |
# File 'lib/deltacloud/drivers/mock/mock_client.rb', line 66 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 |