Class: Rstruct::StructBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rstruct/struct_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = nil, &block) ⇒ StructBuilder

Returns a new instance of StructBuilder.



10
11
12
13
14
# File 'lib/rstruct/struct_builder.rb', line 10

def initialize(registry=nil, &block)
  @__fields = []
  @__registry = registry || Registry::DEFAULT_REGISTRY
  instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
27
28
29
30
31
# File 'lib/rstruct/struct_builder.rb', line 24

def method_missing(typ_arg, *args, &block)
  name = args.shift
  unless typ = @__registry.get(typ_arg)
    raise(InvalidTypeError, "invalid field type: #{typ_arg}")
  end

  field(name, typ, typ_arg, *args, &block)
end

Instance Attribute Details

#__fieldsObject (readonly)

Returns the value of attribute __fields.



8
9
10
# File 'lib/rstruct/struct_builder.rb', line 8

def __fields
  @__fields
end

#__registryObject (readonly)

Returns the value of attribute __registry.



8
9
10
# File 'lib/rstruct/struct_builder.rb', line 8

def __registry
  @__registry
end

Instance Method Details

#array(name, typ, count, opts = {}, &block) ⇒ Object



20
21
22
# File 'lib/rstruct/struct_builder.rb', line 20

def array(name, typ, count, opts={}, &block)
  @__fields << ArrayType.new(name, typ, count, opts, &block)
end

#field(name, typ, typ_name, *args, &block) ⇒ Object



16
17
18
# File 'lib/rstruct/struct_builder.rb', line 16

def field(name, typ, typ_name, *args, &block)
  @__fields << Field.new(name,typ,args,block)
end