class BondCalculator::BaseCalculator

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

Public Instance Methods

bonds_by_type(type, bonds) click to toggle source

filter bond list by type @author Lucas Sant' Anna @param [String] type it is possible to search by any type eg:'corporate' or 'government' @param [Array<Bond>] bonds Bond list @return [Array<Bond>] bond list with the filterd type

# 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) click to toggle source

convert a csv file to a Bond list @author Lucas Sant' Anna @param [file_path] csv_path Any file with the the bond's csv header structure @return [Array<Bond>]

# 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