fabrication 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/fabrication.rb CHANGED
@@ -4,6 +4,7 @@ module Fabrication
4
4
 
5
5
  module Generator
6
6
  autoload :ActiveRecord, 'fabrication/generator/active_record'
7
+ autoload :Mongoid, 'fabrication/generator/mongoid'
7
8
  autoload :Base, 'fabrication/generator/base'
8
9
  end
9
10
 
@@ -1,18 +1,26 @@
1
1
  class Fabrication::Fabricator
2
2
 
3
+ GENERATORS = [
4
+ Fabrication::Generator::ActiveRecord,
5
+ Fabrication::Generator::Mongoid,
6
+ Fabrication::Generator::Base
7
+ ]
8
+
9
+ attr_accessor :fabricator
10
+
3
11
  def initialize(class_name, &block)
4
12
  klass = class_for(class_name)
5
- if klass.ancestors.include?(ActiveRecord::Base)
6
- @fabricator = Fabrication::Generator::ActiveRecord.new(klass, &block)
7
- else
8
- @fabricator = Fabrication::Generator::Base.new(klass, &block)
9
- end
13
+ self.fabricator = GENERATORS.detect do |generator|
14
+ generator.supports?(klass)
15
+ end.new(klass, &block)
10
16
  end
11
17
 
12
18
  def fabricate(options={})
13
- @fabricator.generate(options)
19
+ fabricator.generate(options)
14
20
  end
15
21
 
22
+ protected
23
+
16
24
  #Stolen directly from factory_girl. Thanks thoughtbot!
17
25
  def class_for(class_or_to_s)
18
26
  if class_or_to_s.respond_to?(:to_sym)
@@ -7,6 +7,10 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
7
7
  @instance
8
8
  end
9
9
 
10
+ def self.supports?(klass)
11
+ defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
12
+ end
13
+
10
14
  def method_missing(method_name, *args, &block)
11
15
  method_name = method_name.to_s
12
16
  unless @options.include?(method_name.to_sym)
@@ -16,6 +16,8 @@ class Fabrication::Generator::Base
16
16
  assign(@instance, method_name.to_s, block_given? ? yield : args.first)
17
17
  end
18
18
 
19
+ def self.supports?(klass); true end
20
+
19
21
  private
20
22
 
21
23
  def assign(instance, method_name, value)
@@ -0,0 +1,14 @@
1
+ class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
2
+
3
+ def generate(options)
4
+ @options = options
5
+ @instance = super
6
+ @instance.save
7
+ @instance
8
+ end
9
+
10
+ def self.supports?(klass)
11
+ defined?(Mongoid) && klass.ancestors.include?(Mongoid::Document)
12
+ end
13
+
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -38,4 +38,18 @@ describe Fabrication::Fabricator do
38
38
 
39
39
  end
40
40
 
41
+ context 'with a mongoid document' do
42
+
43
+ let(:fabricator) { Fabrication::Fabricator.new(:author) { name "Seth Godin" } }
44
+
45
+ it 'fabricates a Author instance' do
46
+ fabricator.fabricate.instance_of?(Author).should be_true
47
+ end
48
+
49
+ it 'uses the activerecord generator' do
50
+ fabricator.instance_variable_get(:@fabricator).instance_of?(Fabrication::Generator::Mongoid).should be_true
51
+ end
52
+
53
+ end
54
+
41
55
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fabrication::Generator::Mongoid do
4
+
5
+ before(:all) { TestMigration.up }
6
+ after(:all) { TestMigration.down }
7
+
8
+ context 'mongoid object' do
9
+
10
+ let(:company) do
11
+ Fabrication::Generator::Mongoid.new(Author) do
12
+ name 'Name'
13
+ end.generate({:name => 'Something'})
14
+ end
15
+
16
+ before { company }
17
+
18
+ it 'persists the author upon creation' do
19
+ Author.where(:name => 'Something').first.should be
20
+ end
21
+
22
+ end
23
+
24
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,3 +7,10 @@ require 'spec/autorun'
7
7
  require 'fabrication'
8
8
  require 'ffaker'
9
9
  require 'active_record'
10
+ require 'mongoid'
11
+
12
+ Spec::Runner.configure do |config|
13
+ config.before(:all) do
14
+ Author.delete_all
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ Mongoid.configure do |config|
2
+ config.allow_dynamic_fields = false
3
+ config.master = Mongo::Connection.new.db("fabrication_test")
4
+ end
5
+
6
+ class Author
7
+ include Mongoid::Document
8
+ field :name
9
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Elliott
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-16 00:00:00 -04:00
18
+ date: 2010-06-17 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -82,6 +82,22 @@ dependencies:
82
82
  version: 1.3.0
83
83
  type: :development
84
84
  version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: mongoid
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 49
94
+ segments:
95
+ - 1
96
+ - 9
97
+ - 1
98
+ version: 1.9.1
99
+ type: :development
100
+ version_requirements: *id005
85
101
  description: Fabrication is an object generation framework that lazily generated attributes as they are used
86
102
  email:
87
103
  - paul@hashrocket.com
@@ -95,6 +111,7 @@ files:
95
111
  - lib/fabrication/fabricator.rb
96
112
  - lib/fabrication/generator/active_record.rb
97
113
  - lib/fabrication/generator/base.rb
114
+ - lib/fabrication/generator/mongoid.rb
98
115
  - lib/fabrication/version.rb
99
116
  - lib/fabrication.rb
100
117
  - spec/block_generated_fields_spec.rb
@@ -102,12 +119,14 @@ files:
102
119
  - spec/fabricator_spec.rb
103
120
  - spec/generator/active_record_spec.rb
104
121
  - spec/generator/base_spec.rb
122
+ - spec/generator/mongoid_spec.rb
105
123
  - spec/multiple_instance_spec.rb
106
124
  - spec/simple_object_spec.rb
107
125
  - spec/spec.opts
108
126
  - spec/spec_helper.rb
109
- - spec/support/database_setup.rb
127
+ - spec/support/active_record_setup.rb
110
128
  - spec/support/helper_objects.rb
129
+ - spec/support/mongoid_setup.rb
111
130
  - README.markdown
112
131
  has_rdoc: true
113
132
  homepage: http://github.com/paulelliott/fabrication