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.



213
214
215
216
217
218
219
220
221
# File 'lib/divine/dsl.rb', line 213

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/divine/dsl.rb', line 278

def method_missing(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

Instance Attribute Details

#fieldsObject (readonly)

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



211
212
213
# File 'lib/divine/dsl.rb', line 211

def fields
  @fields
end

#nameObject (readonly)

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



211
212
213
# File 'lib/divine/dsl.rb', line 211

def name
  @name
end

#propertiesObject (readonly)

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



211
212
213
# File 'lib/divine/dsl.rb', line 211

def properties
  @properties
end

Instance Method Details

#complex_fieldsObject

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



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

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

#freeze_signatureObject

Get the freeze signature



246
247
248
249
250
251
252
# File 'lib/divine/dsl.rb', line 246

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)


239
240
241
# File 'lib/divine/dsl.rb', line 239

def freezed?
  nil != freeze_signature
end

#get_field(name) ⇒ Object

Get named field



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

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

#simple_fieldsObject

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



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

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

#versionObject

Get the version of the struct



226
227
228
229
230
231
232
233
234
# File 'lib/divine/dsl.rb', line 226

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