Class: LabelPrinter::PrintJob

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/label_printer/print_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(printer_name, label_class, options) ⇒ PrintJob

Returns a new instance of PrintJob.



16
17
18
19
20
# File 'lib/label_printer/print_job.rb', line 16

def initialize(printer_name, label_class, options)
  @printer_name = printer_name
  @label_class = label_class
  @options = options
end

Instance Attribute Details

#label_classObject (readonly)

Returns the value of attribute label_class.



14
15
16
# File 'lib/label_printer/print_job.rb', line 14

def label_class
  @label_class
end

#labelsObject (readonly)

Returns the value of attribute labels.



14
15
16
# File 'lib/label_printer/print_job.rb', line 14

def labels
  @labels
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/label_printer/print_job.rb', line 14

def options
  @options
end

#printer_nameObject (readonly)

Returns the value of attribute printer_name.



14
15
16
# File 'lib/label_printer/print_job.rb', line 14

def printer_name
  @printer_name
end

Instance Method Details

#build_attributesObject



39
40
41
42
43
44
45
# File 'lib/label_printer/print_job.rb', line 39

def build_attributes
  @build_attributes ||= {
    printer_name: printer_name,
    label_template_name: label_template_name,
    labels: labels_attribute
  }
end

#executeObject

rubocop:todo Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/label_printer/print_job.rb', line 22

def execute # rubocop:todo Metrics/MethodLength
  begin
    LabelPrinter::PmbClient.print(build_attributes)
  rescue LabelPrinter::PmbException => e
    errors.add(:printmybarcode, e)
    return false
  rescue BarcodePrinter::BarcodePrinterException => e
    errors.add(:printer, e)
    return false
  rescue SampleManifest::MultiplexedLibraryBehaviour::Core::MxLibraryTubeException => e
    errors.add(:mx_tube, e)
    return false
  end

  true
end

#find_printerObject



67
68
69
70
# File 'lib/label_printer/print_job.rb', line 67

def find_printer
  BarcodePrinter.find_by(name: printer_name) or
    raise BarcodePrinter::BarcodePrinterException.new, "Could not find barcode printer #{printer_name.inspect}"
end

#label_template_nameString

Returns the name of the label template to use for this print job. If not specified in the options during initialisation, the label template name configured for the printer in the database is used.

Returns:

  • (String)

    the name of the label template to use for this print job



60
61
62
63
64
65
# File 'lib/label_printer/print_job.rb', line 60

def label_template_name
  return options[:label_template_name] if options[:label_template_name]

  printer = find_printer
  printer.barcode_printer_type.label_template_name
end

#labels_attributeObject

returns: a list of labels



48
49
50
51
52
# File 'lib/label_printer/print_job.rb', line 48

def labels_attribute
  printer = find_printer
  printer_type_class = { printer_type_class: printer.barcode_printer_type.class }
  @labels = label_class.new(options.merge(printer_type_class)).labels
end

#number_of_labelsObject



76
77
78
# File 'lib/label_printer/print_job.rb', line 76

def number_of_labels
  labels.count
end

#successObject



72
73
74
# File 'lib/label_printer/print_job.rb', line 72

def success
  "Your #{number_of_labels} label(s) have been sent to printer #{printer_name}"
end