Class: BinData::DSLMixin::DSLParser

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

Overview

A DSLParser parses and accumulates field definitions of the form

type name, params

where:

* +type+ is the under_scored name of a registered type
* +name+ is the (possible optional) name of the field
* +params+ is a hash containing any parameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(the_class, parser_type) ⇒ DSLParser

Returns a new instance of DSLParser.



72
73
74
75
76
# File 'lib/bindata/dsl.rb', line 72

def initialize(the_class, parser_type)
  @the_class   = the_class
  @parser_type = parser_type
  @endian      = parent_attribute(:endian, nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



130
131
132
133
134
135
136
# File 'lib/bindata/dsl.rb', line 130

def method_missing(symbol, *args, &block) #:nodoc:
  type   = symbol
  name   = name_from_field_declaration(args)
  params = params_from_field_declaration(type, args, &block)

  append_field(type, name, params)
end

Instance Attribute Details

#parser_typeObject (readonly)

Returns the value of attribute parser_type.



78
79
80
# File 'lib/bindata/dsl.rb', line 78

def parser_type
  @parser_type
end

Instance Method Details

#dsl_paramsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bindata/dsl.rb', line 113

def dsl_params
  case @parser_type
  when :struct
    to_struct_params
  when :array
    to_array_params
  when :buffer
    to_array_params
  when :choice
    to_choice_params
  when :primitive
    to_struct_params
  else
    raise "unknown parser type #{@parser_type}"
  end
end

#endian(endian = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/bindata/dsl.rb', line 80

def endian(endian = nil)
  if endian.nil?
    @endian
  elsif endian == :big or endian == :little
    @endian = endian
  else
    dsl_raise ArgumentError, "unknown value for endian '#{endian}'"
  end
end

#fieldsObject



103
104
105
106
107
108
109
110
111
# File 'lib/bindata/dsl.rb', line 103

def fields
  unless defined? @fields
    fields = parent_attribute(:fields, nil)
    @fields = SanitizedFields.new(endian)
    @fields.copy_fields(fields) if fields
  end

  @fields
end

#hide(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bindata/dsl.rb', line 90

def hide(*args)
  if option?(:hidden_fields)
    hidden = args.collect { |name| name.to_sym }

    unless defined? @hide
      @hide = parent_attribute(:hide, []).dup
    end

    @hide.concat(hidden.compact)
    @hide
  end
end