Class: Nina::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/nina/builder.rb,
lib/nina/builder/callbacks.rb,
lib/nina/builder/initialization.rb

Overview

Generates module that adds support for objects creation

Defined Under Namespace

Modules: ClassMethods Classes: Callbacks, Initialization

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, abstract_factory: nil, callbacks: nil, &def_block) ⇒ Builder

Returns a new instance of Builder.



25
26
27
28
29
30
31
32
33
# File 'lib/nina/builder.rb', line 25

def initialize(name, abstract_factory: nil, callbacks: nil, &def_block)
  @name = name
  @def_block = def_block
  @abstract_factory = abstract_factory.include(Toritori).extend(ClassMethods)
  @abstract_factory.class_eval(&def_block) if def_block
  @initialization = Builder::Initialization.new(self)
  @callbacks = callbacks&.copy || Callbacks.new(@abstract_factory.factories.keys)
  @observers = []
end

Instance Attribute Details

#abstract_factoryObject (readonly)

Returns the value of attribute abstract_factory.



13
14
15
# File 'lib/nina/builder.rb', line 13

def abstract_factory
  @abstract_factory
end

#callbacksObject (readonly)

Returns the value of attribute callbacks.



13
14
15
# File 'lib/nina/builder.rb', line 13

def callbacks
  @callbacks
end

#def_blockObject (readonly)

Returns the value of attribute def_block.



13
14
15
# File 'lib/nina/builder.rb', line 13

def def_block
  @def_block
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/nina/builder.rb', line 13

def name
  @name
end

Instance Method Details

#add_observer(observer) ⇒ Object



35
36
37
# File 'lib/nina/builder.rb', line 35

def add_observer(observer)
  @observers << observer
end

#copyObject



39
40
41
42
43
# File 'lib/nina/builder.rb', line 39

def copy
  new_builder = self.class.new(name, abstract_factory: abstract_factory, callbacks: @callbacks)
  @observers.each { |observer| new_builder.add_observer(observer) }
  new_builder
end

#nest(delegate: false) {|initialization| ... } ⇒ Object

Yields:

  • (initialization)


51
52
53
54
55
56
# File 'lib/nina/builder.rb', line 51

def nest(delegate: false, &block)
  initialization = Builder::Initialization.new(self)
  yield initialization if block

  Nina.link(initialization.to_h, delegate: delegate)
end

#subclass(&def_block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/nina/builder.rb', line 65

def subclass(&def_block)
  return unless def_block

  @abstract_factory = Class.new(abstract_factory)
  @abstract_factory.class_eval(&def_block)
  @callbacks = callbacks&.copy || Callbacks.new(@abstract_factory.build_order_list)
end

#with_callbacks {|@callbacks| ... } ⇒ Object

Yields:



45
46
47
48
49
# File 'lib/nina/builder.rb', line 45

def with_callbacks(&block)
  yield @callbacks if block

  copy
end

#wrap(delegate: false) {|initialization| ... } ⇒ Object

Yields:

  • (initialization)


58
59
60
61
62
63
# File 'lib/nina/builder.rb', line 58

def wrap(delegate: false, &block)
  initialization = Builder::Initialization.new(self)
  yield initialization if block

  Nina.reverse_link(initialization.to_h, delegate: delegate)
end