Class: Makery::Factory

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

Overview

makes stuff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Factory

Returns a new instance of Factory.



20
21
22
23
24
# File 'lib/makery.rb', line 20

def initialize(*args)
  self.count = 1
  self.traits_repository = {}
  super
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



19
20
21
# File 'lib/makery.rb', line 19

def count
  @count
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



18
19
20
# File 'lib/makery.rb', line 18

def klass
  @klass
end

#traits_repositoryObject

Returns the value of attribute traits_repository.



19
20
21
# File 'lib/makery.rb', line 19

def traits_repository
  @traits_repository
end

Instance Method Details

#base(**attrs) ⇒ Object



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

def base(**attrs)
  @base ||= attrs
end

#call(*traits, **override) ⇒ Object



26
27
28
29
30
31
# File 'lib/makery.rb', line 26

def call(*traits, **override)
  attrs = base.merge(**trait_attrs(traits), **override)
  Builder.call(attrs, klass, :new, count)
ensure
  self.count = count + 1
end

#instantiation_method(method = :new) ⇒ Object



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

def instantiation_method(method = :new)
  @instantiation_method = method
end

#trait(name, **attrs) ⇒ Object



45
46
47
# File 'lib/makery.rb', line 45

def trait(name, **attrs)
  traits_repository[name] = attrs
end

#trait_attrs(traits) ⇒ Object



41
42
43
# File 'lib/makery.rb', line 41

def trait_attrs(traits)
  traits.map { |t| traits_repository[t] }.reduce({}, &:merge)
end