Class: Prawn::LabelSheet

Inherits:
Object
  • Object
show all
Includes:
View
Defined in:
lib/prawn/label_sheet.rb,
lib/prawn/label_sheet/version.rb,
lib/prawn/label_sheet/configuration.rb

Overview

Document supporting bulk label/sticker generation

Defined Under Namespace

Classes: Configuration, Error

Constant Summary collapse

VERSION =

Gem version

'0.2.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, **options) {|doc, item| ... } ⇒ LabelSheet

Returns a new instance of LabelSheet.

Parameters:

  • labels (Enumerable)

    collection of labels

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :layout (String)
  • :break_on (Proc, Integer, String)
  • :document (Prawn::Document)

Yield Parameters:

  • doc (Prawn::Document)

    document

  • item (Object)

    label item



37
38
39
40
41
42
43
44
45
# File 'lib/prawn/label_sheet.rb', line 37

def initialize(labels, **options, &block)
  @layout = setup_layout(options[:layout]).merge(info: options[:info])
  @document = setup_document(options[:document], @layout)

  @count = 0
  @break_on = options[:break_on]

  labels.each { |label| make_label(label, options, &block) }
end

Class Attribute Details

.configPrawn::LabelSheet::Configuration

Current configuration



32
33
34
# File 'lib/prawn/label_sheet/configuration.rb', line 32

def config
  @config ||= Configuration.new
end

Instance Attribute Details

#documentObject (readonly)

Override Prawn::View#document



17
18
19
# File 'lib/prawn/label_sheet.rb', line 17

def document
  @document
end

#layoutObject (readonly)

Returns the value of attribute layout.



18
19
20
# File 'lib/prawn/label_sheet.rb', line 18

def layout
  @layout
end

Class Method Details

.configure {|config| ... } ⇒ Object

Modify configuration

Yield Parameters:



39
40
41
# File 'lib/prawn/label_sheet/configuration.rb', line 39

def configure
  yield config
end

.generate(filename, labels, **options) {|doc, item| ... } ⇒ Object

Render and persist a set of label sheets

Parameters:

  • filename (String)

    Name of output file

  • labels (Enumerable)

    collection of labels

  • options (Hash)

    a customizable set of options

Yield Parameters:

  • doc (Prawn::Document)

    document

  • item (Object)

    label item



26
27
28
29
# File 'lib/prawn/label_sheet.rb', line 26

def self.generate(filename, labels, **options, &block)
  pdf = new(labels, **options, &block)
  pdf.document.render_file(filename)
end

Instance Method Details

#make_label(item, _options) {|doc, item| ... } ⇒ Integer

Generate individual label

Parameters:

  • item (#[], Object)

Yield Parameters:

  • doc (Prawn::Document)

    document

  • item (Object)

    label item

Returns:

  • (Integer)

    tally of labels on current page



53
54
55
56
57
58
# File 'lib/prawn/label_sheet.rb', line 53

def make_label(item, _options)
  break_page if break_page?(item)

  @document.grid(*gridref).bounding_box { yield @document, item }
  @count += 1
end