challah 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.md +9 -2
  2. data/README.md +18 -18
  3. data/lib/challah/test.rb +22 -5
  4. data/lib/challah/version.rb +1 -1
  5. data/test/audit_test.rb +32 -32
  6. data/test/controller_test.rb +9 -9
  7. data/test/cookie_store_test.rb +25 -25
  8. data/test/factories.rb +7 -7
  9. data/test/helper.rb +14 -12
  10. data/test/permission_test.rb +14 -14
  11. data/test/restrictions_controller_test.rb +29 -30
  12. data/test/role_test.rb +26 -26
  13. data/test/session_test.rb +65 -65
  14. data/test/sessions_controller_test.rb +13 -13
  15. data/test/simple_cookie_store_test.rb +25 -25
  16. data/test/user_test.rb +79 -79
  17. data/vendor/bundle/cache/factory_girl-3.0.0.gem +0 -0
  18. data/vendor/bundle/gems/factory_girl-3.0.0/Appraisals +11 -0
  19. data/vendor/bundle/gems/factory_girl-3.0.0/CONTRIBUTION_GUIDELINES.md +10 -0
  20. data/vendor/bundle/gems/factory_girl-3.0.0/Changelog +168 -0
  21. data/vendor/bundle/gems/factory_girl-3.0.0/GETTING_STARTED.md +735 -0
  22. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile +5 -0
  23. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile.lock +95 -0
  24. data/vendor/bundle/gems/factory_girl-3.0.0/LICENSE +19 -0
  25. data/vendor/bundle/gems/factory_girl-3.0.0/README.md +70 -0
  26. data/vendor/bundle/gems/factory_girl-3.0.0/Rakefile +38 -0
  27. data/vendor/bundle/gems/factory_girl-3.0.0/cucumber.yml +1 -0
  28. data/vendor/bundle/gems/factory_girl-3.0.0/factory_girl.gemspec +38 -0
  29. data/vendor/bundle/gems/factory_girl-3.0.0/features/factory_girl_steps.feature +237 -0
  30. data/vendor/bundle/gems/factory_girl-3.0.0/features/find_definitions.feature +75 -0
  31. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/database_steps.rb +42 -0
  32. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/factory_girl_steps.rb +43 -0
  33. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/env.rb +11 -0
  34. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/factories.rb +119 -0
  35. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/2.3.gemfile +7 -0
  36. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile +7 -0
  37. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile.lock +86 -0
  38. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile +7 -0
  39. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile.lock +95 -0
  40. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile +7 -0
  41. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile.lock +93 -0
  42. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl.rb +69 -0
  43. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/aliases.rb +19 -0
  44. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute.rb +40 -0
  45. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/association.rb +23 -0
  46. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/dynamic.rb +20 -0
  47. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/sequence.rb +17 -0
  48. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/static.rb +15 -0
  49. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_assigner.rb +73 -0
  50. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb +54 -0
  51. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/callback.rb +33 -0
  52. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration.rb +22 -0
  53. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/association.rb +25 -0
  54. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/dynamic.rb +25 -0
  55. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/implicit.rb +32 -0
  56. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/static.rb +25 -0
  57. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration_list.rb +48 -0
  58. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition.rb +76 -0
  59. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition_proxy.rb +159 -0
  60. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/errors.rb +19 -0
  61. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator.rb +76 -0
  62. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator_class_definer.rb +34 -0
  63. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory.rb +145 -0
  64. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory_runner.rb +24 -0
  65. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/find_definitions.rb +25 -0
  66. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_factory.rb +15 -0
  67. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_object.rb +7 -0
  68. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/registry.rb +46 -0
  69. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/reload.rb +8 -0
  70. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/sequence.rb +24 -0
  71. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/step_definitions.rb +130 -0
  72. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy.rb +32 -0
  73. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/attributes_for.rb +12 -0
  74. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/build.rb +15 -0
  75. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/create.rb +17 -0
  76. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/stub.rb +61 -0
  77. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy_calculator.rb +29 -0
  78. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax.rb +16 -0
  79. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/blueprint.rb +42 -0
  80. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/default.rb +56 -0
  81. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/generate.rb +70 -0
  82. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/make.rb +45 -0
  83. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/methods.rb +136 -0
  84. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/sham.rb +45 -0
  85. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/vintage.rb +130 -0
  86. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/trait.rb +29 -0
  87. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/version.rb +4 -0
  88. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/aliases_spec.rb +19 -0
  89. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_aliases_spec.rb +45 -0
  90. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_existing_on_object_spec.rb +68 -0
  91. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_for_spec.rb +89 -0
  92. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_from_instance_spec.rb +53 -0
  93. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_ordered_spec.rb +51 -0
  94. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_list_spec.rb +41 -0
  95. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_spec.rb +89 -0
  96. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_stubbed_spec.rb +104 -0
  97. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/callbacks_spec.rb +47 -0
  98. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_list_spec.rb +82 -0
  99. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_spec.rb +117 -0
  100. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/define_child_before_parent_spec.rb +21 -0
  101. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_spec.rb +26 -0
  102. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_without_block_spec.rb +15 -0
  103. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/initialize_with_spec.rb +147 -0
  104. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_factories_spec.rb +184 -0
  105. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_inherited_spec.rb +52 -0
  106. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/nested_attributes_spec.rb +32 -0
  107. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/overrides_spec.rb +61 -0
  108. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/parent_spec.rb +90 -0
  109. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/sequence_spec.rb +33 -0
  110. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/stub_spec.rb +62 -0
  111. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/blueprint_spec.rb +34 -0
  112. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/generate_spec.rb +59 -0
  113. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/make_spec.rb +52 -0
  114. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/sham_spec.rb +43 -0
  115. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/vintage_spec.rb +217 -0
  116. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/traits_spec.rb +421 -0
  117. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/transient_attributes_spec.rb +124 -0
  118. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/aliases_spec.rb +31 -0
  119. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/association_spec.rb +28 -0
  120. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/dynamic_spec.rb +52 -0
  121. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/sequence_spec.rb +16 -0
  122. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/static_spec.rb +19 -0
  123. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_list_spec.rb +78 -0
  124. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_spec.rb +16 -0
  125. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/callback_spec.rb +41 -0
  126. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration/implicit_spec.rb +25 -0
  127. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration_list_spec.rb +71 -0
  128. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_proxy_spec.rb +197 -0
  129. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_spec.rb +104 -0
  130. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/evaluator_class_definer_spec.rb +54 -0
  131. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/factory_spec.rb +279 -0
  132. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/find_definitions_spec.rb +110 -0
  133. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_factory_spec.rb +13 -0
  134. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_object_spec.rb +8 -0
  135. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/registry_spec.rb +81 -0
  136. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/sequence_spec.rb +47 -0
  137. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/attributes_for_spec.rb +18 -0
  138. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/build_spec.rb +7 -0
  139. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/create_spec.rb +13 -0
  140. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/stub_spec.rb +40 -0
  141. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_calculator_spec.rb +33 -0
  142. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_spec.rb +21 -0
  143. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl_spec.rb +22 -0
  144. data/vendor/bundle/gems/factory_girl-3.0.0/spec/spec_helper.rb +26 -0
  145. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/macros/define_constant.rb +86 -0
  146. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/callback.rb +9 -0
  147. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/declaration.rb +71 -0
  148. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/delegate.rb +44 -0
  149. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/trait.rb +9 -0
  150. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/shared_examples/strategy.rb +104 -0
  151. data/vendor/bundle/specifications/factory_girl-3.0.0.gemspec +62 -0
  152. metadata +147 -12
