Class: ZplScaler::ZplReader

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

Overview

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

Constant Summary collapse

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)

/\^([A-Z0-9]{2})([^\^]*)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ ZplReader

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



42
43
44
# File 'lib/zpl-scaler.rb', line 42

def initialize(content)
  @scanner = StringScanner.new content
end

Class Method Details

.uniq_commands(zpl_content) ⇒ Object

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



33
34
35
36
37
38
39
# 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

Instance Method Details

#each_commandObject

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



59
60
61
62
63
# File 'lib/zpl-scaler.rb', line 59

def each_command
  while cmd = next_command
    yield cmd
  end
end

#next_commandObject

Returns the next zpl command if any, or nil.



47
48
49
50
51
52
53
54
55
56
# 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