Class: FactoryLoader
- Inherits:
-
Object
- Object
- FactoryLoader
- Defined in:
- lib/factory_loader.rb
Overview
FactoryLoader is intended to help scale object creation with less pain and less refactoring.
In the early stages of a project object creation is simple and dependencies are kept to a minimum. As the project grows so does the complexity of object creation and dependencies. It doesn’t make sense to create custom factory classes upfront to deal with complex object construction that may not exist yet. But when those custom factories are needed it is usually painful and time consuming to update the code base to use them. It’s also easy for developers to give-in due to time constraints and start making bad decisions.
This is where FactoryLoader comes into play. It automatically creates a Factory class for your objects and provides a Factory#create method which passes any arguments along to your object’s constructor.
When you need to have custom factory behavior you can implement the factory without having to update other code references (assuming you’ve used the factory in the rest of your application rather then direct class references).
project/
init.rb
lib/
|--things/
|-- foo.rb
|-- bar.rb
|--factories/
|-- bar_factory.rb
Given the above project directory structure you could have the following code in init.rb:
factory_loader = FactoryLoader.new("lib/factories")
factory_loader.load("lib/things")
The first call constructs a factory loader telling it which directory is used to store developer-written custom factories.
The second call will create a in-memory factory class for each *.rb file in the lib/things/ directory. A FooFactory class will be created to correspond with the foo.rb file. The generated factory will provide a #create method which will pass along all arguments to the constructor of the object it wraps. So…
FooFactory.new.create :a => :b
is the same as:
Foo.new :a => :b
A BarFactory will NOT be created. This is because we told the FactoryLoader that custom factories are storied in lib/factories/ and a bar_factory.rb file exists there, so FactoryLoader assumes you want to use a custom factory. It also assumes that the class inside of bar_factory.rb is BarFactory.
FactoryLoader dynamically creates the factory classes – they are not written to disk. FactoryLoader also uses file naming conventions to determine what to do. For example:
foo.rb => FooFactory
crazy_dolphins.rb => CrazyDolphinsFactory
Factory.new
The dynamically created factories are CLASSES and create is an INSTANCE method on them. You have to construct a factory in order to use it. This is so the factories themselves can be easily used in dependency injection frameworks.
Public Git repository:
git://github.com/zdennis/factory_loader.git
Homepage:
www.continuousthinking.com/factory_loader
Author:
-
Zach Dennis at Mutually Human Software ([email protected], [email protected])
Special Thanks
-
Dave Crosby at Atomic Object
-
Ryan Fogle at Atomic Object
Constant Summary collapse
- VERSION =
"0.1.2"
Instance Method Summary collapse
-
#initialize(*factory_paths) ⇒ FactoryLoader
constructor
Constructs a FactoryLoader.
-
#load(directory) ⇒ Object
Creates factory classes based on searching filenames in the passed in directory and comparing them against factory file names in the passed in factory_paths given to the constructor.
Constructor Details
#initialize(*factory_paths) ⇒ FactoryLoader
Constructs a FactoryLoader. The passed in factory_paths are searched recursively.
81 82 83 |
# File 'lib/factory_loader.rb', line 81 def initialize(*factory_paths) @factory_paths = factory_paths.map{ |f| File.(f) } end |
Instance Method Details
#load(directory) ⇒ Object
Creates factory classes based on searching filenames in the passed in directory and comparing them against factory file names in the passed in factory_paths given to the constructor.
88 89 90 91 92 93 94 95 96 |
# File 'lib/factory_loader.rb', line 88 def load(directory) # :nodoc: Dir[directory + "/**/*.rb"].each do |file| object_filename = File.basename(file, ".rb") factory_filepath = "#{object_filename}_factory.rb" unless custom_factory_exists?(factory_filepath) load_object_factory object_filename.classify end end end |