Class: Codestrap::Stub::Factory
- Inherits:
-
Object
- Object
- Codestrap::Stub::Factory
- Includes:
- Mixin::Exceptions::Factory
- Defined in:
- lib/codestrap/stub/factory.rb
Overview
Factory for instantiating classes from the Codestrap::Stub namespace
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#construct(*args) ⇒ Object
Constructor for Codestrap::Stub::* Classes.
-
#enforce_methods ⇒ TrueClass, FalseClass
Ensure self.klass has the following methods.
-
#ensure_required ⇒ Object
Ensures any methods aliased to abstract_method are overridden.
-
#initialize(nklass) ⇒ Codestrap::Stub::Factory
constructor
Factory constructor for Codestrap::Stub::* Classes.
-
#to_class(nklass) ⇒ Codestrap::Stub::KLASS
(also: #klass=)
Dynamic Class creation.
Constructor Details
#initialize(nklass) ⇒ Codestrap::Stub::Factory
Factory constructor for Codestrap::Stub::* Classes
15 16 17 18 19 |
# File 'lib/codestrap/stub/factory.rb', line 15 def initialize(nklass) self.klass = nklass enforce_methods ensure_required end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
9 10 11 |
# File 'lib/codestrap/stub/factory.rb', line 9 def klass @klass end |
Instance Method Details
#construct(*args) ⇒ Object
Constructor for Codestrap::Stub::* Classes
29 30 31 |
# File 'lib/codestrap/stub/factory.rb', line 29 def construct(*args) self.klass.new(args) end |
#enforce_methods ⇒ TrueClass, FalseClass
Ensure self.klass has the following methods
klass.pre klass.execute
Inherited from Codestrap::Stub::Factory klass.file klass.finalize
69 70 71 72 73 74 |
# File 'lib/codestrap/stub/factory.rb', line 69 def enforce_methods [:pre, :execute, :overwrite, :overwrite=, :file, :to_disk].each do |method| next if @klass.method_defined? method raise FactoryException, "Error missing method #{@klass.to_s}##{method}" end end |
#ensure_required ⇒ Object
Ensures any methods aliased to abstract_method are overridden
79 80 81 82 83 84 |
# File 'lib/codestrap/stub/factory.rb', line 79 def ensure_required if @klass.abstract_methods.length > 0 methods = @klass.abstract_methods.map{ |method| "#{@klass.to_s}##{method}" } raise FactoryAbstractMethod, "Abstract Method(s) #{methods.join(', ')} not overridden" end end |
#to_class(nklass) ⇒ Codestrap::Stub::KLASS Also known as: klass=
Dynamic Class creation. Looks for Classes in Codestrap::Stub::NKLASS
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/codestrap/stub/factory.rb', line 42 def to_class nklass case when nklass.is_a?(Symbol) klass = nklass.to_s when nklass.is_a?(String) klass = nklass else raise FactoryNotImplemented, %Q(Could not find Class Codestrap::Stub::#{nklass.to_s}) end @klass = ['Codestrap', 'Stub', klass ].reduce(Module, :const_get) @klass end |