Class: StockMarketDays::Calculator
- Inherits:
-
Object
- Object
- StockMarketDays::Calculator
- Includes:
- UtilityMethods
- Defined in:
- lib/stock_market_days/calculator.rb
Instance Attribute Summary collapse
-
#market_days_list ⇒ Object
readonly
Returns the value of attribute market_days_list.
Instance Method Summary collapse
-
#initialize(market_days_file) ⇒ Calculator
constructor
A new instance of Calculator.
- #is_market_day?(date = Date.today) ⇒ Boolean
-
#market_days_between(begin_date, end_date) ⇒ Object
gets number of market days between begin_day (excluding) and end_day (including).
- #market_days_from(begin_day, days) ⇒ Object
Methods included from UtilityMethods
#compare_time, #time_strip_date, #time_strip_time_zone
Constructor Details
#initialize(market_days_file) ⇒ Calculator
Returns a new instance of Calculator.
11 12 13 14 |
# File 'lib/stock_market_days/calculator.rb', line 11 def initialize(market_days_file) file_contents = File.open(market_days_file).read @market_days_list = file_contents.split("\n").map { |date_s| Date.strptime(date_s, '%Y-%m-%d') } end |
Instance Attribute Details
#market_days_list ⇒ Object (readonly)
Returns the value of attribute market_days_list.
9 10 11 |
# File 'lib/stock_market_days/calculator.rb', line 9 def market_days_list @market_days_list end |
Instance Method Details
#is_market_day?(date = Date.today) ⇒ Boolean
16 17 18 |
# File 'lib/stock_market_days/calculator.rb', line 16 def is_market_day?(date=Date.today) market_days_list.include?(date) end |
#market_days_between(begin_date, end_date) ⇒ Object
gets number of market days between begin_day (excluding) and end_day (including)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/stock_market_days/calculator.rb', line 21 def market_days_between(begin_date, end_date) unless (begin_date < end_date) && (end_date <= market_days_list.max) raise "Please enter a begin date before the end date, prior to #{market_days_list.max}" end days_between=0 market_days_list.any? do |date| if date > begin_date && date <= end_date days_between += 1 end return days_between if date > end_date end days_between end |
#market_days_from(begin_day, days) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/stock_market_days/calculator.rb', line 36 def market_days_from(begin_day, days) begin_index = market_days_list.index( market_days_list.find { |md| md >= begin_day } ) if market_days_list[begin_index] == begin_day market_days_list[begin_index + days] elsif market_days_list[begin_index] > begin_day if days == 0 market_days_list[begin_index - 1] else offset = days > 0 ? -1 : 0 market_days_list[begin_index + offset + days] end else raise "Calculator Error - This shouldn't happen in StockMarketDays#market_days_from" end end |