Class: Prawn::Labels

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}, &block) ⇒ Labels

Returns a new instance of Labels.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/prawn/labels.rb', line 43

def initialize(data, options = {}, &block)
  unless @type = Labels.types[options[:type]]
    raise "Label Type Unknown '#{options[:type]}'"
  end

  type["paper_size"]  ||= "A4"
  type["top_margin"]  ||= 36
  type["left_margin"] ||= 36

  options[:document] ||= {}

  options.merge!(:vertical_text => true) if type["vertical_text"]

  @document = Document.new  options[:document].merge(
                            :page_size      => type["paper_size"],
                            :top_margin     => type["top_margin"],
                            :bottom_margin  => type["bottom_margin"],
                            :left_margin    => type["left_margin"],
                            :right_margin   => type["right_margin"])

  generate_grid @type

  data.each_with_index do |record, index|
    if (defined? record.vertical_text)
      options.merge!(:vertical_text => record.vertical_text)
    end
    create_label(index, record, options) do |pdf, record|
      yield pdf, record
    end
  end

end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



12
13
14
# File 'lib/prawn/labels.rb', line 12

def document
  @document
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/prawn/labels.rb', line 12

def type
  @type
end

Class Method Details

.generate(file_name, data, options = {}, &block) ⇒ Object



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

def generate(file_name, data, options = {}, &block)
  labels = Labels.new(data, options, &block)
  labels.document.render_file(file_name)
end

.render(data, options = {}, &block) ⇒ Object



21
22
23
24
# File 'lib/prawn/labels.rb', line 21

def render(data, options = {}, &block)
  labels = Labels.new(data, options, &block)
  labels.document.render
end

.typesObject



34
35
36
37
38
39
# File 'lib/prawn/labels.rb', line 34

def types
  @types ||= begin
    types_file = File.join(File.dirname(__FILE__), 'types.yaml')
    YAML.load_file(types_file)
  end
end

.types=(custom_types) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/prawn/labels.rb', line 26

def types=(custom_types)
  if custom_types.is_a? Hash
    types.merge! custom_types
  elsif custom_types.is_a?(String) && File.exist?(custom_types)
    types.merge! YAML.load_file(custom_types)
  end
end