Class: FinModeling::CalculationSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/finmodeling/calculation_summary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCalculationSummary

Returns a new instance of CalculationSummary.



93
94
95
96
97
98
99
# File 'lib/finmodeling/calculation_summary.rb', line 93

def initialize
  @key_width = 18
  @val_width = 12
  @max_decimals = 4
  @totals_row_enabled = true
  @rows = [ ]
end

Instance Attribute Details

#header_rowObject

Returns the value of attribute header_row.



90
91
92
# File 'lib/finmodeling/calculation_summary.rb', line 90

def header_row
  @header_row
end

#key_widthObject

Returns the value of attribute key_width.



91
92
93
# File 'lib/finmodeling/calculation_summary.rb', line 91

def key_width
  @key_width
end

#max_decimalsObject

Returns the value of attribute max_decimals.



91
92
93
# File 'lib/finmodeling/calculation_summary.rb', line 91

def max_decimals
  @max_decimals
end

#rowsObject

Returns the value of attribute rows.



90
91
92
# File 'lib/finmodeling/calculation_summary.rb', line 90

def rows
  @rows
end

#titleObject

Returns the value of attribute title.



90
91
92
# File 'lib/finmodeling/calculation_summary.rb', line 90

def title
  @title
end

#totals_row_enabledObject

Returns the value of attribute totals_row_enabled.



91
92
93
# File 'lib/finmodeling/calculation_summary.rb', line 91

def totals_row_enabled
  @totals_row_enabled
end

#val_widthObject

Returns the value of attribute val_width.



91
92
93
# File 'lib/finmodeling/calculation_summary.rb', line 91

def val_width
  @val_width
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (RuntimeError)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/finmodeling/calculation_summary.rb', line 170

def +(other)
  raise RuntimeError.new("can't add a CalculationSummary to a #{other.class}") if !other.is_a?(CalculationSummary)
  multics = CalculationSummary.new
  multics.title = @title
  multics.rows = []

  if @header_row
    multics.header_row = CalculationHeader.new(
                           :key  => @header_row.key.dup, 
                           :vals => @header_row.vals.dup + other.header_row.vals.dup)
  end

  myrows  = @rows.dup
  itsrows = other.rows.dup
  while myrows.any? || itsrows.any?
    new_row = CalculationRow.new( :key  => "", :vals => [] )

    if    (myrows.any? && itsrows.empty?)
      new_row.key = myrows.first.key.dup
      new_row.vals += myrows.first.vals.dup
      new_row.vals += [""]*other.num_value_column

      myrows.shift

    elsif (myrows.empty? && itsrows.any?)
      new_row.key = itsrows.first.key.dup
      new_row.vals += [""]*num_value_columns
      new_row.vals += itsrows.first.vals.dup

      itsrows.shift

    elsif (myrows.first.key == itsrows.first.key)
      new_row.key = myrows.first.key.dup
      new_row.vals += myrows.first.vals.dup 
      new_row.vals += itsrows.first.vals.dup

      myrows.shift
      itsrows.shift

    elsif (myrows.first.key < itsrows.first.key)
      if myrow=myrows.find{|row| row.key == itsrows.first.key }
        new_row.key = myrows.first.key.dup
        new_row.vals += myrows.first.vals.dup
        new_row.vals += itsrows.first.vals.dup

        myrows.delete(myrow)
        itsrows.shift

      else
        new_row.key = itsrows.first.key.dup
        new_row.vals += [""]*num_value_columns
        new_row.vals += itsrows.first.vals.dup

        itsrows.shift
      end

    elsif (myrows.first.key > itsrows.first.key)
      if itsrow=itsrows.find{|row| row.key == myrows.first.key }
        new_row.key = myrows.first.key.dup
        new_row.vals += myrows.first.vals.dup 
        new_row.vals += itsrows.first.vals.dup

        myrows.shift
        itsrows.delete(itsrow)

      else
        new_row.key = myrows.first.key.dup
        new_row.vals += myrows.first.vals.dup
        new_row.vals += [""]*other.num_value_columns

        myrows.shift
      end

    end

    multics.rows << new_row
  end

  return multics
end

#auto_scale!Object



105
106
107
108
109
110
111
112
# File 'lib/finmodeling/calculation_summary.rb', line 105

def auto_scale!
  min_val = @rows.map{ |row| row.min_abs_val }.min
  if min_val >= 1000 && min_val < 100000
    @rows.each { |row| row.scale_down_by(:thousand) }
  elsif min_val >= 1000000
    @rows.each { |row| row.scale_down_by(:million) }
  end
end

#filter_by_type(type) ⇒ Object



140
141
142
143
144
145
# File 'lib/finmodeling/calculation_summary.rb', line 140

def filter_by_type(type)
  cs = CalculationSummary.new
  cs.title = @title
  cs.rows = @rows.select{ |x| x.type == type }
  return cs
end

#insert_column_before(col_idx, val = nil) ⇒ Object



114
115
116
117
# File 'lib/finmodeling/calculation_summary.rb', line 114

def insert_column_before(col_idx, val=nil)
  @header_row.insert_column_before(col_idx, val) if @header_row
  @rows.each{ |row| row.insert_column_before(col_idx, val) }
end

#num_value_columnsObject



101
102
103
# File 'lib/finmodeling/calculation_summary.rb', line 101

def num_value_columns
  @rows.map{ |row| row.num_vals }.max
end


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/finmodeling/calculation_summary.rb', line 128

def print
  puts @title

  rows = []
  rows << @header_row if @header_row
  rows += @rows
  rows << CalculationRow.new(:key => "Total", :vals => totals) if @totals_row_enabled
  rows.each { |row| row.print(@key_width, @max_decimals, @val_width) }

  puts
end

#total(col_idx = 0) ⇒ Object



119
120
121
# File 'lib/finmodeling/calculation_summary.rb', line 119

def total(col_idx=0)
  @rows.map{ |row| row.vals[col_idx] }.inject(:+) || 0.0
end

#totalsObject



123
124
125
126
# File 'lib/finmodeling/calculation_summary.rb', line 123

def totals
  return [] if num_value_columns.nil?
  0.upto(num_value_columns-1).map { |col_idx| total(col_idx) }
end

#write_constructor(file, item_name) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/finmodeling/calculation_summary.rb', line 147

def write_constructor(file, item_name)
  file.puts "#{item_name} = FinModeling::CalculationSummary.new"
  file.puts "#{item_name}.title = \"#{@title}\""
  file.puts "#{item_name}.key_width = #{@key_width}"
  file.puts "#{item_name}.val_width = #{@val_width}"
  file.puts "#{item_name}.max_decimals = #{@max_decimals}"
  file.puts "#{item_name}.totals_row_enabled = #{@totals_row_enabled}"

  if @header_row
    header_row_item_name = item_name + "_header_row"
    @header_row.write_constructor(file, header_row_item_name)
    file.puts "#{item_name}.header_row = #{header_row_item_name}"
  end

  row_item_names = []
  @rows.each_with_index do |row, index|
    row_item_name = item_name + "_row#{index}"
    row.write_constructor(file, row_item_name)
    row_item_names << row_item_name
  end
  file.puts "#{item_name}.rows = [#{row_item_names.join(',')}]"
end