Class: PictureFrame::Raster

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/picture_frame/raster.rb

Direct Known Subclasses

Canvas, Image, Stencil

Instance Method Summary collapse

Constructor Details

#initialize(string_or_rows) ⇒ Raster

Returns a new instance of Raster.



5
6
7
8
9
10
# File 'lib/picture_frame/raster.rb', line 5

def initialize(string_or_rows)
  @raster = string_or_rows.is_a?(String) ? string_or_rows.split("\n") : string_or_rows

  trim_around!
  pad_short_rows!
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
15
# File 'lib/picture_frame/raster.rb', line 12

def ==(other)
  return unless other.is_a?(Raster)
  @raster == other.raster
end

#at(row, col) ⇒ Object Also known as: []



37
38
39
# File 'lib/picture_frame/raster.rb', line 37

def at(row, col)
  @raster[row][col]
end

#bottom_rightObject



25
26
27
# File 'lib/picture_frame/raster.rb', line 25

def bottom_right
  [dimensions[0] - 1, dimensions[1] - 1]
end

#dimensionsObject



17
18
19
# File 'lib/picture_frame/raster.rb', line 17

def dimensions
  @dimensions ||= [@raster.size, (@raster.map { |r| r.length }.max || 0)]
end

#each(&block) ⇒ Object



54
55
56
# File 'lib/picture_frame/raster.rb', line 54

def each(&block)
  @raster.each(&block)
end

#each_with_index(&block) ⇒ Object



58
59
60
# File 'lib/picture_frame/raster.rb', line 58

def each_with_index(&block)
  @raster.each_with_index(&block)
end

#heightObject



29
30
31
# File 'lib/picture_frame/raster.rb', line 29

def height
  dimensions[0]
end

#slice(top_left, bottom_right) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/picture_frame/raster.rb', line 43

def slice(top_left, bottom_right)
  top_bottom = (top_left[0]..bottom_right[0])
  left_right = (top_left[1]..bottom_right[1])

  array = top_bottom.inject([]) do |rows, ri|
    rows << @raster[ri].slice(left_right)
  end

  self.class.new(array)
end

#to_sObject



62
63
64
# File 'lib/picture_frame/raster.rb', line 62

def to_s
  @raster.join("\n")
end

#top_leftObject



21
22
23
# File 'lib/picture_frame/raster.rb', line 21

def top_left
  [0, 0]
end

#widthObject



33
34
35
# File 'lib/picture_frame/raster.rb', line 33

def width
  dimensions[1]
end