Class: Codestrap::Stub::Factory

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(nklass) ⇒ Codestrap::Stub::Factory

Factory constructor for Codestrap::Stub::* Classes

Parameters:

  • nklass (Symbol|String)


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

#klassObject (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

Parameters:

  • args (Array)

    Arguments to pass to constructor of child object

Returns:

  • (Object)

    Return the object as specified Factory#klass.new(args)

Raises:



29
30
31
# File 'lib/codestrap/stub/factory.rb', line 29

def construct(*args)
  self.klass.new(args)
end

#enforce_methodsTrueClass, FalseClass

Ensure self.klass has the following methods

klass.pre klass.execute

Inherited from Codestrap::Stub::Factory klass.file klass.finalize

Returns:

  • (TrueClass, FalseClass)

Raises:



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_requiredObject

Ensures any methods aliased to abstract_method are overridden

Raises:

  • (FactoryAbstractMethod)

    Raised if a abstract method (A method aliased to abstract_method), is not created in the child



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

Parameters:

  • nklass (String|Symbol)

    Look for Codestrap::Stub::nklass

Returns:

  • (Codestrap::Stub::KLASS)

    Return class Codestrap::Stub::KLASS

Raises:



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