Module: Seimi::Sangi
- Defined in:
- lib/seimi/sangi.rb
Constant Summary collapse
- CELL_WIDTH =
5- CELL_HEIGHT =
5
Class Method Summary collapse
- .blank_cell ⇒ Object
- .body_lines(cells) ⇒ Object
- .empty_cell ⇒ Object
- .frame(left, middle, right, count) ⇒ Object
- .horizontal_cell(digit) ⇒ Object
- .render(number) ⇒ Object
- .vertical_cell(digit) ⇒ Object
Class Method Details
.blank_cell ⇒ Object
58 59 60 |
# File 'lib/seimi/sangi.rb', line 58 def blank_cell Array.new(CELL_HEIGHT, " " * CELL_WIDTH) end |
.body_lines(cells) ⇒ Object
48 49 50 51 52 |
# File 'lib/seimi/sangi.rb', line 48 def body_lines(cells) (0...CELL_HEIGHT).map do |line| "│#{cells.map { |cell| cell[line] }.join("│")}│" end end |
.empty_cell ⇒ Object
62 63 64 |
# File 'lib/seimi/sangi.rb', line 62 def empty_cell [" ", " ", " [ ] ", " ", " "] end |
.frame(left, middle, right, count) ⇒ Object
54 55 56 |
# File 'lib/seimi/sangi.rb', line 54 def frame(left, middle, right, count) "#{left}#{Array.new(count, "─" * CELL_WIDTH).join(middle)}#{right}" end |
.horizontal_cell(digit) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/seimi/sangi.rb', line 35 def horizontal_cell(digit) return empty_cell if digit.zero? rows = blank_cell if digit <= 5 digit.times { |index| rows[CELL_HEIGHT - 1 - index] = " --- " } else rows[0] = " | " (digit - 5).times { |index| rows[CELL_HEIGHT - 1 - index] = " --- " } end rows end |
.render(number) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/seimi/sangi.rb', line 10 def render(number) digits = Integer(number).abs.digits.reverse cells = digits.each_with_index.map do |digit, index| power = digits.length - index - 1 power.even? ? vertical_cell(digit) : horizontal_cell(digit) end [frame("┌", "┬", "┐", cells.length), *body_lines(cells), frame("└", "┴", "┘", cells.length)].join("\n") end |
.vertical_cell(digit) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/seimi/sangi.rb', line 22 def vertical_cell(digit) return empty_cell if digit.zero? rows = blank_cell if digit <= 5 digit.times { |index| rows[index] = " | " } else rows[0] = " --- " (digit - 5).times { |index| rows[index + 1] = " | " } end rows end |