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.



64
65
66
67
68
69
70
71
# File 'lib/bindata/dsl.rb', line 64

def initialize(the_class, parser_type)
  raise "unknown parser type #{parser_type}" unless parser_abilities[parser_type]

  @the_class      = the_class
  @parser_type    = parser_type
  @validator      = DSLFieldValidator.new(the_class, self)
  @endian         = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



128
129
130
131
# File 'lib/bindata/dsl.rb', line 128

def method_missing(*args, &block)
  ensure_hints
  parse_and_append_field(*args, &block)
end

Instance Attribute Details

#parser_typeObject (readonly)

Returns the value of attribute parser_type.



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

def parser_type
  @parser_type
end

Instance Method Details

#dsl_paramsObject



124
125
126
# File 'lib/bindata/dsl.rb', line 124

def dsl_params
  send(parser_abilities[@parser_type].at(0))
end

#endian(endian = nil) ⇒ Object



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

def endian(endian = nil)
  if endian
    set_endian(endian)
  elsif @endian.nil?
    set_endian(parent_attribute(:endian))
  end
  @endian
end

#fieldsObject



114
115
116
117
118
119
120
121
122
# File 'lib/bindata/dsl.rb', line 114

def fields
  unless defined? @fields
    fields = parent_fields
    @fields = SanitizedFields.new(hints)
    @fields.copy_fields(fields) if fields
  end

  @fields
end

#hide(*args) ⇒ Object



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

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

#search_prefix(*args) ⇒ Object



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

def search_prefix(*args)
  unless defined? @search_prefix
    @search_prefix = parent_attribute(:search_prefix, []).dup
  end

  prefix = args.collect { |name| name.to_sym }.compact
  if prefix.size > 0
    if has_fields?
      dsl_raise SyntaxError, "search_prefix must be called before defining fields"
    end

    @search_prefix = prefix.concat(@search_prefix)
  end

  @search_prefix
end