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.



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

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



19
20
21
22
23
24
25
26
# File 'lib/rstruct/struct_builder.rb', line 19

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.



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

def __fields
  @__fields
end

#__registryObject (readonly)

Returns the value of attribute __registry.



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

def __registry
  @__registry
end

Instance Method Details

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



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

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