fabrication 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,8 @@ module Fabrication
8
8
  autoload :Attribute, 'fabrication/schematic/attribute'
9
9
  autoload :Definition, 'fabrication/schematic/definition'
10
10
  autoload :Manager, 'fabrication/schematic/manager'
11
+ autoload :Evaluator, 'fabrication/schematic/evaluator'
12
+ autoload :Runner, 'fabrication/schematic/runner'
11
13
  end
12
14
 
13
15
  autoload :Config, 'fabrication/config'
@@ -23,6 +25,7 @@ module Fabrication
23
25
  autoload :DataMapper, 'fabrication/generator/data_mapper'
24
26
  autoload :Mongoid, 'fabrication/generator/mongoid'
25
27
  autoload :Sequel, 'fabrication/generator/sequel'
28
+ autoload :Keymaker, 'fabrication/generator/keymaker'
26
29
  autoload :Base, 'fabrication/generator/base'
27
30
  end
28
31
 
@@ -13,15 +13,19 @@ class Fabrication::Generator::Base
13
13
  build_instance
14
14
  end
15
15
 
16
- callbacks[:after_build].each { |callback| callback.call(__instance) } if callbacks[:after_build]
16
+ execute_callbacks(callbacks[:after_build])
17
17
 
18
18
  __instance
19
19
  end
20
20
 
21
+ def execute_callbacks(callbacks)
22
+ callbacks.each { |callback| __instance.instance_eval(&callback) } if callbacks
23
+ end
24
+
21
25
  def create(attributes=[], callbacks=[])
22
26
  build(attributes, callbacks)
23
27
  persist
24
- callbacks[:after_create].each { |callback| callback.call(__instance) } if callbacks[:after_create]
28
+ execute_callbacks(callbacks[:after_create])
25
29
  __instance
26
30
  end
27
31
 
@@ -64,13 +68,7 @@ class Fabrication::Generator::Base
64
68
  end
65
69
 
66
70
  def method_missing(method_name, *args, &block)
67
- return __attributes[method_name] if args.empty? && !block_given? && __attributes[method_name]
68
-
69
- if block_given?
70
- assign(method_name, args.first || {}, &block)
71
- else
72
- assign(method_name, {}, args.first)
73
- end
71
+ __attributes[method_name] || super
74
72
  end
75
73
 
76
74
  protected
@@ -85,34 +83,12 @@ class Fabrication::Generator::Base
85
83
  __instance.save! if __instance.respond_to?(:save!)
86
84
  end
87
85
 
88
- def assign(method_name, options, value=nil, &block)
89
- if options.has_key?(:count)
90
- assign_collection(method_name, options[:count], value, &block)
91
- else
92
- assign_field(method_name, value, &block)
93
- end
94
- end
95
-
96
- def assign_field(field_name, value, &block)
97
- __attributes[field_name] = block_given? ? yield(__attributes) : value
98
- end
99
-
100
- def assign_collection(field_name, count, value, &block)
101
- __attributes[field_name] = block_given? ?
102
- (1..count).map { |i| yield(__attributes, i) } :
103
- value * count
104
- end
105
-
106
86
  def post_initialize; end
107
87
 
108
88
  def process_attributes(attributes)
109
89
  attributes.each do |attribute|
110
90
  __transient_fields << attribute.name if attribute.transient?
111
- if Proc === attribute.value
112
- method_missing(attribute.name, attribute.params, &attribute.value)
113
- else
114
- method_missing(attribute.name, attribute.value)
115
- end
91
+ __attributes[attribute.name] = attribute.processed_value(__attributes)
116
92
  end
117
93
  __attributes.reject! { |k| __transient_fields.include?(k) }
118
94
  end
