Class: Fhlow::LeafFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/module_fhlow/leaffactory.rb

Overview

This class implements the Factory Pattern in order to create concrete Leaf’s (Package’s and Unit’s). There also needs to be a concrete PackageFactory and UnitFacotry because this class is abstract. Therefore it is very easy to implement new Leaf’s if needed.

Direct Known Subclasses

LibraryFactory, PackageFactory, UnitFactory

Constant Summary collapse

@@factories =

An Array of subclasses.

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getLeafFor(_prefix, *_args) ⇒ Object

In order to request a concrete Leaf this function can be used by passing the _prefix (to specify which concrete Leaf is needed) and some arguments (which will be used to initialize the concrete Leaf).

_prefix

Is used to specify which concrete Leaf is needed.

*_args

see Package.create and Unit.create for more information.



60
61
62
63
64
65
66
# File 'lib/module_fhlow/leaffactory.rb', line 60

def LeafFactory.getLeafFor(_prefix, *_args)
    @@factories.each do |subclass|
        if subclass.getPrefix == _prefix
            return subclass.new.create(*_args)
        end
    end
end

.getPrefixObject

This function is abstract and has to be implemented in the subclasses. It’s purpose is to return the prefix of the concrete Leaf.

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/module_fhlow/leaffactory.rb', line 83

def LeafFactory.getPrefix()
    raise NotImplementedError, "You have to implement getPrefix() in sublcasses inherited from LeafFactory!"
end

.inherited(_lf) ⇒ Object

Is invoked when this class gets inherited. This mechanism makes a register function in subclasses obsolete because they register themselves when they inherit.

_lf

a sublcass of LeafFactory



45
46
47
# File 'lib/module_fhlow/leaffactory.rb', line 45

def LeafFactory.inherited(_lf)
    @@factories.push(_lf)
end

.load(_dirname) ⇒ Object

This function loads all files with the extension .rb from the specified directory. So concrete Leaf’s are clearly seperated from the rest of the fhlow implementation.

_dirname

Absolute path to the directory.



72
73
74
75
76
77
78
# File 'lib/module_fhlow/leaffactory.rb', line 72

def LeafFactory.load(_dirname)
    Dir.open(_dirname).each do |file|
        if file =~ /[.]rb$/
            require _dirname+"/"+file 
        end
    end
end

Instance Method Details

#create(*_args) ⇒ Object

This function is abstract and has to be implemented in the subclasses. It’s purpose is to create an object of the subclass.

*_args

An Array of arguments that are passed to the constructor if the subclass.

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/module_fhlow/leaffactory.rb', line 91

def create(*_args)
    raise NotImplementedError, "You have to implement create() in sublcasses inherited from LeafFactory!"
end