From 4176d1b2275017290276eae209c9049e7fcff2d2 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Wed, 7 Oct 2015 23:59:26 +0200 Subject: [PATCH] improvement: add default print method to resources --- lib/vulcano/backend.rb | 2 +- lib/vulcano/plugins/resource.rb | 11 ++++++++++- lib/vulcano/profile_context.rb | 2 +- test/helper.rb | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/vulcano/backend.rb b/lib/vulcano/backend.rb index ea8b1ea48..87143017e 100644 --- a/lib/vulcano/backend.rb +++ b/lib/vulcano/backend.rb @@ -56,7 +56,7 @@ module Vulcano end Vulcano::Resource.registry.each do |id, r| define_method id.to_sym do |*args| - r.new(self, *args) + r.new(self, id.to_s, *args) end end end diff --git a/lib/vulcano/plugins/resource.rb b/lib/vulcano/plugins/resource.rb index 27fb87104..a8a3ce8fa 100644 --- a/lib/vulcano/plugins/resource.rb +++ b/lib/vulcano/plugins/resource.rb @@ -14,9 +14,10 @@ module Vulcano cl = Class.new(obj) do # add some common methods include Vulcano::Plugins::ResourceCommon - def initialize(backend, *args) + def initialize(backend, name, *args) # attach the backend to this instance @__backend_runner__ = backend + @__resource_name__ = name # call the resource initializer super(*args) end @@ -30,6 +31,14 @@ module Vulcano # add the resource to the registry by name Vulcano::Resource.registry[name] = cl end + + # Define methods which are available to all resources + # and may be overwritten. + + # Print the name of the resource + def to_s + @__resource_name__ + end end module ResourceCommon diff --git a/lib/vulcano/profile_context.rb b/lib/vulcano/profile_context.rb index f062c0d5b..292078f03 100644 --- a/lib/vulcano/profile_context.rb +++ b/lib/vulcano/profile_context.rb @@ -63,7 +63,7 @@ module Vulcano Module.new do Vulcano::Resource.registry.each do |id, r| define_method id.to_sym do |*args| - r.new(backend, *args) + r.new(backend, id.to_s, *args) end end end diff --git a/test/helper.rb b/test/helper.rb index c5f0a984e..2e169b2e2 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -151,7 +151,7 @@ class MockLoader def load_resource(resource, *args) # initialize resource with backend and parameters @resource_class = Vulcano::Resource.registry[resource] - @resource = @resource_class.new(backend, *args) + @resource = @resource_class.new(backend, resource, *args) end end