From a711277f16e6ebac75b07ef75252895bc195452b Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Fri, 31 Jan 2014 15:47:39 +0000 Subject: [PATCH] allow from_definition to be invoked from base class This helps testing. --- libraries/pacemaker/cib_object.rb | 34 +++++++++++-------- .../pacemaker/resource/primitive_spec.rb | 5 +++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/libraries/pacemaker/cib_object.rb b/libraries/pacemaker/cib_object.rb index ebb8754..aaded5d 100644 --- a/libraries/pacemaker/cib_object.rb +++ b/libraries/pacemaker/cib_object.rb @@ -36,24 +36,28 @@ module Pacemaker def from_name(name) definition = get_definition(name) return nil unless definition and ! definition.empty? - obj_type = type(definition) - subclass = @@subclasses[obj_type] - unless subclass - raise "No subclass of #{self.name} was registered with type '#{obj_type}'" - end - obj = subclass.from_definition(definition) - return nil unless obj - unless name == obj.name - raise "Name '#{obj.name}' in definition didn't match name '#{name}' used for retrieval" - end - obj + from_definition(definition) end def from_definition(definition) - obj = new(name) - obj.definition = definition - obj.parse_definition - obj + if method(__method__).owner == self.singleton_class + # Invoked via (this) base class + obj_type = type(definition) + subclass = @@subclasses[obj_type] + unless subclass + raise "No subclass of #{self.name} was registered with type '#{obj_type}'" + end + return subclass.from_definition(definition) + else + # Invoked via subclass + obj = new(name) + unless name == obj.name + raise "Name '#{obj.name}' in definition didn't match name '#{name}' used for retrieval" + end + obj.definition = definition + obj.parse_definition + obj + end end end diff --git a/spec/libraries/pacemaker/resource/primitive_spec.rb b/spec/libraries/pacemaker/resource/primitive_spec.rb index 4913f3d..21d9ecc 100644 --- a/spec/libraries/pacemaker/resource/primitive_spec.rb +++ b/spec/libraries/pacemaker/resource/primitive_spec.rb @@ -18,6 +18,11 @@ describe Pacemaker::Resource::Primitive do expect(obj.is_a? Pacemaker::Resource::Primitive).to be_true end + it "should be instantiated via Pacemaker::CIBObject.from_definition" do + obj = Pacemaker::CIBObject.from_definition(@primitive.definition_string) + expect(obj.is_a? Pacemaker::Resource::Primitive).to be_true + end + it "should barf if the loaded definition's type is not primitive" do Mixlib::ShellOut.any_instance.stub(:error!) expect_any_instance_of(Mixlib::ShellOut) \