Class: TextChart

Inherits:
Object
  • Object
show all
Defined in:
lib/text_chart.rb,
lib/text_chart/version.rb

Overview

This is the main class of the text_chart. After calling the #to_s method you’ll receive a string representing the chart, just like this one:

text_chart demonstration Show you how cool this is

73| ###

|                                                         ###

67| ### ### ### ###

|             ###             ###                         ###         ###

54| ### ### ### ### ### ### ###

|     ###     ###     ###     ###                         ### ###     ###

44| ### ### ### ### ### ### ### ### ### ###

|     ### ### ###     ###     ###     ###                 ### ###     ### ###

33| ### ### ### ### ### ### ### ### ### ### ###

|     ### ### ###     ### ### ###     ###                 ### ###     ### ###

27| ### ### ### ### ### ### ### ### ### ### ### ### ### ### 20| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 14| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 10| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###

5| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
 ----------------------------------------------------------------------------------

Defined Under Namespace

Classes: Designer, Error, SizeCalculator

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, subtitle, data, colors = false) ⇒ TextChart

Returns a new instance of TextChart.

Parameters:

  • title (String)
  • subtitle (String)
  • data (Array)
  • colors (Boolean) (defaults to: false)

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/text_chart.rb', line 36

def initialize(title, subtitle, data, colors = false)
  raise Error, "`data` cannot be empty" if data.empty?

  @title = title
  @subtitle = subtitle
  @data = data.empty? ? [0] : data.filter(&:positive?)
  @colors = colors
  @size_calculator = SizeCalculator.new(self)
  @designer = Designer.new(self, @size_calculator)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



47
48
49
# File 'lib/text_chart.rb', line 47

def data
  @data
end

#designerObject (readonly)

Returns the value of attribute designer.



47
48
49
# File 'lib/text_chart.rb', line 47

def designer
  @designer
end

#size_calculatorObject (readonly)

Returns the value of attribute size_calculator.



47
48
49
# File 'lib/text_chart.rb', line 47

def size_calculator
  @size_calculator
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



47
48
49
# File 'lib/text_chart.rb', line 47

def subtitle
  @subtitle
end

#titleObject (readonly)

Returns the value of attribute title.



47
48
49
# File 'lib/text_chart.rb', line 47

def title
  @title
end

Instance Method Details

#size_config(key) ⇒ Integer

Parameters:

  • key (Symbol)

Returns:

  • (Integer)


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

def size_config(key)
  SIZE_CONFIG[key]
end

#to_sString

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
# File 'lib/text_chart.rb', line 50

def to_s
  result = @designer.draw_axis &&
    @designer.draw_bars &&
    @designer.draw_header

  result = @designer.paint if @colors

  result.join
end