Module: Pictureframe

Defined in:
lib/pictureframe.rb,
lib/pictureframe/version.rb

Constant Summary collapse

VERSION =
"0.0.16"

Class Method Summary collapse

Class Method Details

.breakText(string, numChars) ⇒ Object



38
39
40
# File 'lib/pictureframe.rb', line 38

def self.breakText(string, numChars)
  string.scan(/#{'.{1,' + numChars.to_s}}/)
end

.expandLine(left, right, filler, width, text = "") ⇒ Object



42
43
44
45
46
47
# File 'lib/pictureframe.rb', line 42

def self.expandLine(left, right, filler, width, text = "")
  totalLength = left.length + right.length + text.length
  width = totalLength if totalLength > width
  numFiller = width - totalLength
  left + text + filler*numFiller + right
end

.frame(text, width = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pictureframe.rb', line 4

def self.frame(text, width = nil)
  if text.nil?
    return []
  else
    text = text.to_s
  end

  inputLocation = 3
  output = []
  lefts =  [".___", "| ._", "| | ", "| ._", "|___"]
  rights = ["___.", "_. |", " | |", "_. |", "___|"]
  insideLines = [text]

  if width
    textSpace = width - (lefts[0].length * 2)
    insideLines = textSpace < text.length ? breakText(text, textSpace) : [text]
  else
    totalLength = lefts[0].length + rights[0].length + text.length
    width = totalLength
  end

  lefts.each_with_index do |row, i|
    filler =  i == 2 ? " " : "_"
    output[i] = expandLine(lefts[i], rights[i], filler, width)
  end

  insideLines.reverse.each do |text|
    output = output.insert(inputLocation, expandLine("| | ", " | |", " ", width, text))
  end

  puts output.join("\n")
  return output
end