Class: Tortoise::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/tortoise/presenter.rb

Constant Summary collapse

PIXEL_SIZE =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interpreter) ⇒ Presenter

Returns a new instance of Presenter.



9
10
11
# File 'lib/tortoise/presenter.rb', line 9

def initialize(interpreter)
  @canvas = interpreter.canvas
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



5
6
7
# File 'lib/tortoise/presenter.rb', line 5

def canvas
  @canvas
end

Instance Method Details

#to_asciiObject



13
14
15
16
17
# File 'lib/tortoise/presenter.rb', line 13

def to_ascii
  oriented_canvas.inject('') do |ascii, column|
    ascii += column.map { |pixel| pixel ? 'X ' : '. ' }.join.strip + "\n"
  end
end

#to_htmlObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tortoise/presenter.rb', line 19

def to_html
  <<-HTML
    <!DOCTYPE html>
    <html>
    #{html_head}
    <body>
      #{html_canvas}
    </body>
    </html>
  HTML
end

#to_pngObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tortoise/presenter.rb', line 31

def to_png
  white = ChunkyPNG::Color('white')
  black = ChunkyPNG::Color('black')
  png   = ChunkyPNG::Image.new(@canvas.size, @canvas.size)

  @canvas.each_with_index do |column, x|
    column.reverse.each_with_index do |pixel, y|
      png[x, y] = pixel ? black : white
    end
  end

  png.to_blob
end