vitalish-factory_girl 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.rdoc +67 -121
  2. data/Rakefile +103 -29
  3. data/VERSION +1 -0
  4. data/lib/factory_girl/aliases.rb +32 -3
  5. data/lib/factory_girl/attribute/association.rb +1 -1
  6. data/lib/factory_girl/attribute/callback.rb +1 -1
  7. data/lib/factory_girl/attribute/dynamic.rb +3 -3
  8. data/lib/factory_girl/attribute/static.rb +1 -1
  9. data/lib/factory_girl/attribute.rb +3 -3
  10. data/lib/factory_girl/factory.rb +354 -164
  11. data/lib/factory_girl/proxy/attributes_for.rb +1 -1
  12. data/lib/factory_girl/proxy/build.rb +5 -7
  13. data/lib/factory_girl/proxy/create.rb +3 -2
  14. data/lib/factory_girl/proxy/stub.rb +5 -19
  15. data/lib/factory_girl/proxy.rb +23 -7
  16. data/lib/factory_girl/sequence.rb +40 -5
  17. data/lib/factory_girl/step_definitions.rb +13 -19
  18. data/lib/factory_girl/syntax/blueprint.rb +5 -5
  19. data/lib/factory_girl/syntax/generate.rb +8 -13
  20. data/lib/factory_girl/syntax/make.rb +7 -9
  21. data/lib/factory_girl/syntax/sham.rb +8 -11
  22. data/lib/factory_girl/syntax.rb +7 -7
  23. data/lib/factory_girl.rb +19 -8
  24. data/spec/factory_girl/aliases_spec.rb +5 -9
  25. data/spec/factory_girl/attribute/association_spec.rb +4 -4
  26. data/spec/factory_girl/attribute/callback_spec.rb +4 -4
  27. data/spec/factory_girl/attribute/dynamic_spec.rb +10 -21
  28. data/spec/factory_girl/attribute/static_spec.rb +6 -6
  29. data/spec/factory_girl/attribute_spec.rb +6 -6
  30. data/spec/factory_girl/factory_spec.rb +465 -305
  31. data/spec/factory_girl/proxy/attributes_for_spec.rb +3 -3
  32. data/spec/factory_girl/proxy/build_spec.rb +13 -18
  33. data/spec/factory_girl/proxy/create_spec.rb +13 -18
  34. data/spec/factory_girl/proxy/stub_spec.rb +6 -7
  35. data/spec/factory_girl/proxy_spec.rb +3 -3
  36. data/spec/factory_girl/sequence_spec.rb +39 -16
  37. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  38. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  39. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  40. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  41. data/spec/integration_spec.rb +304 -0
  42. data/spec/models.rb +43 -0
  43. data/spec/spec_helper.rb +10 -85
  44. metadata +15 -3
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.6
@@ -1,14 +1,42 @@
1
- module FactoryGirl
1
+ class Factory
2
2
 
3
3
  class << self
4
4
  attr_accessor :aliases #:nodoc:
5
5
  end
6
6
  self.aliases = [
7
- [/(.+)_id/, '\1'],
7
+ [/(.*)_id/, '\1'],
8
8
  [/(.*)/, '\1_id']
9
9
  ]
10
10
 
11
- def self.aliases_for(attribute) #:nodoc:
11
+ # Defines a new alias for attributes.
12
+ #
13
+ # Arguments:
14
+ # * pattern: +Regexp+
15
+ # A pattern that will be matched against attributes when looking for
16
+ # aliases. Contents captured in the pattern can be used in the alias.
17
+ # * replace: +String+
18
+ # The alias that results from the matched pattern. Captured strings can
19
+ # be substituded like with +String#sub+.
20
+ #
21
+ # Example:
22
+ #
23
+ # Factory.alias /(.*)_confirmation/, '\1'
24
+ #
25
+ # factory_girl starts with aliases for foreign keys, so that a :user
26
+ # association can be overridden by a :user_id parameter:
27
+ #
28
+ # Factory.define :post do |p|
29
+ # p.association :user
30
+ # end
31
+ #
32
+ # # The user association will not be built in this example. The user_id
33
+ # # will be used instead.
34
+ # Factory(:post, :user_id => 1)
35
+ def self.alias (pattern, replace)
36
+ self.aliases << [pattern, replace]
37
+ end
38
+
39
+ def self.aliases_for (attribute) #:nodoc:
12
40
  aliases.collect do |params|
