Module: Prawn::Component::DocumentExtensions

Defined in:
lib/prawn/component/document_extensions.rb

Instance Method Summary collapse

Instance Method Details

#data_source(name) ⇒ Object



4
5
6
7
8
9
# File 'lib/prawn/component/document_extensions.rb', line 4

def data_source(name)
  define_singleton_method(name) do
    @data_source ||= {}
    @data_source[name] ||= yield
  end
end

#draw(component, params = {}) ⇒ Object

consider adding support for padding and if we ever allow dynamic rendering, margins



13
14
15
16
17
18
19
20
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
# File 'lib/prawn/component/document_extensions.rb', line 13

def draw(component, params={})
  if Symbol === component
    component = Prawn::Component.definitions[component]
  end

  box = params.delete(:box)
  border = params.delete(:border)

  if box.nil?
    component.new(self, params).draw
    stroke_bounds if border
  elsif box.kind_of?(Prawn::Document::GridBox) # FIXME: Massive duplication + explicit type check
    box.bounding_box do
      bounds.define_singleton_method(:move_past_bottom) do
        raise Prawn::Errors::CannotFit
      end

      component.new(self, params).draw
      stroke_bounds if border
    end
  else
    left, top, width, height = box

    top  = top >= 0 ? bounds.top - top : top*-1
    left = left >= 0 ? left : bounds.right - left*-1

    bounding_box([left, top], :width => width, :height => height) do
      bounds.define_singleton_method(:move_past_bottom) do
        raise Prawn::Errors::CannotFit
      end

      component.new(self, params).draw
      stroke_bounds if border
    end
  end
end