Class: BondCalculator::BaseCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/bond_calculator/base_calculator.rb

Overview

Abstract base class for Bond Calculation. Provides some helper methods for convert the csv file to a bond map and a filter for the bonds collection

Author:

  • Lucas Sant’ Anna

Since:

  • 0.0.1

Direct Known Subclasses

SpreadToBenchmark, SpreadToCurve

Instance Method Summary collapse

Instance Method Details

#bonds_by_type(type, bonds) ⇒ Array<Bond>

filter bond list by type

Parameters:

  • type (String)

    it is possible to search by any type eg:‘corporate’ or ‘government’

  • bonds (Array<Bond>)

    Bond list

Returns:

  • (Array<Bond>)

    bond list with the filterd type

Author:

  • Lucas Sant’ Anna

Since:

  • 0.0.1



30
31
32
33
34
35
# File 'lib/bond_calculator/base_calculator.rb', line 30

def bonds_by_type(type, bonds)
  return nil if type.nil? || bonds.nil?

  bonds.select { |bond| bond.type == type }
    .sort_by { |bond| bond.term_years }
end

#csv_to_bonds(csv_path) ⇒ Array<Bond>

convert a csv file to a Bond list

Parameters:

  • csv_path (file_path)

    Any file with the the bond’s csv header structure

Returns:

Author:

  • Lucas Sant’ Anna

Since:

  • 0.0.1



18
19
20
21
22
23
# File 'lib/bond_calculator/base_calculator.rb', line 18

def csv_to_bonds(csv_path)
  csv = File.open(csv_path, 'r')
  bonds_csv = CSV.parse(csv, headers: true).map(&:to_h)
  csv.close
  bonds_csv.map { |line| Bond.new(line) }
end