Class: Dashes::Chart
Instance Method Summary collapse
-
#initialize ⇒ Chart
constructor
A new instance of Chart.
- #max_width(max_width) ⇒ Object
- #row(label, num) ⇒ Object
- #title(title) ⇒ Object
- #to_s ⇒ Object
- #total_width ⇒ Object
- #width(width) ⇒ Object
Constructor Details
#initialize ⇒ Chart
Returns a new instance of Chart.
145 146 147 148 149 150 |
# File 'lib/dashes.rb', line 145 def initialize @title = nil @rows = [] @width = nil @max_width = nil end |
Instance Method Details
#max_width(max_width) ⇒ Object
164 165 166 |
# File 'lib/dashes.rb', line 164 def max_width(max_width) @max_width = max_width end |
#row(label, num) ⇒ Object
156 157 158 |
# File 'lib/dashes.rb', line 156 def row(label, num) @rows << [label, num] end |
#title(title) ⇒ Object
152 153 154 |
# File 'lib/dashes.rb', line 152 def title(title) @title = title end |
#to_s ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/dashes.rb', line 168 def to_s return '' if @rows.empty? , wlabel = , label_width wtotal = total_width - 4 # 4 for side borders and padding = wtotal - wlabel - 1 = > ? /.to_f : 1 separator = "+-#{'-'*wtotal}-+" format = [separator] if @title format << "| #{cell(@title, wtotal)} |" format << separator end @rows.each do |label, num| = '=' * (num * ).floor format << "| #{cell(label, wlabel, :right)} #{cell(, )} |" end format << separator format.join("\n") end |
#total_width ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/dashes.rb', line 189 def total_width if @rows.empty? 0 elsif @width @width else # 4 for borders/padding twidth = [ + label_width + 1, title_width].max + 4 @max_width ? [twidth, @max_width].min : twidth end end |
#width(width) ⇒ Object
160 161 162 |
# File 'lib/dashes.rb', line 160 def width(width) @width = width end |