class ZplScaler::ZplReader

NOTE: doesn't handle ZPL that changes the control char (default: '^')

Constants

RX_ZPL_COMMAND

Example format: ^XXparam1,param2,,param4 ZplCommand name: XX (the command is read as 2 chars, no more no less) 4 (5) params (param 3 & 5 are not given)

Public Class Methods

new(content) click to toggle source

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

# File lib/zpl-scaler.rb, line 42
def initialize(content)
  @scanner = StringScanner.new content
end
uniq_commands(zpl_content) click to toggle source

Returns the list of unique commands used in the given ZPL.

# File lib/zpl-scaler.rb, line 33
def self.uniq_commands(zpl_content)
  uniq_cmds = Set.new
  new(zpl_content).each_command do |cmd|
    uniq_cmds << cmd.name
  end
  uniq_cmds.to_a
end

Public Instance Methods

each_command() { |cmd| ... } click to toggle source

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

# File lib/zpl-scaler.rb, line 59
def each_command
  while cmd = next_command
    yield cmd
  end
end
next_command() click to toggle source

Returns the next zpl command if any, or nil.

# File lib/zpl-scaler.rb, line 47
def next_command
  return if @scanner.eos?

  @scanner.scan(RX_ZPL_COMMAND)

  cmd_name = @scanner[1]
  raw_params = @scanner[2]

  ZplCommand.new(cmd_name, raw_params&.split(',') || [])
end