Module: FactoryGuy::Constructable

Defined in:
lib/factory_guy/constructable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object

Need to define #default_attributes def default_attributes ; end



12
13
14
15
16
17
18
19
# File 'lib/factory_guy/constructable.rb', line 12

def self.extended(mod)
  mod.class_eval do
    cattr_accessor :resource

    resource_name = mod.to_s.split(/Factory/)[0]
    self.resource = const_get(resource_name)
  end
end

Instance Method Details

#attributesObject



21
22
23
24
25
26
27
# File 'lib/factory_guy/constructable.rb', line 21

def attributes
  begin
    default_attributes
  rescue NameError
    raise(ImplementationError, "Your factory must implement class-level `#{self}.default_attributes`.")
  end
end

#buildObject



29
30
31
# File 'lib/factory_guy/constructable.rb', line 29

def build
  resource.new(attributes)
end

#createObject



33
34
35
# File 'lib/factory_guy/constructable.rb', line 33

def create
  build.tap(&:save)
end

#create!Object



37
38
39
# File 'lib/factory_guy/constructable.rb', line 37

def create!
  build.tap(&:save!)
end