factory_loader 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/factory_loader.rb +48 -23
- metadata +2 -2
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ desc "Generate RDoc documentation"
|
|
10
10
|
Rake::RDocTask.new do |rdoc|
|
11
11
|
rdoc.rdoc_dir = 'rdoc'
|
12
12
|
rdoc.title = "FactoryLoader: intended to help scaling complex object creation with less pain and less refactoring."
|
13
|
-
rdoc.rdoc_files.include('lib/**/*.rb', 'README','
|
13
|
+
rdoc.rdoc_files.include('lib/**/*.rb', 'README.txt','History.txt','MIT-LICENSE')
|
14
14
|
end
|
15
15
|
|
16
16
|
require 'rubygems'
|
data/lib/factory_loader.rb
CHANGED
@@ -3,63 +3,88 @@ require 'find'
|
|
3
3
|
# FactoryLoader is intended to help scale object creation with less pain and less
|
4
4
|
# refactoring.
|
5
5
|
#
|
6
|
-
# In the early stages of a project object creation is simple and
|
6
|
+
# In the early stages of a project object creation is simple and
|
7
7
|
# dependencies are kept to a minimum. As the project grows so does the
|
8
|
-
# complexity of object
|
8
|
+
# complexity of object creation and dependencies. It doesn't make
|
9
9
|
# sense to create custom factory classes upfront to deal with complex
|
10
10
|
# object construction that may not exist yet. But when those custom
|
11
11
|
# factories are needed it is usually painful and time consuming to update
|
12
12
|
# the code base to use them. It's also easy for developers to give-in
|
13
13
|
# due to time constraints and start making bad decisions.
|
14
14
|
#
|
15
|
-
# This is where FactoryLoader comes into play. It
|
15
|
+
# This is where FactoryLoader comes into play. It automatically creates a Factory
|
16
16
|
# class for your objects and provides a Factory#create method which passes any arguments
|
17
17
|
# along to your object's constructor.
|
18
18
|
#
|
19
19
|
# When you need to have custom factory behavior you can implement the factory
|
20
|
-
# without having to update other code references (assuming you've used
|
20
|
+
# without having to update other code references (assuming you've used the factory
|
21
21
|
# in the rest of your application rather then direct class references).
|
22
22
|
#
|
23
|
-
#
|
23
|
+
# project/
|
24
|
+
# init.rb
|
25
|
+
# lib/
|
26
|
+
# |--things/
|
27
|
+
# |-- foo.rb
|
28
|
+
# |-- bar.rb
|
29
|
+
# |--factories/
|
30
|
+
# |-- bar_factory.rb
|
24
31
|
#
|
25
|
-
#
|
26
|
-
# lib/
|
27
|
-
# |--things/
|
28
|
-
# |-- foo.rb
|
29
|
-
# |-- bar.rb
|
30
|
-
# |--factories/
|
31
|
-
# |-- bar_factory.rb
|
32
|
-
#
|
33
|
-
# Given this project directory structure you could have the following code
|
32
|
+
# Given the above project directory structure you could have the following code
|
34
33
|
# in init.rb:
|
35
34
|
# factory_loader = FactoryLoader.new("lib/factories")
|
36
35
|
# factory_loader.load("lib/things")
|
37
36
|
#
|
38
37
|
# The first call constructs a factory loader telling it which directory is used
|
39
|
-
# to store custom factories.
|
38
|
+
# to store developer-written custom factories.
|
40
39
|
#
|
41
|
-
# The second call will create a factory for each *.rb file
|
42
|
-
#
|
43
|
-
#
|
40
|
+
# The second call will create a in-memory factory class for each *.rb file
|
41
|
+
# in the lib/things/ directory. A FooFactory class will be created to
|
42
|
+
# correspond with the foo.rb file. The generated factory
|
44
43
|
# will provide a #create method which will pass along all arguments to
|
45
44
|
# the constructor of the object it wraps. So...
|
46
45
|
# FooFactory.new.create :a => :b
|
47
46
|
# is the same as:
|
48
47
|
# Foo.new :a => :b
|
49
48
|
#
|
50
|
-
# A
|
49
|
+
# A BarFactory will NOT be created. This is because
|
51
50
|
# we told the FactoryLoader that custom factories are storied in lib/factories/
|
52
|
-
# and a bar_factory.rb exists there, so FactoryLoader assumes you want to use
|
53
|
-
# a custom factory.
|
51
|
+
# and a bar_factory.rb file exists there, so FactoryLoader assumes you want to use
|
52
|
+
# a custom factory. It also assumes that the class inside of bar_factory.rb is BarFactory.
|
53
|
+
#
|
54
|
+
# FactoryLoader dynamically creates the factory classes -- they are not written
|
55
|
+
# to disk. FactoryLoader also uses file naming conventions to determine
|
56
|
+
# what to do. For example:
|
57
|
+
# foo.rb => FooFactory
|
58
|
+
# crazy_dolphins.rb => CrazyDolphinsFactory
|
59
|
+
#
|
60
|
+
# === Factory.new
|
61
|
+
# The dynamically created factories are CLASSES and create is an INSTANCE method on them. You
|
62
|
+
# have to construct a factory in order to use it. This is so the factories themselves can be easily used in dependency injection
|
63
|
+
# frameworks.
|
64
|
+
#
|
65
|
+
# === Public Git repository:
|
66
|
+
# git://github.com/zdennis/factory_loader.git
|
67
|
+
#
|
68
|
+
# === Homepage:
|
69
|
+
# http://www.continuousthinking.com/factory_loader
|
70
|
+
#
|
71
|
+
# === Author:
|
72
|
+
# * Zach Dennis at Mutually Human Software (zach.dennis@gmail.com, zdennis@mutuallyhuman.com)
|
54
73
|
#
|
55
|
-
#
|
74
|
+
# === Special Thanks
|
75
|
+
# * Dave Crosby at Atomic Object
|
76
|
+
# * Ryan Fogle at Atomic Object
|
56
77
|
class FactoryLoader
|
57
|
-
VERSION = "0.1.
|
78
|
+
VERSION = "0.1.1"
|
58
79
|
|
80
|
+
# Constructs a FactoryLoader. The passed in factory_paths are searched recursively.
|
59
81
|
def initialize(*factory_paths)
|
60
82
|
@factory_paths = factory_paths.map{ |f| File.expand_path(f) }
|
61
83
|
end
|
62
84
|
|
85
|
+
# Creates factory classes based on searching filenames in the passed in directory
|
86
|
+
# and comparing them against factory file names in the passed in factory_paths
|
87
|
+
# given to the constructor.
|
63
88
|
def load(directory) # :nodoc:
|
64
89
|
Dir[directory + "/**/*.rb"].each do |file|
|
65
90
|
object_filename = File.basename(file, ".rb")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-03-
|
12
|
+
date: 2008-03-18 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|