@@ -0,0 +1,33 @@
1
+ module FactoryGirl
2
+ class Callback
3
+ attr_reader :name, :block
4
+
5
+ def initialize(name, block)
6
+ @name = name.to_sym
7
+ @block = block
8
+ check_name
9
+ end
10
+
11
+ def run(instance, evaluator)
12
+ case block.arity
13
+ when 1 then block.call(instance)
14
+ when 2 then block.call(instance, evaluator)
15
+ else block.call
16
+ end
17
+ end
18
+
19
+ def ==(other)
20
+ name == other.name &&
21
+ block == other.block
22
+ end
23
+
24
+ private
25
+
26
+ def check_name
27
+ unless FactoryGirl.callback_names.include?(name)
28
+ raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
29
+ "Valid callback names are #{FactoryGirl.callback_names.inspect}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require "factory_girl/declaration/static"
2
+ require "factory_girl/declaration/dynamic"
3
+ require "factory_girl/declaration/association"
4
+ require "factory_girl/declaration/implicit"
5
+
6
+ module FactoryGirl
7
+ class Declaration
8
+ attr_reader :name
9
+
10
+ def initialize(name, ignored = false)
11
+ @name = name
12
+ @ignored = ignored
13
+ end
14
+
15
+ def to_attributes
16
+ build
17
+ end
18
+
19
+ protected
20
+ attr_reader :ignored
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ class Association < Declaration
4
+ def initialize(name, options)
5
+ super(name, false)
6
+ @options = options
7
+ end
8
+
9
+ def ==(other)
10
+ name == other.name &&
11
+ options == other.options
12
+ end
13
+
14
+ protected
15
+ attr_reader :options
16
+
17
+ private
18
+
19
+ def build
20
+ factory_name = @options.delete(:factory) || name
21
+ [Attribute::Association.new(name, factory_name, @options)]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ class Dynamic < Declaration
4
+ def initialize(name, ignored = false, block = nil)
5
+ super(name, ignored)
6
+ @block = block
7
+ end
8
+
9
+ def ==(other)
10
+ name == other.name &&
11
+ ignored == other.ignored &&
12
+ block == other.block
13
+ end
14
+
15
+ protected
16
+ attr_reader :block
17
+
18
+ private
19
+
20
+ def build
21
+ [Attribute::Dynamic.new(name, @ignored, @block)]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ class Implicit < Declaration
4
+ def initialize(name, factory = nil, ignored = false)
5
+ super(name, ignored)
6
+ @factory = factory
7
+ end
8
+
9
+ def ==(other)
10
+ name == other.name &&
11
+ factory == other.factory &&
12
+ ignored == other.ignored
13
+ end
14
+
15
+ protected
16
+ attr_reader :factory
17
+
18
+ private
19
+
20
+ def build
21
+ if FactoryGirl.factories.registered?(name)
22
+ [Attribute::Association.new(name, name, {})]
23
+ elsif FactoryGirl.sequences.registered?(name)
24
+ [Attribute::Sequence.new(name, name, @ignored)]
25
+ else
26
+ @factory.inherit_traits([name])
27
+ []
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ class Static < Declaration
4
+ def initialize(name, value, ignored = false)
5
+ super(name, ignored)
6
+ @value = value
7
+ end
8
+
9
+ def ==(other)
10
+ name == other.name &&
11
+ value == other.value &&
12
+ ignored == other.ignored
13
+ end
14
+
15
+ protected
16
+ attr_reader :value
17
+
18
+ private
19
+
20
+ def build
21
+ [Attribute::Static.new(name, @value, @ignored)]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ module FactoryGirl
2
+ class DeclarationList
3
+ include Enumerable
4
+
5
+ def initialize(name = nil)
6
+ @declarations = []
7
+ @name = name
8
+ @overridable = false
9
+ end
10
+
11
+ def declare_attribute(declaration)
12
+ delete_declaration(declaration) if overridable?
13
+
14
+ @declarations << declaration
15
+ declaration
16
+ end
17
+
18
+ def overridable
19
+ @overridable = true
20
+ end
21
+
22
+ def attribute_list
23
+ AttributeList.new(@name).tap do |list|
24
+ to_attributes.each do |attribute|
25
+ list.define_attribute(attribute)
26
+ end
27
+ end
28
+ end
29
+
30
+ def each(&block)
31
+ @declarations.each(&block)
32
+ end
33
+
34
+ private
35
+
36
+ def delete_declaration(declaration)
37
+ @declarations.delete_if {|decl| decl.name == declaration.name }
38
+ end
39
+
40
+ def to_attributes
41
+ @declarations.inject([]) {|result, declaration| result += declaration.to_attributes }
42
+ end
43
+
44
+ def overridable?
45
+ @overridable
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,76 @@
1
+ module FactoryGirl
2
+ class Definition
3
+ attr_reader :callbacks, :defined_traits, :declarations, :constructor
4
+
5
+ def initialize(name = nil, base_traits = [])
6
+ @declarations = DeclarationList.new(name)
7
+ @callbacks = []
8
+ @defined_traits = []
9
+ @to_create = lambda {|instance| instance.save! }
10
+ @base_traits = base_traits
11
+ @additional_traits = []
12
+ @constructor = nil
13
+ end
14
+
15
+ delegate :declare_attribute, to: :declarations
16
+
17
+ def attributes
18
+ @attributes ||= declarations.attribute_list
19
+ end
20
+
21
+ def compile
22
+ attributes
23
+ end
24
+
25
+ def processing_order
26
+ base_traits + [self] + additional_traits
27
+ end
28
+
29
+ def overridable
30
+ declarations.overridable
31
+ self
32
+ end
33
+
34
+ def inherit_traits(new_traits)
35
+ @additional_traits += new_traits
36
+ end
37
+
38
+ def add_callback(callback)
39
+ @callbacks << callback
40
+ end
41
+
42
+ def to_create(&block)
43
+ if block_given?
44
+ @to_create = block
45
+ else
46
+ @to_create
47
+ end
48
+ end
49
+
50
+ def define_trait(trait)
51
+ @defined_traits << trait
52
+ end
53
+
54
+ def define_constructor(&block)
55
+ @constructor = block
56
+ end
57
+
58
+ private
59
+
60
+ def base_traits
61
+ @base_traits.map { |name| trait_by_name(name) }
62
+ end
63
+
64
+ def additional_traits
65
+ @additional_traits.map { |name| trait_by_name(name) }
66
+ end
67
+
68
+ def trait_by_name(name)
69
+ trait_for(name) || FactoryGirl.trait_by_name(name)
70
+ end
71
+
72
+ def trait_for(name)
73
+ defined_traits.detect {|trait| trait.name == name }
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,159 @@
1
+ module FactoryGirl
2
+ class DefinitionProxy
3
+ UNPROXIED_METHODS = %w(__send__ __id__ nil? send object_id extend instance_eval initialize block_given? raise)
4
+
5
+ (instance_methods + private_instance_methods).each do |method|
6
+ undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
7
+ end
8
+
9
+ attr_reader :child_factories
10
+
11
+ def initialize(definition, ignore = false)
12
+ @definition = definition
13
+ @ignore = ignore
14
+ @child_factories = []
15
+ end
16
+
17
+ # Adds an attribute that should be assigned on generated instances for this
18
+ # factory.
19
+ #
20
+ # This method should be called with either a value or block, but not both. If
21
+ # called with a block, the attribute will be generated "lazily," whenever an
22
+ # instance is generated. Lazy attribute blocks will not be called if that
23
+ # attribute is overridden for a specific instance.
24
+ #
25
+ # When defining lazy attributes, an instance of FactoryGirl::Strategy will
26
+ # be yielded, allowing associations to be built using the correct build
27
+ # strategy.
28
+ #
29
+ # Arguments:
30
+ # * name: +Symbol+ or +String+
31
+ # The name of this attribute. This will be assigned using "name=" for
32
+ # generated instances.
33
+ # * value: +Object+
34
+ # If no block is given, this value will be used for this attribute.
35
+ def add_attribute(name, value = nil, &block)
36
+ raise AttributeDefinitionError, "Both value and block given" if value && block_given?
37
+
38
+ declaration = if block_given?
39
+ Declaration::Dynamic.new(name, @ignore, block)
40
+ else
41
+ Declaration::Static.new(name, value, @ignore)
42
+ end
43
+
44
+ @definition.declare_attribute(declaration)
45
+ end
46
+
47
+ def ignore(&block)
48
+ proxy = DefinitionProxy.new(@definition, true)
49
+ proxy.instance_eval(&block)
50
+ end
51
+
52
+ # Calls add_attribute using the missing method name as the name of the
53
+ # attribute, so that:
54
+ #
55
+ # factory :user do
56
+ # name 'Billy Idol'
57
+ # end
58
+ #
59
+ # and:
60
+ #
61
+ # factory :user do
62
+ # add_attribute :name, 'Billy Idol'
63
+ # end
64
+ #
65
+ # are equivalent.
66
+ #
67
+ # If no argument or block is given, factory_girl will look for a sequence
68
+ # or association with the same name. This means that:
69
+ #
70
+ # factory :user do
71
+ # email { create(:email) }
72
+ # association :account
73
+ # end
74
+ #
75
+ # and:
76
+ #
77
+ # factory :user do
78
+ # email
79
+ # account
80
+ # end
81
+ #
82
+ # are equivalent.
83
+ def method_missing(name, *args, &block)
84
+ if args.empty? && block.nil?
85
+ @definition.declare_attribute(Declaration::Implicit.new(name, @definition, @ignore))
86
+ elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
87
+ association(name, *args)
88
+ elsif FactoryGirl.callback_names.include?(name)
89
+ @definition.add_callback(Callback.new(name, block))
90
+ else
91
+ add_attribute(name, *args, &block)
92
+ end
93
+ end
94
+
95
+ # Adds an attribute that will have unique values generated by a sequence with
96
+ # a specified format.
97
+ #
98
+ # The result of:
99
+ # factory :user do
100
+ # sequence(:email) { |n| "person#{n}@example.com" }
101
+ # end
102
+ #
103
+ # Is equal to:
104
+ # sequence(:email) { |n| "person#{n}@example.com" }
105
+ #
106
+ # factory :user do
107
+ # email { FactoryGirl.create(:email) }
108
+ # end
109
+ #
110
+ # Except that no globally available sequence will be defined.
111
+ def sequence(name, start_value = 1, &block)
112
+ sequence = Sequence.new(name, start_value, &block)
113
+ add_attribute(name) { sequence.next }
114
+ end
115
+
116
+ # Adds an attribute that builds an association. The associated instance will
117
+ # be built using the same build strategy as the parent instance.
118
+ #
119
+ # Example:
120
+ # factory :user do
121
+ # name 'Joey'
122
+ # end
123
+ #
124
+ # factory :post do
125
+ # association :author, factory: :user
126
+ # end
127
+ #
128
+ # Arguments:
129
+ # * name: +Symbol+
130
+ # The name of this attribute.
131
+ # * options: +Hash+
132
+ #
133
+ # Options:
134
+ # * factory: +Symbol+ or +String+
135
+ # The name of the factory to use when building the associated instance.
136
+ # If no name is given, the name of the attribute is assumed to be the
137
+ # name of the factory. For example, a "user" association will by
138
+ # default use the "user" factory.
139
+ def association(name, options = {})
140
+ @definition.declare_attribute(Declaration::Association.new(name, options))
141
+ end
142
+
143
+ def to_create(&block)
144
+ @definition.to_create(&block)
145
+ end
146
+
147
+ def factory(name, options = {}, &block)
148
+ @child_factories << [name, options, block]
149
+ end
150
+
151
+ def trait(name, &block)
152
+ @definition.define_trait(Trait.new(name, &block))
153
+ end
154
+
155
+ def initialize_with(&block)
156
+ @definition.define_constructor(&block)
157
+ end
158
+ end
159
+ end