fabrication 0.2.2 → 0.2.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.
@@ -1,21 +1,14 @@
|
|
1
1
|
class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
2
2
|
|
3
|
-
attr_accessor :options
|
4
|
-
|
5
|
-
def generate(options)
|
6
|
-
self.options = options
|
7
|
-
self.instance = super.tap { |t| t.save }
|
8
|
-
end
|
9
|
-
|
10
3
|
def self.supports?(klass)
|
11
4
|
defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
|
12
5
|
end
|
13
6
|
|
14
7
|
def method_missing(method_name, *args, &block)
|
15
8
|
method_name = method_name.to_s
|
16
|
-
count = (args && args.first && args.first[:count]) || 0
|
17
9
|
unless options.include?(method_name.to_sym)
|
18
10
|
if block_given?
|
11
|
+
count = (args && args.first && args.first[:count]) || 0
|
19
12
|
unless (args.first && args.first[:force]) || instance.class.columns.map(&:name).include?(method_name)
|
20
13
|
# copy the original getter
|
21
14
|
instance.instance_variable_set("@__#{method_name}_original", instance.method(method_name).clone)
|
@@ -48,4 +41,10 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
|
48
41
|
end
|
49
42
|
end
|
50
43
|
|
44
|
+
protected
|
45
|
+
|
46
|
+
def after_generation
|
47
|
+
instance.save
|
48
|
+
end
|
49
|
+
|
51
50
|
end
|
@@ -1,28 +1,39 @@
|
|
1
1
|
class Fabrication::Generator::Base
|
2
2
|
|
3
|
-
|
3
|
+
def self.supports?(klass); true end
|
4
4
|
|
5
|
-
def
|
6
|
-
self.
|
7
|
-
self.parent = parent
|
8
|
-
self.block = block
|
5
|
+
def after_create(&block)
|
6
|
+
self.after_create_block = block if block_given?
|
9
7
|
end
|
10
8
|
|
11
9
|
def generate(options={})
|
12
10
|
self.instance = parent ? parent.fabricate : klass.new
|
13
|
-
|
11
|
+
self.options = options
|
12
|
+
instance_eval(&block)
|
14
13
|
options.each { |k,v| assign(instance, k, v) }
|
14
|
+
after_generation
|
15
|
+
after_create_block.call(instance) if after_create_block
|
15
16
|
instance
|
16
17
|
end
|
17
18
|
|
19
|
+
def initialize(klass, parent=nil, &block)
|
20
|
+
self.klass = klass
|
21
|
+
self.parent = parent
|
22
|
+
self.block = block
|
23
|
+
end
|
24
|
+
|
18
25
|
def method_missing(method_name, *args, &block)
|
19
26
|
assign(instance, method_name.to_s, args.first, &block)
|
20
27
|
end
|
21
28
|
|
22
|
-
|
29
|
+
protected
|
30
|
+
|
31
|
+
def after_generation; end
|
23
32
|
|
24
33
|
private
|
25
34
|
|
35
|
+
attr_accessor :after_create_block, :block, :instance, :klass, :options, :parent
|
36
|
+
|
26
37
|
def assign(instance, method_name, param)
|
27
38
|
value = nil
|
28
39
|
if param.is_a?(Hash) && param[:count] && param[:count] > 1
|
data/lib/fabrication/version.rb
CHANGED
data/spec/spec.opts
CHANGED
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:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 3
|
10
|
+
version: 0.2.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-
|
18
|
+
date: 2010-07-02 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|