Class: BinData::DSLMixin::DSLFieldParser

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

Overview

Extracts the details from a field declaration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hints, symbol, *args, &block) ⇒ DSLFieldParser

Returns a new instance of DSLFieldParser.



340
341
342
343
344
345
# File 'lib/bindata/dsl.rb', line 340

def initialize(hints, symbol, *args, &block)
  @hints  = hints
  @type   = symbol
  @name   = name_from_field_declaration(args)
  @params = params_from_field_declaration(args, &block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



347
348
349
# File 'lib/bindata/dsl.rb', line 347

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



347
348
349
# File 'lib/bindata/dsl.rb', line 347

def params
  @params
end

#typeObject (readonly)

Returns the value of attribute type.



347
348
349
# File 'lib/bindata/dsl.rb', line 347

def type
  @type
end

Instance Method Details

#name_from_field_declaration(args) ⇒ Object



349
350
351
352
353
354
355
356
# File 'lib/bindata/dsl.rb', line 349

def name_from_field_declaration(args)
  name, _ = args
  if name == "" || name.is_a?(Hash)
    nil
  else
    name
  end
end

#params_from_args(args) ⇒ Object



368
369
370
371
372
373
# File 'lib/bindata/dsl.rb', line 368

def params_from_args(args)
  name, params = args
  params = name if name.is_a?(Hash)

  params || {}
end

#params_from_block(&block) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bindata/dsl.rb', line 375

def params_from_block(&block)
  bindata_classes = {
    array:      BinData::Array,
    buffer:     BinData::Buffer,
    choice:     BinData::Choice,
    delayed_io: BinData::DelayedIO,
    section:    BinData::Section,
    skip:       BinData::Skip,
    struct:     BinData::Struct
  }

  if bindata_classes.include?(@type)
    parser = DSLParser.new(bindata_classes[@type], @type)
    parser.endian(@hints[:endian])
    parser.search_prefix(*@hints[:search_prefix])
    parser.instance_eval(&block)

    parser.dsl_params
  else
    {}
  end
end

#params_from_field_declaration(args, &block) ⇒ Object



358
359
360
361
362
363
364
365
366
# File 'lib/bindata/dsl.rb', line 358

def params_from_field_declaration(args, &block)
  params = params_from_args(args)

  if block_given?
    params.merge(params_from_block(&block))
  else
    params
  end
end