Class: Rdpl::Job

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Commandable
Defined in:
lib/job.rb

Overview

A Job instance represents a print job to be sent to the printer. A print job may contain a list of instances of Rdpl::Label.

The default process to send jobs to the printer is configuring the printer in cups and passing the printer’s cups identifier to the constructor. Rdpl will use this id and issue something like lpr -P cups_id some_temp_file. This makes this lib unusable if you’re not on some kind of *nix box.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commandable

#command

Constructor Details

#initialize(options = {}) ⇒ Job

Creates a new instance of Rdpl::Job

Example:

job = Rdpl::Job.new :printer => "some_cups_id"

The possible options are:

  • :printer the cups id of the printer to be used. This option is required.

  • :sensor the type of sensor to be used. Can be one of Rdpl::Sensor::REFLEXIVE or Rdpl::Sensor::EDGE. This is optional and defaults to Rdpl::Sensor::EDGE

  • :measurement the measurement system to be used. Can be one of :inches or :metric. For metric, mm will be used as unit. This is optional and defaults to :inches.



29
30
31
32
33
34
# File 'lib/job.rb', line 29

def initialize(options = {})
  initialize_options options
  @contents = ''
  start
  @labels = []
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



12
13
14
# File 'lib/job.rb', line 12

def labels
  @labels
end

#printerObject

Returns the value of attribute printer.



12
13
14
# File 'lib/job.rb', line 12

def printer
  @printer
end

#sensorObject

Returns the current sensor type. If no :sensor option was specified, defaults to Rdpl::Sensor::EDGE.



43
# File 'lib/job.rb', line 43

def sensor; @sensor ||= Sensor::EDGE; end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/job.rb', line 12

def state
  @state
end

Instance Method Details

#<<(label) ⇒ Object Also known as: add_label

Adds a new Rdpl::Label to be printed in this job. After a label in added, the job will insert a FEED command (STX F <CR><LF>).



56
57
58
59
60
61
# File 'lib/job.rb', line 56

def <<(label)
  @labels << label
  label.job = self
  @contents << label.dump
  feed
end

#dumpObject

Dumps the contents to be printed to a string.



65
# File 'lib/job.rb', line 65

def dump; @contents.dup; end

#eachObject

Yields each Rdpl::Label instance contained in this job.



37
38
39
# File 'lib/job.rb', line 37

def each
  @labels.each { |label| yield label }
end

#feedObject

Insert a FEED</t> command (<tt>STX F <CR><LF>).



68
# File 'lib/job.rb', line 68

def feed; command FEED; end

#in?Boolean

Returns true if the current measurement system is :inches.

Returns:

  • (Boolean)


49
# File 'lib/job.rb', line 49

def in?; measurement == :inches; end

#measurementObject

Returns the current measurement system in use.



46
# File 'lib/job.rb', line 46

def measurement; @measurement ||= :inches; end

#mm?Boolean

Returns true if the current measurement system is :metric.

Returns:

  • (Boolean)


52
# File 'lib/job.rb', line 52

def mm?; measurement == :metric; end

Sends the job’s contents to the printer. The destination will be the cups printer id informed in the job’s creation.

The printing process is very simple, Rdpl will create a temp file and issue a lpr system command using the cups printer id and this file.



75
76
77
78
79
80
# File 'lib/job.rb', line 75

def print
  tempfile = Tempfile.new 'datamax_label'
  tempfile << dump
  tempfile.close
  Kernel.system "lpr -P #{printer} #{tempfile.path}"
end