Module: Nina
- Defined in:
- lib/nina.rb,
lib/nina/builder.rb,
lib/nina/version.rb,
lib/nina/builder/callbacks.rb,
lib/nina/builder/initialization.rb
Overview
This should be a kind of factory that creates complex objects from simple ones. It should use torirori to create objects. It also enriches objects with some methods that make them more like linked lists.
Defined Under Namespace
Modules: ClassMethods, MethodMissingDelegation
Classes: Builder, Error
Constant Summary
collapse
- VERSION =
'0.2.1'
Class Method Summary
collapse
Class Method Details
.def_reader(accessor, on:, to:, delegate: false) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/nina.rb', line 52
def self.def_reader(accessor, on:, to:, delegate: false)
on.define_singleton_method(accessor) { to }
on.define_singleton_method(:predecessor) { to }
def on.predecessors
Enumerator.new do |y|
obj = self
y << obj = obj.predecessor while obj.methods.detect { |m| m == :predecessor }
end
end
return unless delegate
on.extend(MethodMissingDelegation)
end
|
.included(receiver) ⇒ Object
48
49
50
|
# File 'lib/nina.rb', line 48
def self.included(receiver)
receiver.extend ClassMethods
end
|
.link(build, delegate: false, &block) ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/nina.rb', line 66
def self.link(build, delegate: false, &block)
result = nil
build.each.inject(nil) do |prev, (name, object)|
Nina.def_reader(name, on: prev, to: object, delegate: delegate) if prev
yield(name, object) if block
object.tap { |o| result ||= o }
end
result
end
|
.reverse_link(build, delegate: false, &block) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/nina.rb', line 76
def self.reverse_link(build, delegate: false, &block)
build.each.with_index(-1).inject(nil) do |prev, ((name, object), idx)|
Nina.def_reader(build.keys[idx], on: object, to: prev, delegate: delegate) if prev
yield(name, object) if block
object
end
end
|