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.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, **options) ⇒ 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)
  • :document (Prawn::Document)

39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prawn/label_sheet.rb', line 39

def initialize(labels, **options)
  @layout = setup_layout(options[:layout])

  @document = resolve_document(options[:document])
  @document.define_grid @layout
  # @document.on_page_create { self.instance_variable_set :@count, 0 }

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

  labels.each do |label|
    make_label(label, options) { |pdf, item| yield pdf, item }
  end
end

Instance Attribute Details

#documentObject (readonly)

Override Prawn::View#document


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

def document
  @document
end

#layoutObject (readonly)

Returns the value of attribute layout


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

def layout
  @layout
end

Class Method Details

.configPrawn::LabelSheet::Configuration

Current configuration


29
30
31
# File 'lib/prawn/label_sheet/configuration.rb', line 29

def self.config
  @config ||= Configuration.new
end

.config=(configuration) ⇒ Object

Define configuration

Parameters:


36
37
38
# File 'lib/prawn/label_sheet/configuration.rb', line 36

def self.config=(configuration)
  @config = configuration
end

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

Modify configuration

Yield Parameters:


43
44
45
# File 'lib/prawn/label_sheet/configuration.rb', line 43

def self.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, CSV)
  • options (Hash)

    a customizable set of options

Yield Parameters:

  • doc (Prawn::Document)

    document

  • item (Object)

    label item


30
31
32
33
# File 'lib/prawn/label_sheet.rb', line 30

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


60
61
62
63
64
65
66
67
# File 'lib/prawn/label_sheet.rb', line 60

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

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