beefcake 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/protoc-gen-beefcake +74 -36
  2. metadata +4 -4
@@ -133,7 +133,7 @@ end
133
133
 
134
134
 
135
135
  module Beefcake
136
- class Generator < Struct.new(:c)
136
+ class Generator
137
137
 
138
138
  L = CodeGeneratorRequest::FieldDescriptorProto::Label
139
139
  T = CodeGeneratorRequest::FieldDescriptorProto::Type
@@ -154,45 +154,67 @@ module Beefcake
154
154
  CodeGeneratorResponse.new(:file => file)
155
155
  end
156
156
 
157
+ attr_reader :c
158
+
159
+ def initialize(c)
160
+ @c = c
161
+ @n = 0
162
+ end
163
+
157
164
  def file!(file)
158
- c.puts "## Generated from #{file.name} for #{file.package}"
165
+ puts "## Generated from #{file.name} for #{file.package}"
159
166
 
160
167
  file.message_type.each do |mt|
161
168
  message!(mt)
162
169
  end
163
170
  end
164
171
 
165
- def message!(pkg, mt)
166
- c.puts
167
- c.puts "class #{mt.name}"
168
- c.puts " include Beefcake::Message"
169
- c.puts
170
-
171
- mt.enum_type.each do |et|
172
- enum!(et)
173
- end
172
+ def indent(&blk)
173
+ @n += 1
174
+ blk.call
175
+ @n -= 1
176
+ end
174
177
 
175
- ## Generate Types
176
- (mt.nested_type || []).each do |nt|
177
- message!(nt)
178
- end
179
- c.puts
178
+ def indent!(n)
179
+ @n = n
180
+ end
180
181
 
181
- ## Generate fields
182
- (mt.field || []).each do |f|
183
- field!(pkg, f)
182
+ def message!(pkg, mt)
183
+ puts
184
+ puts "class #{mt.name}"
185
+
186
+ indent do
187
+ puts "include Beefcake::Message"
188
+ puts
189
+
190
+ mt.enum_type.each do |et|
191
+ enum!(et)
192
+ end
193
+
194
+ ## Generate Types
195
+ (mt.nested_type || []).each do |nt|
196
+ message!(nt)
197
+ end
198
+ puts
199
+
200
+ ## Generate fields
201
+ (mt.field || []).each do |f|
202
+ field!(pkg, f)
203
+ end
204
+ puts
184
205
  end
185
- c.puts
186
206
 
187
- c.puts "end"
207
+ puts "end"
188
208
  end
189
209
 
190
210
  def enum!(et)
191
- c.puts " module #{et.name}"
192
- et.value.each do |v|
193
- c.puts " %s = %d" % [v.name, v.number]
211
+ puts "module #{et.name}"
212
+ indent do
213
+ et.value.each do |v|
214
+ puts "%s = %d" % [v.name, v.number]
215
+ end
194
216
  end
195
- c.puts " end"
217
+ puts "end"
196
218
  end
197
219
 
198
220
  def field!(pkg, f)
@@ -216,13 +238,13 @@ module Beefcake
216
238
  end
217
239
 
218
240
  # Finally, generate the declaration
219
- c.print " %s %s, %s, %d" % [label, name, type, f.number]
241
+ out = "%s %s, %s, %d" % [label, name, type, f.number]
220
242
 
221
243
  if f.default_value
222
- c.print ", :default => #{f.default_value}"
244
+ out += ", :default => #{f.default_value}"
223
245
  end
224
246
 
225
- c.puts
247
+ puts out
226
248
  end
227
249
 
228
250
  # Determines the name for a
@@ -231,18 +253,34 @@ module Beefcake
231
253
  end
232
254
 
233
255
  def compile(ns, file)
234
- c.puts "## Generated from #{file.name} for #{file.package}"
235
-
236
- ns.each do |name|
237
- c.puts "module #{name}"
256
+ puts "## Generated from #{file.name} for #{file.package}"
257
+ puts "require \"beefcake\""
258
+ puts
259
+
260
+ ns!(*ns) do
261
+ file.message_type.each do |mt|
262
+ message!(file.package, mt)
263
+ end
238
264
  end
265
+ end
239
266
 
240
- file.message_type.each do |mt|
241
- message!(file.package, mt)
267
+ def ns!(n, *tail, &blk)
268
+ puts "module #{n}"
269
+ indent do
270
+ if tail.empty?
271
+ blk.call
272
+ else
273
+ ns!(*tail, &blk)
274
+ end
242
275
  end
276
+ puts "end"
277
+ end
243
278
 
244
- ns.each do |name|
245
- c.puts "end"
279
+ def puts(msg=nil)
280
+ if msg
281
+ c.puts((" " * @n) + msg)
282
+ else
283
+ c.puts
246
284
  end
247
285
  end
248
286
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beefcake
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Blake Mizerany
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-22 00:00:00 -07:00
18
+ date: 2011-03-23 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency