class Codestrap::Strap::Factory
Factory
for instantiating classes from the Codestrap::Strap
namespace
Attributes
klass[R]
Public Class Methods
new(nklass)
click to toggle source
Factory
constructor for Codestrap::Template::* Classes
@param [Class, Symbol, String] nklass @return [Codestrap::Stub::Factory]
# File lib/codestrap/strap/factory.rb, line 15 def initialize(nklass) self.klass = nklass enforce_methods ensure_required end
Public Instance Methods
construct(*args)
click to toggle source
Constructor for Codestrap::Strap::* Classes
@param [Array] args
Arguments to pass to constructor of child object
@return [Object]
Return the object as specified Factory#klass.new(args)
# File lib/codestrap/strap/factory.rb, line 27 def construct(*args) self.klass.new(args) end
enforce_methods()
click to toggle source
Ensure self.klass has the following methods
klass.pre klass.execute
Inherited from Codestrap::Template::Factory
klass.file klass.finalize
@raise [FactoryMethodEnforcement]
Raised if a enforced method doesn't exist in the child
@return [TrueClass, FalseClass]
# File lib/codestrap/strap/factory.rb, line 67 def enforce_methods [:pre, :execute, :overwrite, :overwrite=, :working_dir, :to_disk].each do |method| next if @klass.method_defined? method raise FactoryException, "Error missing method #{@klass.to_s}##{method}" end end
ensure_required()
click to toggle source
Ensures any methods aliased to abstract_method are overridden @raise [FactoryAbstractMethod]
Raised if a abstract method (A method aliased to abstract_method), is not created in the child
# File lib/codestrap/strap/factory.rb, line 77 def ensure_required if @klass.abstract_methods.length > 0 methods = @klass.abstract_methods.map{ |method| "#{@klass.to_s}##{method}" } raise FactoryException, "Abstract Method(s) #{methods.join(', ')} not overridden" end end
to_class(nklass)
click to toggle source
Dynamic Class creation. Looks for Classes in Codestrap::Strap::NKLASS
@param [String|Symbol] nklass
Look for Codestrap::Template::nklass
@raise [FactoryArgumentError]
Raised if nklass is not type String or type Symbol
@return [Codestrap::Stub::KLASS]
Return class Codestrap::Template::KLASS
# File lib/codestrap/strap/factory.rb, line 40 def to_class nklass case when nklass.is_a?(Symbol) klass = nklass.to_s when nklass.is_a?(String) klass = nklass else raise ArgumentError, %Q(Could not find Class Codestrap::Strap::#{nklass.to_s}) end @klass = ['Codestrap', 'Strap', klass].reduce(Module, :const_get) @klass end
Also aliased as: klass=