Class: ActiveFactory::Factory

Inherits:
Struct
  • Object
show all
Defined in:
lib/active_factory.rb

Overview

creates instances of the given model class

Instance Attribute Summary collapse

Instance Method Summary collapse

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_buildObject

Returns the value of attribute after_build

Returns:

  • (Object)

    the current value of after_build



104
105
106
# File 'lib/active_factory.rb', line 104

def after_build
  @after_build
end

#attribute_expressionsObject

Returns the value of attribute attribute_expressions

Returns:

  • (Object)

    the current value of attribute_expressions



104
105
106
# File 'lib/active_factory.rb', line 104

def attribute_expressions
  @attribute_expressions
end

#before_saveObject

Returns the value of attribute before_save

Returns:

  • (Object)

    the current value of before_save



104
105
106
# File 'lib/active_factory.rb', line 104

def before_save
  @before_save
end

#model_classObject

Returns the value of attribute model_class

Returns:

  • (Object)

    the current value of model_class



104
105
106
# File 'lib/active_factory.rb', line 104

def model_class
  @model_class
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



104
105
106
# File 'lib/active_factory.rb', line 104

def name
  @name
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



104
105
106
# File 'lib/active_factory.rb', line 104

def parent
  @parent
end

#prefer_associationsObject

Returns the value of attribute prefer_associations

Returns:

  • (Object)

    the current value of 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