Class: Boxer
- Inherits:
-
Object
show all
- Defined in:
- lib/boxer.rb,
lib/boxer/version.rb
Defined Under Namespace
Classes: ViewMissingError
Constant Summary
collapse
- VERSION =
"1.0.2"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, options = {}, &block) ⇒ Boxer
Returns a new instance of Boxer.
16
17
18
19
20
21
22
23
|
# File 'lib/boxer.rb', line 16
def initialize(name, options={}, &block)
@name = name
@block = block
@fallback = []
@views = {}
@views_chain = {}
@options = options
end
|
Class Method Details
.box(name, options = {}, &block) ⇒ Object
27
28
29
|
# File 'lib/boxer.rb', line 27
def self.box(name, options={}, &block)
(@boxes ||= {})[name] = self.new(name, options, &block)
end
|
.boxes ⇒ Object
31
32
33
|
# File 'lib/boxer.rb', line 31
def self.boxes
@boxes
end
|
.clear! ⇒ Object
35
36
37
|
# File 'lib/boxer.rb', line 35
def self.clear!
@boxes = {}
end
|
39
40
41
|
# File 'lib/boxer.rb', line 39
def self.configure
yield config
end
|
.ship(name, *args) ⇒ Object
43
44
45
46
|
# File 'lib/boxer.rb', line 43
def self.ship(name, *args)
fail "Unknown box: #{name.inspect}" unless @boxes.has_key?(name)
@boxes[name].ship(*args)
end
|
Instance Method Details
#emit(val) ⇒ Object
50
51
52
|
# File 'lib/boxer.rb', line 50
def emit(val)
@fallback = [val]
end
|
#precondition {|_self| ... } ⇒ Object
85
86
87
|
# File 'lib/boxer.rb', line 85
def precondition
yield self
end
|
#ship(*args) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/boxer.rb', line 54
def ship(*args)
args = args.dup
if args.last.is_a?(Hash)
args[-1] = args.last.dup
view = args.last.delete(:view)
args.slice!(-1) if args.last.empty?
end
view ||= :base
modules = self.class.config.box_includes
black_box = Class.new do
modules.each do |mod|
include mod
end
end
block_result = black_box.new.instance_exec(self, *args, &@block)
if @fallback.length > 0
return @fallback.pop
elsif @views_chain.any?
unless @views_chain.has_key?(view)
fail ViewMissingError.new([@name, view].map(&:inspect).join('/'))
end
return @views_chain[view].inject({}) do |res, view_name|
res.deep_merge(@views[view_name].call(*args))
end
else
return block_result
end
end
|
#view(name, opts = {}, &block) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/boxer.rb', line 89
def view(name, opts={}, &block)
@views_chain[name] = []
if opts.has_key?(:extends)
ancestors = Array(opts[:extends]).map do |parent|
(@views_chain[parent] || []) + [parent]
end.flatten.uniq
@views_chain[name] += ancestors
end
@views_chain[name] << name
@views[name] = block
end
|