Module: Pictureframe

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

Constant Summary collapse

VERSION =
"0.0.17"

Class Method Summary collapse

Class Method Details

.breakText(string, numChars) ⇒ Object



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

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

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



48
49
50
51
52
53
# File 'lib/pictureframe.rb', line 48

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



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
37
38
39
40
41
42
# File 'lib/pictureframe.rb', line 10

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