Class: Rdpl::Label

Inherits:
Object
  • Object
show all
Includes:
Commandable
Defined in:
lib/label.rb

Overview

Represents a label to be printed. Labels must me included in an instance of Rdpl::Job.

Constant Summary collapse

START =
'L'
FINISH =
'E'
DEFAULT_DOT_SIZE =
11
DEFAULT_HEAT =
14

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commandable

#command

Constructor Details

#initialize(options = {}) ⇒ Label

Creates a new instance of Rdpl::Label

Available options are:

  • :heat the heat setting to be used when printing this label. This is optional and defaults to 14.

  • :dot_size the dot size to be used when printing this label. This is optional and defaults to 11.

  • :start_of_print the inicial position to start printing the label. This is optional and has no default value, since this setting depends on the printer model.

  • :quantity the number of copies of this label to be printed.



22
23
24
25
# File 'lib/label.rb', line 22

def initialize(options = {})
  @contents = ''
  start options
end

Instance Attribute Details

#job=(value) ⇒ Object (writeonly)

Sets the attribute job

Parameters:

  • value

    the value to set the attribute job to.



5
6
7
# File 'lib/label.rb', line 5

def job=(value)
  @job = value
end

#quantityObject (readonly)

Returns the value of attribute quantity.



4
5
6
# File 'lib/label.rb', line 4

def quantity
  @quantity
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/label.rb', line 4

def state
  @state
end

Instance Method Details

#<<(arg) ⇒ Object

Adds a new element to this label. An element can be one of the following:

  • Rdpl::Barcode

  • Rdpl::BitmappedText

  • Rdpl::Box

  • Rdpl::Line

  • A simple string containing DPL commands

Raises:



51
52
53
54
# File 'lib/label.rb', line 51

def <<(arg)
  raise EndedElementError unless state == :open
  @contents << arg.to_s << NEW_LINE
end

#[](arg) ⇒ Object

:nodoc:



40
41
42
# File 'lib/label.rb', line 40

def [](arg) # :nodoc:
  @contents[arg]
end

#dot_sizeObject

Returns the current dot size.



57
58
59
# File 'lib/label.rb', line 57

def dot_size
  @dot_size || DEFAULT_DOT_SIZE
end

#dumpObject

Dumps this label’s contents to a string.



28
29
30
# File 'lib/label.rb', line 28

def dump
  @contents.dup
end

#end!Object

Closes this label for edition. This appends DPL specific commands to the label, so the printer knows where the printing must end and where another label is started.



34
35
36
37
38
# File 'lib/label.rb', line 34

def end!
  self << formatted_quantity unless quantity.nil?
  self << FINISH 
  self.state = :finished
end

#heatObject

Returns the current heat setting to be used when printing.



62
63
64
# File 'lib/label.rb', line 62

def heat
  @heat || DEFAULT_HEAT
end

#mm?Boolean

Returns true if this label’s job is setted to use millimeters as measurement unit. I the label does not have a job yet, it will return false.

Returns:

  • (Boolean)


68
69
70
# File 'lib/label.rb', line 68

def mm?
  @job ? @job.mm? : false
end

#start_of_printObject

Returns the start of print position for the label. If the option wasn’t specified in the label’s constructor, nil will be returned and the printer will assume the default start of print.

It works this way since the default value for this parameter depends on the printer’s model.



77
78
79
80
# File 'lib/label.rb', line 77

def start_of_print
  return nil if @start_of_print.nil?
  @start_of_print.to_f / (mm? ? 10 : 100)
end