Class: Divine::StructBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/divine/dsl.rb

Overview

Responsible for building structs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ps) ⇒ StructBuilder

Returns a new instance of StructBuilder.



206
207
208
209
210
211
212
213
214
# File 'lib/divine/dsl.rb', line 206

def initialize(name, ps)
  @properties = ps
  @name = name.to_sym
  @fields = []
  unless @name == :_inline_
    $all_structs[@name] ||= []
    $all_structs[@name] << self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



335
336
337
# File 'lib/divine/dsl.rb', line 335

def method_missing(m, *args, &block)
  _process(m, *args, &block)
end

Instance Attribute Details

#fieldsObject (readonly)

Name = name of the struct Version = struct version (not currently used) Fields = All defined fields



204
205
206
# File 'lib/divine/dsl.rb', line 204

def fields
  @fields
end

#nameObject (readonly)

Name = name of the struct Version = struct version (not currently used) Fields = All defined fields



204
205
206
# File 'lib/divine/dsl.rb', line 204

def name
  @name
end

#propertiesObject (readonly)

Name = name of the struct Version = struct version (not currently used) Fields = All defined fields



204
205
206
# File 'lib/divine/dsl.rb', line 204

def properties
  @properties
end

Instance Method Details

#_process(m, *args, &block) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/divine/dsl.rb', line 313

def _process(m, *args, &block)
  #puts "... #{m} #{args.inspect}"
  type = $available_types[m]
  if type
    if block_given?
      #puts ".... recursive definition"
      builder = StructBuilder.new(:_inline_, args)
      Docile.dsl_eval(builder, &block)
      args << builder
    end
    if @name == :_inline_
      # Pad the _inline_ name to anonymous inner types
      @fields << type.new(self, m, [@name] + args)
    else
      @fields << type.new(self, m, args)
    end
    #puts "... adding #{m} to #{name}, got #{fields} ..."
  else
    super.send(m, args, block)
  end
end

#binary(*args, &block) ⇒ Object



299
300
301
# File 'lib/divine/dsl.rb', line 299

def binary(*args, &block)
  _process(:binary, *args, &block)
end

#bool(*args, &block) ⇒ Object



305
306
307
# File 'lib/divine/dsl.rb', line 305

def bool(*args, &block)
  _process(:bool, *args, &block)
end

#complex_fieldsObject

Get all complex fields, i.e. lists and hashmaps



257
258
259
# File 'lib/divine/dsl.rb', line 257

def complex_fields
  fields.reject(&:simple?)
end

#dint63(*args, &block) ⇒ Object



278
279
280
# File 'lib/divine/dsl.rb', line 278

def dint63(*args, &block)
  _process(:dint63, *args, &block)
end

#freeze_signatureObject

Get the freeze signature



239
240
241
242
243
244
245
# File 'lib/divine/dsl.rb', line 239

def freeze_signature
  if properties && properties[:freeze]
    properties[:freeze]
  else
    nil
  end
end

#freezed?Boolean

Is the struct freezed? I.e. no changes are allowed

Returns:

  • (Boolean)


232
233
234
# File 'lib/divine/dsl.rb', line 232

def freezed?
  nil != freeze_signature
end

#get_field(name) ⇒ Object

Get named field



264
265
266
267
268
269
# File 'lib/divine/dsl.rb', line 264

def get_field(name)
  fields.each do |f|
    return f if f.name == name
  end
  return nil
end

#int16(*args, &block) ⇒ Object



293
294
295
# File 'lib/divine/dsl.rb', line 293

def int16(*args, &block)
  _process(:int16, *args, &block)
end

#int24(*args, &block) ⇒ Object



290
291
292
# File 'lib/divine/dsl.rb', line 290

def int24(*args, &block)
  _process(:int24, *args, &block)
end

#int32(*args, &block) ⇒ Object



287
288
289
# File 'lib/divine/dsl.rb', line 287

def int32(*args, &block)
  _process(:int32, *args, &block)
end

#int8(*args, &block) ⇒ Object



296
297
298
# File 'lib/divine/dsl.rb', line 296

def int8(*args, &block)
  _process(:int8, *args, &block)
end

#ip_number(*args, &block) ⇒ Object



308
309
310
# File 'lib/divine/dsl.rb', line 308

def ip_number(*args, &block)
  _process(:ip_number, *args, &block)
end

#list(*args, &block) ⇒ Object



271
272
273
# File 'lib/divine/dsl.rb', line 271

def list(*args, &block)
  _process(:list, *args, &block)
end

#map(*args, &block) ⇒ Object



275
276
277
# File 'lib/divine/dsl.rb', line 275

def map(*args, &block)
  _process(:map, *args, &block)
end

#simple_fieldsObject

Get all simple fields, i.e. basic types like string, etc



250
251
252
# File 'lib/divine/dsl.rb', line 250

def simple_fields
  fields.select(&:simple?)
end

#sint32(*args, &block) ⇒ Object



284
285
286
# File 'lib/divine/dsl.rb', line 284

def sint32(*args, &block)
  _process(:sint32, *args, &block)
end

#sint64(*args, &block) ⇒ Object



281
282
283
# File 'lib/divine/dsl.rb', line 281

def sint64(*args, &block)
  _process(:sint64, *args, &block)
end

#string(*args, &block) ⇒ Object



302
303
304
# File 'lib/divine/dsl.rb', line 302

def string(*args, &block)
  _process(:string, *args, &block)
end

#versionObject

Get the version of the struct



219
220
221
222
223
224
225
226
227
# File 'lib/divine/dsl.rb', line 219

def version
  if properties && properties[:version]
    properties[:version].to_i
  else
    1
  end
rescue => e
  raise "Failed to get version number from '#{name}': #{properties.inspect}\n#{e}"
end