dfl-factories-and-workers 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,8 +1,12 @@
1
- * [10/14/2008] - 0.0.2 (gem release)
1
+ * [10/15/2008] - 0.1.1
2
+
3
+ Fixed problems with ./script/generate migration and method missing
4
+ factories.rb now requires a TestFactories module namespace!
5
+
6
+ * [10/14/2008] - 0.1.0
2
7
 
3
8
  Fixed problem with rails/init.rb
4
-
5
-
9
+
6
10
  * [08/05/2008] - 0.0.1 (gem release)
7
11
 
8
12
  Initial release as a gem.
data/README.rdoc CHANGED
@@ -34,9 +34,11 @@ It will be loaded during plugin initialization (see init.rb). You could also exp
34
34
 
35
35
  A minimal factories.rb file:
36
36
 
37
+ module TestFactories # NOTE: this module namespace is a new addition from v0.1.1 onwards
37
38
  include FactoriesAndWorkers::Factory # mixin to Object, which is especially useful for script/console work; season to taste.
38
39
 
39
40
  factory :user, :name => "default name"
41
+ end
40
42
 
41
43
  This would define methods named +build_user+, +create_user+, and +valid_user_attributes+ to be available in any Object in the project.
42
44
  If you wanted to override the default valid attributes, you could call:
@@ -93,10 +95,11 @@ There is also a rake task to automagically generate template factories from your
93
95
  rake factory:generate # will print templates for all AR models
94
96
  rake factory:generate MODEL=user # will print template for user model
95
97
 
98
+ Factories also really come in handy in the development console. The plugin and factories.rb are automatically loaded for test and development environments (see init.rb)
96
99
 
97
100
  =The FactoryWorker
98
101
 
99
- The FactoryWorker is a work in progress.
102
+ The FactoryWorker is a work in progress. It currently doesn't execute in the same scope/binding as where it's called from, which means instance variables aren't shared :(
100
103
 
101
104
  If you create a file named 'factory_workers.rb' in either your spec or test directory, you can define snippets of code that can be ran at anytime, anywhere in your tests (this may not be true in the future, I may limit where it can be run, iono).
102
105
 
@@ -5,6 +5,16 @@ module FactoriesAndWorkers
5
5
  module Factory
6
6
  def self.included( base )
7
7
  base.extend ClassMethods
8
+
9
+ # factory methods are defined as class methods; this delegation will allow them to also be called as instance methods
10
+ def method_missing method, *args, &block
11
+ if ClassMethods.method_defined?(method)
12
+ self.class.send method, *args, &block
13
+ else
14
+ super
15
+ end
16
+ end
17
+
8
18
  end
9
19
 
10
20
  module ClassMethods
@@ -28,15 +38,6 @@ module FactoriesAndWorkers
28
38
  end
29
39
  end
30
40
 
31
- # factory methods are defined as class methods; this delegation will allow them to also be called as instance methods
32
- def method_missing method, *args, &block
33
- if self.class.respond_to?( method )
34
- self.class.send method, *args, &block
35
- else
36
- super
37
- end
38
- end
39
-
40
41
  end
41
42
 
42
43
  class FactoryBuilder
@@ -7,6 +7,14 @@ module FactoriesAndWorkers
7
7
 
8
8
  def self.included( base )
9
9
  base.extend ClassMethods
10
+ # factory_worker methods are defined as class methods; this delegation will allow them to also be called as instance methods
11
+ def method_missing method, *args, &block
12
+ if ClassMethods.method_defined?(method)
13
+ self.class.send method, *args, &block
14
+ else
15
+ super
16
+ end
17
+ end
10
18
  end
11
19
 
12
20
  module ClassMethods
data/rails/init.rb CHANGED
@@ -1,8 +1,27 @@
1
1
  require 'fileutils'
2
2
 
3
3
  config.after_initialize do
4
+
5
+ if script_console_running = defined?(::IRB) && ::IRB.conf[:LOAD_MODULES] && ::IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
6
+ # mixin to Object if we are running in ./script/console
7
+ obj = Object
8
+ else # otherwise just mix into Test::Unit to play it safe
9
+ obj = Test::Unit::TestCase
10
+ end
11
+
12
+ # mixin plugin
13
+ obj.send :include, FactoriesAndWorkers::Factory
14
+ obj.send :include, FactoriesAndWorkers::Worker
15
+
16
+ # load factory and worker definition files if they exist
4
17
  %w(spec/factories.rb spec/factory_workers.rb test/factories.rb test/factory_workers.rb).each do |file|
5
18
  path = File.join(Dir.pwd, file)
6
19
  require path if File.exists?(path)
7
20
  end
21
+
22
+ # mixin factory and worker definitions
23
+ obj.send :include, ::TestFactories if defined?(TestFactories)
24
+ obj.send :include, ::FactoryWorkers if defined?(FactoryWorkers)
8
25
  end
26
+
27
+
data/test/factories.rb CHANGED
@@ -1,18 +1,21 @@
1
- include FactoriesAndWorkers::Factory
1
+ # module TestFactories
2
+ include FactoriesAndWorkers::Factory
2
3
 
3
- factory :monkey, {
4
- :name => "George",
5
- :unique => "$UNIQ(10)",
6
- :counter => "$COUNT",
7
- :number => lambda{ increment! :foo }
8
- }
4
+ factory :monkey, {
5
+ :name => "George",
6
+ :unique => "$UNIQ(10)",
7
+ :counter => "$COUNT",
8
+ :number => lambda{ increment! :foo }
9
+ }
9
10
 
10
- factory :pirate, {
11
- :catchphrase => "Ahhrrrr, Matey!",
12
- :monkey => :belongs_to_model,
13
- :created_on => lambda{ 1.day.ago }
14
- }
11
+ factory :pirate, {
12
+ :catchphrase => "Ahhrrrr, Matey!",
13
+ :monkey => :belongs_to_model,
14
+ :created_on => lambda{ 1.day.ago }
15
+ }
15
16
 
16
- factory :ninja_pirate, {
17
- :catchphrase => "(silent)"
18
- }, :class => Pirate, :chain => lambda{ valid_pirate_attributes( :monkey => nil ) }
17
+ factory :ninja_pirate, {
18
+ :catchphrase => "(silent)"
19
+ }, :class => Pirate, :chain => lambda{ valid_pirate_attributes( :monkey => nil ) }
20
+
21
+ # end
data/test/test_helper.rb CHANGED
@@ -11,12 +11,12 @@ ENV["RAILS_ENV"] = "test"
11
11
  require File.expand_path(File.join(app_root, "config", "boot"))
12
12
  $:.unshift(File.expand_path(File.join(plugin_root, "lib")))
13
13
 
14
- # initialize the plugin
15
- require plugin_root + "/init"
16
14
 
17
15
  # pull in just what we need from Rails
18
16
  %w( active_record active_support ).each { |f| require f }
19
17
 
18
+ # initialize the plugin
19
+ require plugin_root + "/lib/factories-and-workers"
20
20
 
21
21
  # we'll use a totally standalone db for our testing
22
22
  options = { :adapter => "sqlite3", :timeout => 500, :database => ":memory:" }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dfl-factories-and-workers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Herald
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2008-10-15 00:00:00 -07:00
14
+ date: 2008-10-16 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies: []
17
17