Class: Rdpl::Job
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
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#printer ⇒ Object
readonly
Returns the value of attribute printer.
-
#sensor ⇒ Object
Returns the current sensor type.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#<<(label) ⇒ Object
(also: #add_label)
Adds a new
Rdpl::Label
to be printed in this job. -
#dump ⇒ Object
Dumps the contents to be printed to a string.
-
#each ⇒ Object
Yields each
Rdpl::Label
instance contained in this job. -
#feed ⇒ Object
Insert a
FEED</t> command (<tt>STX F <CR><LF>
). -
#in? ⇒ Boolean
Returns
true
if the current measurement system is:inches
. -
#initialize(options = {}) ⇒ Job
constructor
Creates a new instance of Rdpl::Job.
-
#measurement ⇒ Object
Returns the current measurement system in use.
-
#mm? ⇒ Boolean
Returns
true
if the current measurement system is:metric
. -
#print ⇒ Object
Sends the job’s contents to the printer.
Methods included from Commandable
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 ofRdpl::Sensor::REFLEXIVE
orRdpl::Sensor::EDGE
. This is optional and defaults toRdpl::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( = {}) @contents = '' start @labels = [] end |
Instance Attribute Details
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
12 13 14 |
# File 'lib/job.rb', line 12 def labels @labels end |
#printer ⇒ Object
Returns the value of attribute printer.
12 13 14 |
# File 'lib/job.rb', line 12 def printer @printer end |
#sensor ⇒ Object
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 |
#state ⇒ Object (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 |
#dump ⇒ Object
Dumps the contents to be printed to a string.
65 |
# File 'lib/job.rb', line 65 def dump; @contents.dup; end |
#each ⇒ Object
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 |
#feed ⇒ Object
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
.
49 |
# File 'lib/job.rb', line 49 def in?; measurement == :inches; end |
#measurement ⇒ Object
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
.
52 |
# File 'lib/job.rb', line 52 def mm?; measurement == :metric; end |
#print ⇒ Object
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 |