Class: Beefcake::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/beefcake/generator.rb

Constant Summary collapse

L =
CodeGeneratorRequest::FieldDescriptorProto::Label
T =
CodeGeneratorRequest::FieldDescriptorProto::Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c) ⇒ Generator

Returns a new instance of Generator.


160
161
162
163
# File 'lib/beefcake/generator.rb', line 160

def initialize(c)
  @c = c
  @n = 0
end

Instance Attribute Details

#cObject (readonly)

Returns the value of attribute c


158
159
160
# File 'lib/beefcake/generator.rb', line 158

def c
  @c
end

Class Method Details

.compile(ns, req) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/beefcake/generator.rb', line 143

def self.compile(ns, req)
  file = req.proto_file.map do |file|
    g = new(StringIO.new)
    g.compile(ns, file)

    g.c.rewind
    CodeGeneratorResponse::File.new(
      :name => File.basename(file.name, ".proto") + ".pb.rb",
      :content => g.c.read
    )
  end

  CodeGeneratorResponse.new(:file => file)
end

Instance Method Details

#compile(ns, file) ⇒ Object


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/beefcake/generator.rb', line 271

def compile(ns, file)
  package_part = file.package ? "for #{file.package}" : ''
  puts "## Generated from #{file.name} #{package_part}".strip
  puts "require \"beefcake\""
  puts

  ns!(ns) do
    Array(file.enum_type).each do |et|
      enum!(et)
    end

    file.message_type.each do |mt|
      define! mt
    end

    file.message_type.each do |mt|
      message!(file.package, mt)
    end
  end
end

#define!(mt) ⇒ Object


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/beefcake/generator.rb', line 175

def define!(mt)
  puts
  puts "class #{mt.name}"

  indent do
    puts "include Beefcake::Message"

    ## Enum Types
    Array(mt.enum_type).each do |et|
      enum!(et)
    end

    ## Nested Types
    Array(mt.nested_type).each do |nt|
      define!(nt)
    end
  end
  puts "end"
end

#enum!(et) ⇒ Object


214
215
216
217
218
219
220
221
222
223
# File 'lib/beefcake/generator.rb', line 214

def enum!(et)
  puts
  puts "module #{et.name}"
  indent do
    et.value.each do |v|
      puts "%s = %d" % [v.name, v.number]
    end
  end
  puts "end"
end

#field!(pkg, f) ⇒ Object


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/beefcake/generator.rb', line 225

def field!(pkg, f)
  # Turn the label into Ruby
  label = name_for(f, L, f.label)

  # Turn the name into a Ruby
  name = ":#{f.name}"

  # Determine the type-name and convert to Ruby
  type = if f.type_name
    # We have a type_name so we will use it after converting to a
    # Ruby friendly version
    t = f.type_name
    if pkg
      t = t.gsub(pkg, "") # Remove the leading package name
    end
    t = t.gsub(/^\.*/, "")       # Remove leading `.`s

    t.gsub(".", "::")  # Convert to Ruby namespacing syntax
  else
    ":#{name_for(f, T, f.type)}"
  end

  # Finally, generate the declaration
  out = "%s %s, %s, %d" % [label, name, type, f.number]

  if f.default_value
    v = case f.type
    when T::TYPE_ENUM
      "%s::%s" % [type, f.default_value]
    when T::TYPE_STRING, T::TYPE_BYTES
      '"%s"' % [f.default_value.gsub('"', '\"')]
    else
      f.default_value
    end

    out += ", :default => #{v}"
  end

  puts out
end

#indent(&blk) ⇒ Object


165
166
167
168
169
# File 'lib/beefcake/generator.rb', line 165

def indent(&blk)
  @n += 1
  blk.call
  @n -= 1
end

#indent!(n) ⇒ Object


171
172
173
# File 'lib/beefcake/generator.rb', line 171

def indent!(n)
  @n = n
end

#message!(pkg, mt) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/beefcake/generator.rb', line 195

def message!(pkg, mt)
  puts
  puts "class #{mt.name}"

  indent do
    ## Generate Types
    Array(mt.nested_type).each do |nt|
      message!(pkg, nt)
    end

    ## Generate Fields
    Array(mt.field).each do |f|
      field!(pkg, f)
    end
  end

  puts "end"
end

#name_for(b, mod, val) ⇒ Object

Determines the name for a


267
268
269
# File 'lib/beefcake/generator.rb', line 267

def name_for(b, mod, val)
  b.name_for(mod, val).to_s.gsub(/.*_/, "").downcase
end

#ns!(modules, &blk) ⇒ Object


292
293
294
295
296
297
298
299
300
301
302
# File 'lib/beefcake/generator.rb', line 292

def ns!(modules, &blk)
  if modules.empty?
    blk.call
  else
    puts "module #{modules.first}"
    indent do
      ns!(modules[1..-1], &blk)
    end
    puts "end"
  end
end

#puts(msg = nil) ⇒ Object


304
305
306
307
308
309
310
# File 'lib/beefcake/generator.rb', line 304

def puts(msg=nil)
  if msg
    c.puts(("  " * @n) + msg)
  else
    c.puts
  end
end