@@ -0,0 +1,11 @@
1
+ class Fabrication::Generator::Keymaker < Fabrication::Generator::Base
2
+
3
+ def self.supports?(klass)
4
+ defined?(Keymaker) && klass.ancestors.include?(Keymaker::Node)
5
+ end
6
+
7
+ def persist
8
+ __instance.save
9
+ end
10
+
11
+ end
@@ -1,8 +1,9 @@
1
1
  class Fabrication::Schematic::Attribute
2
2
 
3
- attr_accessor :name, :params, :value
3
+ attr_accessor :klass, :name, :params, :value
4
4
 
5
- def initialize(name, value, params={}, &block)
5
+ def initialize(klass, name, value, params={}, &block)
6
+ self.klass = klass
6
7
  self.name = name
7
8
  self.params = params
8
9
  self.value = value.nil? ? block : value
@@ -20,4 +21,23 @@ class Fabrication::Schematic::Attribute
20
21
  params[:transient]
21
22
  end
22
23
 
24
+ def processed_value(processed_attributes)
25
+ if count
26
+ (1..count).map { |i| execute(processed_attributes, i, &value) }
27
+ elsif value_proc?
28
+ execute(processed_attributes, &value)
29
+ else
30
+ value
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def execute(*args, &block)
37
+ Fabrication::Schematic::Runner.new(klass).instance_exec(*args, &block)
38
+ end
39
+
40
+ def count; params[:count] end
41
+ def value_proc?; Proc === value end
42
+
23
43
  end
@@ -5,23 +5,18 @@ class Fabrication::Schematic::Definition
5
5
  Fabrication::Generator::DataMapper,
6
6
  Fabrication::Generator::Sequel,
7
7
  Fabrication::Generator::Mongoid,
8
+ Fabrication::Generator::Keymaker,
8
9
  Fabrication::Generator::Base
9
10
  ]
10
11
 
11
12
  attr_accessor :klass
12
13
  def initialize(klass, &block)
13
14
  self.klass = klass
14
- instance_eval(&block) if block_given?
15
+ process_block(&block)
15
16
  end
16
17
 
17
- def after_build(&block)
18
- callbacks[:after_build] ||= []
19
- callbacks[:after_build] << block
20
- end
21
-
22
- def after_create(&block)
23
- callbacks[:after_create] ||= []
24
- callbacks[:after_create] << block
18
+ def process_block(&block)
19
+ Fabrication::Schematic::Evaluator.new.process(self, &block) if block_given?
25
20
  end
26
21
 
27
22
  def attribute(name)
@@ -29,7 +24,7 @@ class Fabrication::Schematic::Definition
29
24
  end
30
25
 
31
26
  def append_or_update_attribute(attribute_name, value, params={}, &block)
32
- attribute = Fabrication::Schematic::Attribute.new(attribute_name, value, params, &block)
27
+ attribute = Fabrication::Schematic::Attribute.new(klass, attribute_name, value, params, &block)
33
28
  if index = attributes.index { |a| a.name == attribute.name }
34
29
  attribute.transient! if attributes[index].transient?
35
30
  attributes[index] = attribute
@@ -86,53 +81,21 @@ class Fabrication::Schematic::Definition
86
81
  self.attributes = original.attributes.clone
87
82
  end
88
83
 
89
- def init_with(*args)
90
- args
91
- end
92
-
93
84
  def merge(overrides={}, &block)
94
- clone.tap do |schematic|
95
- schematic.instance_eval(&block) if block_given?
85
+ clone.tap do |definition|
86
+ definition.process_block(&block)
96
87
  overrides.each do |name, value|
97
- schematic.append_or_update_attribute(name.to_sym, value)
88
+ definition.append_or_update_attribute(name.to_sym, value)
98
89
  end
99
90
  end
100
91
  end
101
92
 
102
- def method_missing(method_name, *args, &block)
103
- params = Fabrication::Support.extract_options!(args)
104
- value = args.first
105
- block = generate_value(method_name, params) if args.empty? && !block_given?
106
- append_or_update_attribute(method_name, value, params, &block)
107
- end
108
-
109
- def on_init(&block)
110
- callbacks[:on_init] = block
111
- end
112
-
113
- def initialize_with(&block)
114
- callbacks[:initialize_with] = block
115
- end
116
-
117
- def transient(*field_names)
118
- field_names.each do |field_name|
119
- append_or_update_attribute(field_name, nil, transient: true)
120
- end
121
- end
122
-
123
- def sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
124
- name = "#{self.klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
125
- Fabrication::Sequencer.sequence(name, start, &block)
126
- end
127
-
128
- private
129
-
130
93
  def generate_value(name, params)
131
94
  if params[:count]
132
95
  name = name.to_s.singularize if name.to_s.respond_to?(:singularize)
133
- Proc.new { Fabricate.build(params[:fabricator] || name) }
96
+ proc { Fabricate.build(params[:fabricator] || name) }
134
97
  else
135
- Proc.new { Fabricate(params[:fabricator] || name) }
98
+ proc { Fabricate(params[:fabricator] || name) }
136
99
  end
137
100
  end
138
101
  end
@@ -0,0 +1,42 @@
1
+ class Fabrication::Schematic::Evaluator < BasicObject
2
+
3
+ def process(definition, &block)
4
+ @_definition = definition
5
+ instance_eval(&block)
6
+ end
7
+
8
+ def method_missing(method_name, *args, &block)
9
+ params = ::Fabrication::Support.extract_options!(args)
10
+ value = args.first
11
+ block = @_definition.generate_value(method_name, params) if args.empty? && !block
12
+ @_definition.append_or_update_attribute(method_name, value, params, &block)
13
+ end
14
+
15
+ def after_build(&block)
16
+ @_definition.callbacks[:after_build] ||= []
17
+ @_definition.callbacks[:after_build] << block
18
+ end
19
+
20
+ def after_create(&block)
21
+ @_definition.callbacks[:after_create] ||= []
22
+ @_definition.callbacks[:after_create] << block
23
+ end
24
+
25
+ def on_init(&block)
26
+ @_definition.callbacks[:on_init] = block
27
+ end
28
+
29
+ def initialize_with(&block)
30
+ @_definition.callbacks[:initialize_with] = block
31
+ end
32
+
33
+ def init_with(*args)
34
+ args
35
+ end
36
+
37
+ def transient(*field_names)
38
+ field_names.each do |field_name|
39
+ @_definition.append_or_update_attribute(field_name, nil, transient: true)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ class Fabrication::Schematic::Runner
2
+
3
+ attr_accessor :klass
4
+
5
+ def initialize(klass)
6
+ self.klass = klass
7
+ end
8
+
9
+ def sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
10
+ name = "#{klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
11
+ Fabrication::Sequencer.sequence(name, start, &block)
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.4.0'
2
+ VERSION = '2.5.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-13 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -171,6 +171,22 @@ dependencies:
171
171
  - - ! '>='
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: keymaker
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
174
190
  - !ruby/object:Gem::Dependency
175
191
  name: mongoid
176
192
  requirement: !ruby/object:Gem::Requirement
@@ -269,11 +285,14 @@ files:
269
285
  - lib/fabrication/generator/active_record.rb
270
286
  - lib/fabrication/generator/base.rb
271
287
  - lib/fabrication/generator/data_mapper.rb
288
+ - lib/fabrication/generator/keymaker.rb
272
289
  - lib/fabrication/generator/mongoid.rb
273
290
  - lib/fabrication/generator/sequel.rb
274
291
  - lib/fabrication/schematic/attribute.rb
275
292
  - lib/fabrication/schematic/definition.rb
293
+ - lib/fabrication/schematic/evaluator.rb
276
294
  - lib/fabrication/schematic/manager.rb
295
+ - lib/fabrication/schematic/runner.rb
277
296
  - lib/fabrication/sequencer.rb
278
297
  - lib/fabrication/support.rb
279
298
  - lib/fabrication/syntax/make.rb