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.



36
37
38
39
40
# File 'lib/bindata/dsl.rb', line 36

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:



100
101
102
103
104
105
106
# File 'lib/bindata/dsl.rb', line 100

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.



42
43
44
# File 'lib/bindata/dsl.rb', line 42

def parser_type
  @parser_type
end

Instance Method Details

#dsl_paramsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bindata/dsl.rb', line 83

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

#endian(endian = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/bindata/dsl.rb', line 44

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/bindata/dsl.rb', line 72

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

  @fields
end

#hide(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bindata/dsl.rb', line 54

def hide(*args)
  if option?(:hidden_fields)
    hidden = args.collect do |name|
               unless Symbol === name
                 warn "Hidden field '#{name}' should be provided as a symbol.  Using strings is deprecated"
               end
               name.to_sym
             end

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

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