Class: Cosmos::MacroParser

Inherits:
Object show all
Defined in:
lib/cosmos/packets/parsers/macro_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ MacroParser

Returns a new instance of MacroParser.

Parameters:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 47

def initialize(parser)
  @parser = parser
  @usage = '#{keyword} <FIRST INDEX> <LAST INDEX> [NAME FORMAT]'
  parser.verify_num_parameters(2, 3, @usage)
  @macro = OpenStruct.new(:started => true, :list => [])
  first_index = parser.parameters[0].to_i
  last_index  = parser.parameters[1].to_i
  if first_index < last_index
    @macro.indices = (first_index..last_index).to_a
  else
    @macro.indices = (last_index..first_index).to_a.reverse
  end
  @macro.format = parser.parameters[2] ? parser.parameters[2] : '%s%d'
  @macro.format_order = get_format_order()
end

Class Method Details

.end(parser, packet) ⇒ Object

Ends the Macro and adds all the items to the packet

Parameters:

  • parser (ConfigParser)

    Configuration Parser

  • packet (Packet)

    Packet to add the macro items to



37
38
39
40
41
42
43
44
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 37

def self.end(parser, packet)
  raise parser.error("First start a macro with MACRO_APPEND_START") unless @macro
  @macro.complete(packet)
ensure
  # Ensure this class instance variable gets cleared so we can process the
  # next call to start
  @macro = nil
end

.new_itemObject

Adds a new item to the Macro



16
17
18
19
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 16

def self.new_item
  return unless @macro
  @macro.new_item
end

.start(parser) ⇒ Object

Starts a new Macro

Parameters:



24
25
26
27
28
29
30
31
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 24

def self.start(parser)
  if @macro
    @macro = nil
    raise parser.error("First close the previous MACRO_APPEND_START with a MACRO_APPEND_END")
  else
    @macro = MacroParser.new(parser)
  end
end

Instance Method Details

#complete(packet) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 69

def complete(packet)
  @parser.verify_num_parameters(0, 0, @parser.keyword)
  raise @parser.error("Missing MACRO_APPEND_START before this config.line.", @parser.keyword) unless @macro
  raise @parser.error("No items appended in MACRO_APPEND list", @parser.keyword) if @macro.list.empty?

  create_new_packet_items(packet)
end

#new_itemObject



63
64
65
66
67
# File 'lib/cosmos/packets/parsers/macro_parser.rb', line 63

def new_item
  if @parser.keyword.include?('APPEND')
    @macro.list << @parser.parameters[0].upcase
  end
end