Class: ActionView::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/barcoder.rb

Constant Summary collapse

VALID_BARCODER_OPTIONS =

important defaults, should not be messed with.

[:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :output_type]
DEFAULT_BARCODER_OUTPUT_FORMAT =
'gif'
DEFAULT_BARCODER_ENCODING =
Gbarcode::BARCODE_39 | Gbarcode::BARCODE_NO_CHECKSUM
BARCODE_STORAGE_PATH =
"public/images/barcodes"

Instance Method Summary collapse

Instance Method Details

#barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING }) ⇒ Object

support for the original barcode-generator plugin syntax.



54
55
56
# File 'lib/barcoder.rb', line 54

def barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING })
  to_barcode(str, options)
end

#to_barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING }) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/barcoder.rb', line 21

def to_barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING })
  # verify requirements
  options.assert_valid_keys(VALID_BARCODER_OPTIONS)
  output_format = options[:output_format] ? options[:output_format] : DEFAULT_BARCODER_OUTPUT_FORMAT
  output_type = options[:output_type] ? options[:output_type] : :stream
  # generate the barcode object with all supplied options
  options[:encoding_format] = DEFAULT_BARCODER_ENCODING unless options[:encoding_format]
  bc = Gbarcode.barcode_create(str.to_s)
  
  bc.width  = options[:width]          if options[:width]
  bc.height = options[:height]         if options[:height]
  bc.scalef = options[:scaling_factor] if options[:scaling_factor]
  bc.xoff   = options[:xoff]           if options[:xoff]
  bc.yoff   = options[:yoff]           if options[:yoff]
  bc.margin = options[:margin]         if options[:margin]
  
  Gbarcode.barcode_encode(bc, options[:encoding_format])
  
  if options[:no_ascii]
    print_options = Gbarcode::BARCODE_OUT_EPS|Gbarcode::BARCODE_NO_ASCII
  else
    print_options = Gbarcode::BARCODE_OUT_EPS
  end
  
  # this is where the magic happens.
  data = `echo "#{get_bytes_from_barcode(bc, print_options)}" | convert eps: #{output_format}:`
  
  # simple output strategy, define :output_type => :disk in the #to_barcode call if you want
  # it to write out to the disk for you, otherwise it will be a data url stream.
  output_type == :disk ? barcode_to_disk(data, bc, output_format) : barcode_to_stream(data, output_format, str)
end