Class: DataPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/sp500_analyzer/data_point.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDataPoint

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

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/sp500_analyzer/data_point.rb', line 7

def date
  @date
end

#historical_maxObject

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

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/sp500_analyzer/data_point.rb', line 7

def id
  @id
end

#max_dateObject

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_changeObject

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

#priceObject

Returns the value of attribute price.



7
8
9
# File 'lib/sp500_analyzer/data_point.rb', line 7

def price
  @price
end

#yearly_changeObject

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

.allObject



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_maxObject



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_monthObject



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_yearObject



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