Class: FinModeling::CalculationRow

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

Direct Known Subclasses

CalculationHeader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CalculationRow

Returns a new instance of CalculationRow.



6
7
8
9
10
# File 'lib/finmodeling/calculation_summary.rb', line 6

def initialize(args = {})
  @key  = args[:key] || ""
  @type = args[:type]
  @vals = args[:vals] || []
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/finmodeling/calculation_summary.rb', line 4

def key
  @key
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/finmodeling/calculation_summary.rb', line 4

def type
  @type
end

#valsObject

Returns the value of attribute vals.



4
5
6
# File 'lib/finmodeling/calculation_summary.rb', line 4

def vals
  @vals
end

Instance Method Details

#insert_column_before(col_idx, val) ⇒ Object



38
39
40
# File 'lib/finmodeling/calculation_summary.rb', line 38

def insert_column_before(col_idx, val)
  @vals.insert(col_idx, val)
end

#min_abs_valObject



22
23
24
# File 'lib/finmodeling/calculation_summary.rb', line 22

def min_abs_val
  @vals.map{ |x| x.abs }.min
end

#num_valsObject



18
19
20
# File 'lib/finmodeling/calculation_summary.rb', line 18

def num_vals
  @vals.length
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/finmodeling/calculation_summary.rb', line 42

def print(key_width=18, max_decimals=4, val_width=12)
  type_and_key = ""
  type_and_key += "[#{@type}] " if @type
  type_and_key += @key
  key_lines = type_and_key.split_into_lines_shorter_than(key_width).map{ |line| line.fixed_width_left_justify(key_width) }
  
  justified_vals = ""
  @vals.each do |val|
    val_with_commas = val.to_s.with_thousands_separators
    justified_vals += "  " + val_with_commas.cap_decimals(max_decimals).fixed_width_right_justify(val_width) 
  end
   
  puts "\t" + key_lines.shift + justified_vals
  key_lines.each do |line|
    puts "\t " + line
  end
end

#scale_down_by(val) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/finmodeling/calculation_summary.rb', line 26

def scale_down_by(val)
  if val == :thousand
    @vals.map!{ |val| val / 1000.0 }
    @key += " ($KK)"
  elsif val == :million
    @vals.map!{ |val| val / 1000000.0 }
    @key += " ($MM)"
  else
    raise RuntimeError.new("Bogus val: #{val}")
  end
end

#valid_valsObject



12
13
14
15
16
# File 'lib/finmodeling/calculation_summary.rb', line 12

def valid_vals
  ArrayWithStats.new(@vals.select{ |val| !val.nil? &&
                                         !val.is_a?(Complex) &&
                                         (!val.is_a?(Float) || (!val.nan? && val.finite?))})
end

#write_constructor(file, item_name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/finmodeling/calculation_summary.rb', line 60

def write_constructor(file, item_name)
  file.puts "args = { }"
  file.puts "args[:key] = \"#{@key}\""
  file.puts "args[:type] = \"#{@type}\""
  file.puts "args[:vals] = [#{@vals.join(', ')}]"
  file.puts "#{item_name} = FinModeling::CalculationRow.new(args)"
end