Class: BondCalculator::BaseCalculator
- Inherits:
-
Object
- Object
- BondCalculator::BaseCalculator
- 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
Direct Known Subclasses
Instance Method Summary collapse
-
#bonds_by_type(type, bonds) ⇒ Array<Bond>
filter bond list by type.
-
#csv_to_bonds(csv_path) ⇒ Array<Bond>
convert a csv file to a Bond list.
Instance Method Details
#bonds_by_type(type, bonds) ⇒ Array<Bond>
filter bond list by type
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
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 |