Sha256: 85a6559743c7f7733523f34ff3af3e14eb99c1ca115e7158ae8069d54579dab2

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'tempfile'

module Rdpl
  class Job
    attr_reader :labels, :printer, :state
    attr_writer :sensor

    include Commandable
    include Enumerable

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

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

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

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

    def in?; measurement == :inches; end

    def mm?; measurement == :metric; end

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

    def dump; @contents.dup; end

    def feed; command FEED; end

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

    private
    def printer=(printer); @printer = printer; end
    def measurement=(measurement); @measurement = measurement; end

    def initialize_options(options)
      validate_measurement_option options[:measurement]
      options.each_pair { |option, value| self.send("#{option}=", value) }  
      raise MissingPrinterNameError if printer.nil?
      @state = :open
    end

    def start; command sensor; end

    def validate_measurement_option(option)
      raise ArgumentError, 'should be :metric or :inches' unless [nil, :metric, :inches].include?(option)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdpl-0.1.0 lib/job.rb