Class: PictureFrame::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/picture_frame/frame.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_string, options = {}) ⇒ Frame

Returns a new instance of Frame.


7
8
9
10
# File 'lib/picture_frame/frame.rb', line 7

def initialize(template_string, options = {})
  @template    = Stencil.new(template_string)
  @placeholder = options[:placeholder] || '@'
end

Instance Method Details

#frame(text) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/picture_frame/frame.rb', line 12

def frame(text)
  image = Image.new(text)

  Canvas.new(@template.bottom_right, image.dimensions).tap do |canvas|
    pp = placeholder_position
    return '' if pp.empty?

    r1, c1 = pp
    rn, cm = image.dimensions

    canvas.print_at(:tl, @template.slice_from(pp, :tl))
    canvas.print_at(:tr, @template.slice_from(pp, :tr))
    canvas.print_at(:br, @template.slice_from(pp, :br))
    canvas.print_at(:bl, @template.slice_from(pp, :bl))

    canvas.print_at(:t, @template.slice_from(pp, :t), c1, cm)
    canvas.print_at(:b, @template.slice_from(pp, :b), c1, cm)
    canvas.print_at(:l, @template.slice_from(pp, :l), r1, rn)
    canvas.print_at(:r, @template.slice_from(pp, :r), r1, rn)

    canvas.print_at(pp, image)
  end.to_s
end