Class: ActiveFactory::Factory
- Inherits:
-
Struct
- Object
- Struct
- ActiveFactory::Factory
- Defined in:
- lib/active_factory.rb
Overview
creates instances of the given model class
Instance Attribute Summary collapse
-
#after_build ⇒ Object
Returns the value of attribute after_build.
-
#attribute_expressions ⇒ Object
Returns the value of attribute attribute_expressions.
-
#before_save ⇒ Object
Returns the value of attribute before_save.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#prefer_associations ⇒ Object
Returns the value of attribute prefer_associations.
Instance Method Summary collapse
- #apply_after_build(index, context, model) ⇒ Object
- #apply_before_save(index, context, model) ⇒ Object
- #attributes_for(index) ⇒ Object
-
#initialize(name, parent, *overridable) ⇒ Factory
constructor
A new instance of Factory.
- #merge_overridable(overridable) ⇒ Object
Constructor Details
#initialize(name, parent, *overridable) ⇒ Factory
Returns a new instance of Factory.
106 107 108 109 110 111 112 113 114 |
# File 'lib/active_factory.rb', line 106 def initialize name, parent, *overridable @overridable = parent ? parent.merge_overridable(overridable) : overridable super(name, parent, *@overridable) self.attribute_expressions = parent.attribute_expressions.merge(self.attribute_expressions) if parent name.is_a? Symbol or raise "factory name #{name.inspect} must be symbol" self.model_class ||= (@overridable[0] = Kernel.const_get(name.to_s.capitalize)) end |
Instance Attribute Details
#after_build ⇒ Object
Returns the value of attribute after_build
104 105 106 |
# File 'lib/active_factory.rb', line 104 def after_build @after_build end |
#attribute_expressions ⇒ Object
Returns the value of attribute attribute_expressions
104 105 106 |
# File 'lib/active_factory.rb', line 104 def attribute_expressions @attribute_expressions end |
#before_save ⇒ Object
Returns the value of attribute before_save
104 105 106 |
# File 'lib/active_factory.rb', line 104 def before_save @before_save end |
#model_class ⇒ Object
Returns the value of attribute model_class
104 105 106 |
# File 'lib/active_factory.rb', line 104 def model_class @model_class end |
#name ⇒ Object
Returns the value of attribute name
104 105 106 |
# File 'lib/active_factory.rb', line 104 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent
104 105 106 |
# File 'lib/active_factory.rb', line 104 def parent @parent end |
#prefer_associations ⇒ Object
Returns the value of attribute prefer_associations
104 105 106 |
# File 'lib/active_factory.rb', line 104 def prefer_associations @prefer_associations end |
Instance Method Details
#apply_after_build(index, context, model) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/active_factory.rb', line 127 def apply_after_build index, context, model if after_build CreationContext.new(index, context, model). instance_eval(&after_build) end end |
#apply_before_save(index, context, model) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/active_factory.rb', line 134 def apply_before_save index, context, model if before_save CreationContext.new(index, context, model). instance_eval(&before_save) end end |
#attributes_for(index) ⇒ Object
121 122 123 124 125 |
# File 'lib/active_factory.rb', line 121 def attributes_for index context = CreationContext.new(index) attrs = attribute_expressions.map { |a, e| [a, context.instance_eval(&e)] } Hash[attrs] end |
#merge_overridable(overridable) ⇒ Object
116 117 118 119 |
# File 'lib/active_factory.rb', line 116 def merge_overridable overridable overridable.zip(@overridable). map { |his, my| his or my } end |