/
generators.rb
163 lines (138 loc) · 3.31 KB
/
generators.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
module Temple
# Exception raised if invalid temple expression is found
#
# @api public
class InvalidExpression < RuntimeError
end
# Abstract generator base class
# Generators should inherit this class and
# compile the Core Abstraction to ruby code.
#
# @api public
class Generator
include Mixins::CompiledDispatcher
include Mixins::Options
define_options :capture_generator,
:buffer => '_buf'
def call(exp)
[preamble, compile(exp), postamble].join('; ')
end
def on(*exp)
raise InvalidExpression, "Generator supports only core expressions - found #{exp.inspect}"
end
def on_multi(*exp)
exp.map {|e| compile(e) }.join('; ')
end
def on_newline
"\n"
end
def on_capture(name, exp)
options[:capture_generator].new(:buffer => name).call(exp)
end
def on_static(text)
concat(text.inspect)
end
def on_dynamic(code)
concat(code)
end
def on_code(code)
code
end
protected
def buffer
options[:buffer]
end
def concat(str)
"#{buffer} << (#{str})"
end
end
module Generators
# Implements an array buffer.
#
# _buf = []
# _buf << "static"
# _buf << dynamic
# _buf
#
# @api public
class Array < Generator
def preamble
"#{buffer} = []"
end
def postamble
buffer
end
end
# Just like Array, but calls #join on the array.
#
# _buf = []
# _buf << "static"
# _buf << dynamic
# _buf.join
#
# @api public
class ArrayBuffer < Array
def call(exp)
case exp.first
when :static
"#{buffer} = #{exp.last.inspect}"
when :dynamic
"#{buffer} = (#{exp.last}).to_s"
else
super
end
end
def postamble
"#{buffer} = #{buffer}.join"
end
end
# Implements a string buffer.
#
# _buf = ''
# _buf << "static"
# _buf << dynamic.to_s
# _buf
#
# @api public
class StringBuffer < ArrayBuffer
def preamble
"#{buffer} = ''"
end
def postamble
buffer
end
def on_dynamic(code)
concat("(#{code}).to_s")
end
end
# Implements a rails output buffer.
#
# @output_buffer = ActiveSupport::SafeBuffer
# @output_buffer.safe_concat "static"
# @output_buffer.safe_concat dynamic.to_s
# @output_buffer
#
# @api public
class RailsOutputBuffer < StringBuffer
define_options :streaming,
:buffer_class => 'ActiveSupport::SafeBuffer',
:buffer => '@output_buffer',
# output_buffer is needed for Rails 3.1 Streaming support
:capture_generator => RailsOutputBuffer
def call(exp)
[preamble, compile(exp), postamble].join('; ')
end
def preamble
if options[:streaming] && options[:buffer] == '@output_buffer'
"#{buffer} = output_buffer || #{options[:buffer_class]}.new"
else
"#{buffer} = #{options[:buffer_class]}.new"
end
end
def concat(str)
"#{buffer}.safe_concat((#{str}))"
end
end
end
Generator.default_options[:capture_generator] = Generators::StringBuffer
end