Class: DataPoint
- Inherits:
-
Object
- Object
- DataPoint
- Defined in:
- lib/sp500_analyzer/data_point.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#historical_max ⇒ Object
Returns the value of attribute historical_max.
-
#id ⇒ Object
Returns the value of attribute id.
-
#max_date ⇒ Object
Returns the value of attribute max_date.
-
#monthly_change ⇒ Object
Returns the value of attribute monthly_change.
-
#price ⇒ Object
Returns the value of attribute price.
-
#yearly_change ⇒ Object
Returns the value of attribute yearly_change.
Class Method Summary collapse
- .all ⇒ Object
- .find_by_date(date) ⇒ Object
- .find_by_year(year) ⇒ Object
- .set_historical_max ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ DataPoint
constructor
A new instance of DataPoint.
- #previous_month ⇒ Object
- #previous_year ⇒ Object
Constructor Details
#initialize ⇒ DataPoint
Returns a new instance of DataPoint.
9 10 11 |
# File 'lib/sp500_analyzer/data_point.rb', line 9 def initialize @@all << self end |
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def date @date end |
#historical_max ⇒ Object
Returns the value of attribute historical_max.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def historical_max @historical_max end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def id @id end |
#max_date ⇒ Object
Returns the value of attribute max_date.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def max_date @max_date end |
#monthly_change ⇒ Object
Returns the value of attribute monthly_change.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def monthly_change @monthly_change end |
#price ⇒ Object
Returns the value of attribute price.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def price @price end |
#yearly_change ⇒ Object
Returns the value of attribute yearly_change.
7 8 9 |
# File 'lib/sp500_analyzer/data_point.rb', line 7 def yearly_change @yearly_change end |
Class Method Details
.all ⇒ Object
13 14 15 |
# File 'lib/sp500_analyzer/data_point.rb', line 13 def self.all @@all end |
.find_by_date(date) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/sp500_analyzer/data_point.rb', line 58 def self.find_by_date(date) date_array = date.split("/") all.detect do |datapoint| datapoint.date.month == date_array[0].to_i && datapoint.date.year == date_array[1].to_i end end |
.find_by_year(year) ⇒ Object
52 53 54 55 56 |
# File 'lib/sp500_analyzer/data_point.rb', line 52 def self.find_by_year(year) all.select do |datapoint| datapoint.date.year == year end end |
.set_historical_max ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sp500_analyzer/data_point.rb', line 37 def self.set_historical_max all.each_with_index do |datapoint, index| loop_index = 0 maximum = datapoint until loop_index >= index if all[loop_index].price >= maximum.price maximum = all[loop_index] end loop_index += 1 end datapoint.historical_max = maximum end nil end |
Instance Method Details
#previous_month ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sp500_analyzer/data_point.rb', line 17 def previous_month year = date.year month = date.month day = date.day if month == 1 prev_month = 12 else prev_month = month-1 end Date.new(year, prev_month, day) end |
#previous_year ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/sp500_analyzer/data_point.rb', line 29 def previous_year year = date.year month = date.month day = date.day prev_year = year-1 Date.new(year-1, month, day) end |