13
41
  pattern, replace = *params
14
42
  if pattern.match(attribute.to_s)
@@ -18,4 +46,5 @@ module FactoryGirl
18
46
  end
19
47
  end.compact << attribute
20
48
  end
49
+
21
50
  end
@@ -1,4 +1,4 @@
1
- module FactoryGirl
1
+ class Factory
2
2
  class Attribute #:nodoc:
3
3
 
4
4
  class Association < Attribute #:nodoc:
@@ -1,4 +1,4 @@
1
- module FactoryGirl
1
+ class Factory
2
2
  class Attribute #:nodoc:
3
3
 
4
4
  class Callback < Attribute #:nodoc:
@@ -1,4 +1,4 @@
1
- module FactoryGirl
1
+ class Factory
2
2
  class Attribute #:nodoc:
3
3
 
4
4
  class Dynamic < Attribute #:nodoc:
@@ -8,8 +8,8 @@ module FactoryGirl
8
8
  end
9
9
 
10
10
  def add_to(proxy)
11
- value = @block.arity == 1 ? @block.call(proxy) : proxy.instance_eval(&@block)
12
- if FactoryGirl::Sequence === value
11
+ value = @block.arity.zero? ? @block.call : @block.call(proxy)
12
+ if Factory::Sequence === value
13
13
  raise SequenceAbuseError
14
14
  end
15
15
  proxy.set(name, value)
@@ -1,4 +1,4 @@
1
- module FactoryGirl
1
+ class Factory
2
2
  class Attribute #:nodoc:
3
3
 
4
4
  class Static < Attribute #:nodoc:
@@ -1,4 +1,4 @@
1
- module FactoryGirl
1
+ class Factory
2
2
 
3
3
  # Raised when defining an invalid attribute:
4
4
  # * Defining an attribute which has a name ending in "="
@@ -6,7 +6,7 @@ module FactoryGirl
6
6
  # * Defining an attribute twice in the same factory
7
7
  class AttributeDefinitionError < RuntimeError
8
8
  end
9
-
9
+
10
10
  class Attribute #:nodoc:
11
11
 
12
12
  attr_reader :name
@@ -16,7 +16,7 @@ module FactoryGirl
16
16
 
17
17
  if @name.to_s =~ /=$/
