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, *options) ⇒ DSLParser

Returns a new instance of DSLParser.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bindata/dsl.rb', line 71

def initialize(the_class, *options)
  @the_class = the_class

  @options = parent_options_plus_these(options)

  @endian = parent_attribute(:endian, nil)

  if option?(:hidden_fields)
    @hide = parent_attribute(:hide, []).dup
  end

  if option?(:sanitize_fields)
    fields = parent_attribute(:fields, nil)
    @fields = Sanitizer.new.create_sanitized_fields(fields)
  else
    fields = parent_attribute(:fields, UnSanitizedFields.new)
    @fields = fields.dup
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



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

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

#optionsObject (readonly)

Returns the value of attribute options.



91
92
93
# File 'lib/bindata/dsl.rb', line 91

def options
  @options
end

Instance Method Details

#endian(endian = nil) ⇒ Object



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

def endian(endian = nil)
  if endian.nil?
    @endian
  elsif endian.respond_to? :endian
    @endian = endian
  elsif [:little, :big].include?(endian)
    @endian = Sanitizer.new.create_sanitized_endian(endian)
  else
    dsl_raise ArgumentError, "unknown value for endian '#{endian}'"
  end
end

#fieldObject



116
117
118
# File 'lib/bindata/dsl.rb', line 116

def field
  @fields[0]
end

#fieldsObject



112
113
114
# File 'lib/bindata/dsl.rb', line 112

def fields
  @fields
end

#hide(*args) ⇒ Object



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

def hide(*args)
  if option?(:hidden_fields)
    @hide.concat(args.collect { |name| name.to_s })
    @hide
  end
end

#to_struct_paramsObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bindata/dsl.rb', line 120

def to_struct_params
  result = {:fields => fields}
  if not endian.nil?
    result[:endian] = endian
  end
  if option?(:hidden_fields) and not hide.empty?
    result[:hide] = hide
  end

  result
end