Class: Zpl::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/zpl-transformer/reader.rb

Overview

NOTE: doesn’t handle ZPL that changes the control char (default is assumed: ‘^’)

Instance Method Summary collapse

Constructor Details

#initialize(content, strip_spaces: true) ⇒ Reader

Creates a new reader that will read ZPL commands from content string.



10
11
12
13
# File 'lib/zpl-transformer/reader.rb', line 10

def initialize(content, strip_spaces: true)
  @scanner = StringScanner.new content
  @strip_spaces = strip_spaces
end

Instance Method Details

#each_commandObject

Yields each ZPL command to the block. Stops when there are no more commands to read.



40
41
42
43
44
# File 'lib/zpl-transformer/reader.rb', line 40

def each_command
  while cmd = next_command
    yield cmd
  end
end

#next_commandObject

Returns the next zpl command if any, or nil.



29
30
31
32
33
34
35
36
37
# File 'lib/zpl-transformer/reader.rb', line 29

def next_command
  while token = next_token
    if token.is_a?(Command)
      return token
    end
  end

  nil
end

#next_tokenObject

Returns the next zpl command or ignored string if any, or nil.

The ignored string can be a newlines, ignored chars, spaces or unsupported syntax.



18
19
20
21
22
23
24
25
26
# File 'lib/zpl-transformer/reader.rb', line 18

def next_token
  return if @scanner.eos?

  if chars_to_ignore = parse_ignore_chars
    return chars_to_ignore
  end

  parse_zpl_cmd
end