18
18
  attribute_name = $`
19
- raise AttributeDefinitionError,
19
+ raise AttributeDefinitionError,
20
20
  "factory_girl uses 'f.#{attribute_name} value' syntax " +
21
21
  "rather than 'f.#{attribute_name} = value'"
22
22
  end
@@ -1,215 +1,405 @@
1
- module FactoryGirl
1
+ class Factory
2
+
3
+ # Raised when a factory is defined that attempts to instantiate itself.
4
+ class AssociationDefinitionError < RuntimeError
5
+ end
6
+
7
+ # Raised when a callback is defined that has an invalid name
8
+ class InvalidCallbackNameError < RuntimeError
9
+ end
10
+
2
11
  class << self
3
12
  attr_accessor :factories #:nodoc:
13
+
14
+ # An Array of strings specifying locations that should be searched for
15
+ # factory definitions. By default, factory_girl will attempt to require
16
+ # "factories," "test/factories," and "spec/factories." Only the first
17
+ # existing file will be loaded.
18
+ attr_accessor :definition_file_paths
4
19
  end
5
20
 
6
21
  self.factories = {}
22
+ self.definition_file_paths = %w(factories test/factories spec/factories)
23
+
24
+ attr_reader :factory_name #:nodoc:
25
+ attr_reader :attributes #:nodoc:
26
+
27
+ # Defines a new factory that can be used by the build strategies (create and
28
+ # build) to build new objects.
29
+ #
30
+ # Arguments:
31
+ # * name: +Symbol+ or +String+
32
+ # A unique name used to identify this factory.
33
+ # * options: +Hash+
34
+ #
35
+ # Options:
36
+ # * class: +Symbol+, +Class+, or +String+
37
+ # The class that will be used when generating instances for this factory. If not specified, the class will be guessed from the factory name.
38
+ # * parent: +Symbol+
39
+ # The parent factory. If specified, the attributes from the parent
40
+ # factory will be copied to the current one with an ability to override
41
+ # them.
42
+ # * default_strategy: +Symbol+
43
+ # The strategy that will be used by the Factory shortcut method.
44
+ # Defaults to :create.
45
+ #
46
+ # Yields: +Factory+
47
+ # The newly created factory.
48
+ def self.define (name, options = {})
49
+ instance = Factory.new(name, options)
50
+ yield(instance)
51
+ if parent = options.delete(:parent)
52
+ instance.inherit_from(Factory.factory_by_name(parent))
53
+ end
54
+ self.factories[instance.factory_name] = instance
55
+ end
56
+
57
+ def class_name #:nodoc:
58
+ @options[:class] || factory_name
59
+ end
7
60
 
8
- def self.factory_by_name(name)
9
- factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
61
+ def build_class #:nodoc:
62
+ @build_class ||= class_for(class_name)
63
+ end
64
+
65
+ def default_strategy #:nodoc:
66
+ @options[:default_strategy] || :create
10
67
  end
11
68
 
12
- def self.register_factory(factory, options = {})
13
- if options[:as]
14
- name = options[:as]
15
- else
16
- name = factory.name
17
- factory.aliases.each do |alias_name|
18
- register_factory(factory, :as => alias_name)
69
+ def initialize (name, options = {}) #:nodoc:
70
+ assert_valid_options(options)
71
+ @factory_name = factory_name_for(name)
72
+ @options = options
73
+ @attributes = []
74
+ @params = []
75
+ end
76
+
77
+ def inherit_from(parent) #:nodoc:
78
+ @options[:class] ||= parent.class_name
79
+ parent.attributes.each do |attribute|
80
+ unless attribute_defined?(attribute.name)
81
+ @attributes << attribute.clone
19
82
  end
20
83
  end
84
+ end
21
85
 
22
- if self.factories[name]
23
- raise DuplicateDefinitionError, "Factory already defined: #{name}"
86
+ # Adds an attribute that should be assigned on generated instances for this
87
+ # factory.
88
+ #
89
+ # This method should be called with either a value or block, but not both. If
90
+ # called with a block, the attribute will be generated "lazily," whenever an
91
+ # instance is generated. Lazy attribute blocks will not be called if that
92
+ # attribute is overriden for a specific instance.
93
+ #
94
+ # When defining lazy attributes, an instance of Factory::Proxy will
95
+ # be yielded, allowing associations to be built using the correct build
96
+ # strategy.
97
+ #
98
+ # Arguments:
99
+ # * name: +Symbol+ or +String+
100
+ # The name of this attribute. This will be assigned using :"#{name}=" for
101
+ # generated instances.
102
+ # * value: +Object+
103
+ # If no block is given, this value will be used for this attribute.
104
+ def add_attribute (name, value = nil, &block)
105
+ if block_given?
106
+ if value
107
+ raise AttributeDefinitionError, "Both value and block given"
108
+ else
109
+ attribute = Attribute::Dynamic.new(name, block)
110
+ end
111
+ else
112
+ attribute = Attribute::Static.new(name, value)
24
113
  end
25
114
 
26
- self.factories[name] = factory
27
- end
28
-
29
- # Raised when a factory is defined that attempts to instantiate itself.
30
- class AssociationDefinitionError < RuntimeError
31
- end
115
+ if attribute_defined?(attribute.name)
116
+ raise AttributeDefinitionError, "Attribute already defined: #{name}"
117
+ end
32
118
 
33
- # Raised when a callback is defined that has an invalid name
34
- class InvalidCallbackNameError < RuntimeError
119
+ @attributes << attribute
35
120
  end
36
121
 
37
- # Raised when a factory is defined with the same name as a previously-defined factory.
38
- class DuplicateDefinitionError < RuntimeError
122
+ # Calls add_attribute using the missing method name as the name of the
123
+ # attribute, so that:
124
+ #
125
+ # Factory.define :user do |f|
126
+ # f.name 'Billy Idol'
127
+ # end
128
+ #
129
+ # and:
130
+ #
131
+ # Factory.define :user do |f|
132
+ # f.add_attribute :name, 'Billy Idol'
133
+ # end
134
+ #
135
+ # are equivilent.
136
+ def method_missing (name, *args, &block)
137
+ add_attribute(name, *args, &block)
39
138
  end
40
139
 
41
- class Factory
42
- attr_reader :name #:nodoc:
43
- attr_reader :attributes #:nodoc:
44
-
45
- def factory_name
46
- puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
47
- name
48
- end
49
-
50
- def class_name #:nodoc:
51
- @options[:class] || name
140
+ # Adds an attribute that builds an association. The associated instance will
141
+ # be built using the same build strategy as the parent instance.
142
+ #
143
+ # Example:
144
+ # Factory.define :user do |f|
145
+ # f.name 'Joey'
146
+ # end
147
+ #
148
+ # Factory.define :post do |f|
149
+ # f.association :author, :factory => :user
150
+ # end
151
+ #
152
+ # Arguments:
153
+ # * name: +Symbol+
154
+ # The name of this attribute.
155
+ # * options: +Hash+
156
+ #
157
+ # Options:
158
+ # * factory: +Symbol+ or +String+
159
+ # The name of the factory to use when building the associated instance.
160
+ # If no name is given, the name of the attribute is assumed to be the
161
+ # name of the factory. For example, a "user" association will by
162
+ # default use the "user" factory.
163
+ def association (name, options = {})
164
+ factory_name = options.delete(:factory) || name
165
+ if factory_name_for(factory_name) == self.factory_name
166
+ raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'"
52
167
  end
168
+ @attributes << Attribute::Association.new(name, factory_name, options)
169
+ end
53
170
 
54
- def build_class #:nodoc:
55
- @build_class ||= class_for(class_name)
171
+ # Adds an attribute that will have unique values generated by a sequence with
172
+ # a specified format.
173
+ #
174
+ # The result of:
175
+ # Factory.define :user do |f|
176
+ # f.sequence(:email) { |n| "person#{n}@example.com" }
177
+ # end
178
+ #
179
+ # Is equal to:
180
+ # Factory.sequence(:email) { |n| "person#{n}@example.com" }
181
+ #
182
+ # Factory.define :user do |f|
183
+ # f.email { Factory.next(:email) }
184
+ # end
185
+ #
186
+ # Except that no globally available sequence will be defined.
187
+ def sequence (name, &block)
188
+ s = Sequence.new(&block)
189
+ add_attribute(name) { s.next }
190
+ end
191
+
192
+ def param(name)
193
+ @params << name
194
+ end
195
+
196
+ def after_build(&block)
197
+ callback(:after_build, &block)
198
+ end
199
+
200
+ def after_create(&block)
201
+ callback(:after_create, &block)
202
+ end
203
+
204
+ def after_stub(&block)
205
+ callback(:after_stub, &block)
206
+ end
207
+
208
+ def callback(name, &block)
209
+ unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
210
+ raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
56
211
  end
212
+ @attributes << Attribute::Callback.new(name.to_sym, block)
213
+ end
214
+
215
+ # Generates and returns a Hash of attributes from this factory. Attributes
216
+ # can be individually overridden by passing in a Hash of attribute => value
217
+ # pairs.
218
+ #
219
+ # Arguments:
220
+ # * name: +Symbol+ or +String+
221
+ # The name of the factory that should be used.
222
+ # * overrides: +Hash+
223
+ # Attributes to overwrite for this set.
224
+ #
225
+ # Returns: +Hash+
226
+ # A set of attributes that can be used to build an instance of the class
227
+ # this factory generates.
228
+ def self.attributes_for (name, overrides = {})
229
+ factory_by_name(name).run(Proxy::AttributesFor, overrides)
230
+ end
57
231
 
58
- def default_strategy #:nodoc:
59
- @options[:default_strategy] || :create
60
- end
232
+ # Generates and returns an instance from this factory. Attributes can be
233
+ # individually overridden by passing in a Hash of attribute => value pairs.
234
+ #
235
+ # Arguments:
236
+ # * name: +Symbol+ or +String+
237
+ # The name of the factory that should be used.
238
+ # * overrides: +Hash+
239
+ # Attributes to overwrite for this instance.
240
+ #
241
+ # Returns: +Object+
242
+ # An instance of the class this factory generates, with generated attributes
243
+ # assigned.
244
+ def self.build (name, overrides = {})
245
+ factory_by_name(name).run(Proxy::Build, overrides)
246
+ end
61
247
 
62
- def initialize(name, options = {}) #:nodoc:
63
- assert_valid_options(options)
64
- @name = factory_name_for(name)
65
- @options = options
66
- @attributes = []
67
- end
248
+ # Generates, saves, and returns an instance from this factory. Attributes can
249
+ # be individually overridden by passing in a Hash of attribute => value
250
+ # pairs.
251
+ #
252
+ # Instances are saved using the +save!+ method, so ActiveRecord models will
253
+ # raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
254
+ #
255
+ # Arguments:
256
+ # * name: +Symbol+ or +String+
257
+ # The name of the factory that should be used.
258
+ # * overrides: +Hash+
259
+ # Attributes to overwrite for this instance.
260
+ #
261
+ # Returns: +Object+
262
+ # A saved instance of the class this factory generates, with generated
263
+ # attributes assigned.
264
+ def self.create (name, overrides = {})
265
+ factory_by_name(name).run(Proxy::Create, overrides)
266
+ end
267
+
268
+ # Generates and returns an object with all attributes from this factory
269
+ # stubbed out. Attributes can be individually overridden by passing in a Hash
270
+ # of attribute => value pairs.
271
+ #
272
+ # Arguments:
273
+ # * name: +Symbol+ or +String+
274
+ # The name of the factory that should be used.
275
+ # * overrides: +Hash+
276
+ # Attributes to overwrite for this instance.
277
+ #
278
+ # Returns: +Object+
279
+ # An object with generated attributes stubbed out.
280
+ def self.stub (name, overrides = {})
281
+ factory_by_name(name).run(Proxy::Stub, overrides)
282
+ end
283
+
284
+ # Executes the default strategy for the given factory. This is usually create,
285
+ # but it can be overridden for each factory.
286
+ #
287
+ # Arguments:
288
+ # * name: +Symbol+ or +String+
289
+ # The name of the factory that should be used.
290
+ # * overrides: +Hash+
291
+ # Attributes to overwrite for this instance.
292
+ #
293
+ # Returns: +Object+
294
+ # The result of the default strategy.
295
+ def self.default_strategy (name, overrides = {})
296
+ self.send(factory_by_name(name).default_strategy, name, overrides)
297
+ end
68
298
 
69
- def inherit_from(parent) #:nodoc:
70
- @options[:class] ||= parent.class_name
71
- @options[:default_strategy] ||= parent.default_strategy
299
+ def self.find_definitions #:nodoc:
300
+ definition_file_paths.each do |path|
301
+ require("#{path}.rb") if File.exists?("#{path}.rb")
72
302
 
73
- new_attributes = []
74
- parent.attributes.each do |attribute|
75
- unless attribute_defined?(attribute.name)
76
- new_attributes << attribute.clone
303
+ if File.directory? path
304
+ Dir[File.join(path, '*.rb')].each do |file|
305
+ require file
77
306
  end
78
307
  end
79
- @attributes.unshift *new_attributes
80
- end
81
-
82
- def define_attribute(attribute)
83
- name = attribute.name
84
- # TODO: move these checks into Attribute
85
- if attribute_defined?(name)
86
- raise AttributeDefinitionError, "Attribute already defined: #{name}"
87
- end
88
- if attribute.respond_to?(:factory) && attribute.factory == self.name
89
- raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'"
90
- end
91
- @attributes << attribute
92
308
  end
309
+ end
93
310
 
94
- def add_callback(name, &block)
95
- unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
96
- raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
97
- end
98
- @attributes << Attribute::Callback.new(name.to_sym, block)
311
+ def run (proxy_class, overrides) #:nodoc:
312
+ proxy = proxy_class.new(build_class)
313
+ overrides = symbolize_keys(overrides)
314
+ overrides = overrides.delete_if do |attr, val|
315
+ attr_is_param = @params.include?(attr)
316
+ proxy.set_param(attr,val) if attr_is_param
317
+ attr_is_param
99
318
  end
100
-
101
- def run (proxy_class, overrides) #:nodoc:
102
- proxy = proxy_class.new(build_class)
103
- overrides = symbolize_keys(overrides)
104
- overrides.each {|attr, val| proxy.set(attr, val) }
105
- passed_keys = overrides.keys.collect {|k| FactoryGirl.aliases_for(k) }.flatten
106
- @attributes.each do |attribute|
107
- unless passed_keys.include?(attribute.name)
108
- attribute.add_to(proxy)
109
- end
319
+ overrides.each {|attr, val| proxy.set(attr, val) }
320
+ passed_keys = overrides.keys.collect {|k| Factory.aliases_for(k) }.flatten
321
+ @attributes.each do |attribute|
322
+ unless passed_keys.include?(attribute.name)
323
+ attribute.add_to(proxy)
110
324
  end
111
- proxy.result
112
325
  end
326
+ proxy.result
327
+ end
113
328
 
114
- def human_name(*args, &block)
115
- name.to_s.gsub('_', ' ')
116
- end
329
+ def self.factory_by_name (name)
330
+ factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
331
+ end
117
332
 
118
- def associations
119
- attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
333
+ def human_name(*args, &block)
334
+ if args.size == 0 && block.nil?
335
+ factory_name.to_s.gsub('_', ' ')
336
+ else
337
+ add_attribute(:human_name, *args, &block)
120
338
  end
339
+ end
121
340
 
122
- # Alternate names for this factory.
123
- #
124
- # Example:
125
- #
126
- # factory :user, :aliases => [:author] do
127
- # # ...
128
- # end
129
- #
130
- # Factory(:author).class
131
- # # => User
132
- #
133
- # Because an attribute defined without a value or block will build an
134
- # association with the same name, this allows associations to be defined
135
- # without factories, such as:
136
- #
137
- # factory :user, :aliases => [:author] do
138
- # # ...
139
- # end
140
- #
141
- # factory :post do
142
- # author
143
- # end
144
- #
145
- # Factory(:post).author.class
146
- # # => User
147
- def aliases
148
- @options[:aliases] || []
149
- end
341
+ def associations
342
+ attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
343
+ end
150
344
 
151
- private
345
+ private
152
346
 
153
- def class_for (class_or_to_s)
154
- if class_or_to_s.respond_to?(:to_sym)
155
- class_name = variable_name_to_class_name(class_or_to_s)
156
- class_name.split('::').inject(Object) do |object, string|
157
- object.const_get(string)
158
- end
159
- else
160
- class_or_to_s
161
- end
347
+ def class_for (class_or_to_s)
348
+ if class_or_to_s.respond_to?(:to_sym)
349
+ Object.const_get(variable_name_to_class_name(class_or_to_s))
350
+ else
351
+ class_or_to_s
162
352
  end
353
+ end
163
354
 
164
- def factory_name_for(class_or_to_s)
165
- if class_or_to_s.respond_to?(:to_sym)
166
- class_or_to_s.to_sym
167
- else
168
- class_name_to_variable_name(class_or_to_s).to_sym
169
- end
355
+ def factory_name_for (class_or_to_s)
356
+ if class_or_to_s.respond_to?(:to_sym)
357
+ class_or_to_s.to_sym
358
+ else
359
+ class_name_to_variable_name(class_or_to_s).to_sym
170
360
  end
361
+ end
171
362
 
172
- def attribute_defined? (name)
173
- !@attributes.detect {|attr| attr.name == name && !attr.is_a?(Attribute::Callback) }.nil?
174
- end
363
+ def attribute_defined? (name)
364
+ !@attributes.detect {|attr| attr.name == name && !attr.is_a?(Factory::Attribute::Callback) }.nil?
365
+ end
175
366
 
176
- def assert_valid_options(options)
177
- invalid_keys = options.keys - [:class, :parent, :default_strategy, :aliases]
178
- unless invalid_keys == []
179
- raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
180
- end
181
- assert_valid_strategy(options[:default_strategy]) if options[:default_strategy]
367
+ def assert_valid_options(options)
368
+ invalid_keys = options.keys - [:class, :parent, :default_strategy]
369
+ unless invalid_keys == []
370
+ raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
182
371
  end
183
-
184
- def assert_valid_strategy(strategy)
185
- unless Proxy.const_defined? variable_name_to_class_name(strategy)
186
- raise ArgumentError, "Unknown strategy: #{strategy}"
187
- end
372
+ assert_valid_strategy(options[:default_strategy]) if options[:default_strategy]
373
+ end
374
+
375
+ def assert_valid_strategy(strategy)
376
+ unless Factory::Proxy.const_defined? variable_name_to_class_name(strategy)
377
+ raise ArgumentError, "Unknown strategy: #{strategy}"
188
378
  end
379
+ end
189
380
 
190
- # Based on ActiveSupport's underscore inflector
191
- def class_name_to_variable_name(name)
192
- name.to_s.gsub(/::/, '/').
193
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
194
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
195
- tr("-", "_").
196
- downcase
197
- end
381
+ # Based on ActiveSupport's underscore inflector
382
+ def class_name_to_variable_name(name)
383
+ name.to_s.gsub(/::/, '/').
384
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
385
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
386
+ tr("-", "_").
387
+ downcase
388
+ end
198
389
 
199
- # Based on ActiveSupport's camelize inflector
200
- def variable_name_to_class_name(name)
201
- name.to_s.
202
- gsub(/\/(.?)/) { "::#{$1.upcase}" }.
203
- gsub(/(?:^|_)(.)/) { $1.upcase }
204
- end
390
+ # Based on ActiveSupport's camelize inflector
391
+ def variable_name_to_class_name(name)
392
+ name.to_s.
393
+ gsub(/\/(.?)/) { "::#{$1.upcase}" }.
394
+ gsub(/(?:^|_)(.)/) { $1.upcase }
395
+ end
205
396
 
206
- # From ActiveSupport
207
- def symbolize_keys(hash)
208
- hash.inject({}) do |options, (key, value)|
209
- options[(key.to_sym rescue key) || key] = value
210
- options
211
- end
397
+ # From ActiveSupport
398
+ def symbolize_keys(hash)
399
+ hash.inject({}) do |options, (key, value)|
400
+ options[(key.to_sym rescue key) || key] = value
401
+ options
212
402
  end
213
-
214
403
  end
404
+
215
405
  end