has_barcode

Nice class method wrapper for barby

MIT License

Stars
52

= has_barcode

{}[http://travis-ci.org/dpickett/has_barcode]

A nice wrapper for Barcode generation using {barby}[https://github.com/toretore/barby].

class Product include HasBarcode

has_barcode :barcode,
  :outputter => :png,
  :type => :code_39,
  :value => Proc.new { |p| p.number }

def number
  self.id
end

end

Product.new.barcode # => Barby::Code39 object Product.new.barcode_data # => <Barby::Code39 object>.to_png

You can also pass the Barby barcode type directly like:

require 'barby/barcode/code_39' class Product include HasBarcode

has_barcode :barcode,
  :outputter => :png,
  :type => Barby::Code39,
  :value => Proc.new { |p| p.number }

end

== Why has_barcode is a good choice for Heroku Other libraries – such as {barcode_generator}[https://github.com/anujluthra/barcode-generator] – commonly rely on {gbarcode}[https://github.com/ahx/gbarcode], a GNU Barcode C library. This is problem since you need to install GNU barcode which {Heroku does not support}[https://github.com/perezd/barcoder/issues/1]. Luckily has_barcode doesn't have this dependency.

A typical Heroku setup might look like:

class Coupon < ActiveRecord::Base include HasBarcode

has_barcode :barcode,
  :outputter => :svg,
  :type => :code_39,
  :value => Proc.new { |c| c.id }

end

Notice we're using the svg outputter. This is good choice since it doesn't need to store images in the file system, which can be an issue given {Heroku's read only file system}[http://devcenter.heroku.com/articles/read-only-filesystem].

To display your barcode in the view, do something like:

@coupon.barcode_data.html_safe

== Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a
    future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    (if you want to have your own version, that is fine but
    bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2009 Dan Pickett. See LICENSE for details.