Class: Codestrap::Strap::Factory
- Inherits:
-
Object
- Object
- Codestrap::Strap::Factory
- Includes:
- Mixin::Exceptions::Factory
- Defined in:
- lib/codestrap/strap/factory.rb
Overview
Factory for instantiating classes from the Codestrap::Strap namespace
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#construct(*args) ⇒ Object
Constructor for Codestrap::Strap::* 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::Template::* Classes.
-
#to_class(nklass) ⇒ Codestrap::Stub::KLASS
(also: #klass=)
Dynamic Class creation.
Constructor Details
#initialize(nklass) ⇒ Codestrap::Stub::Factory
Factory constructor for Codestrap::Template::* Classes
15 16 17 18 19 |
# File 'lib/codestrap/strap/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/strap/factory.rb', line 9 def klass @klass end |
Instance Method Details
#construct(*args) ⇒ Object
Constructor for Codestrap::Strap::* Classes
27 28 29 |
# File 'lib/codestrap/strap/factory.rb', line 27 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::Template::Factory klass.file klass.finalize
67 68 69 70 71 72 |
# 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 ⇒ Object
Ensures any methods aliased to abstract_method are overridden
77 78 79 80 81 82 |
# 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) ⇒ Codestrap::Stub::KLASS Also known as: klass=
Dynamic Class creation. Looks for Classes in Codestrap::Strap::NKLASS
40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |