Class: Fortune::EventList

Inherits:
Array
  • Object
show all
Defined in:
lib/fortune/event_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ EventList

Returns a new instance of EventList.



5
6
7
8
9
# File 'lib/fortune/event_list.rb', line 5

def initialize(h = {})
  h.each{|name, m| self.push(Event.new({:name => name, :m => m}))}
  @sum_m = 0
  @intervals = {}
end

Instance Attribute Details

#intervalsObject

Returns the value of attribute intervals.



4
5
6
# File 'lib/fortune/event_list.rb', line 4

def intervals
  @intervals
end

#nObject

Returns the value of attribute n.



4
5
6
# File 'lib/fortune/event_list.rb', line 4

def n
  @n
end

#sum_mObject

Returns the value of attribute sum_m.



4
5
6
# File 'lib/fortune/event_list.rb', line 4

def sum_m
  @sum_m
end

Instance Method Details

#activeObject



40
41
42
# File 'lib/fortune/event_list.rb', line 40

def active
  self.select{|event| event.has_come?}.first
end

#calcObject



10
11
12
13
14
15
16
# File 'lib/fortune/event_list.rb', line 10

def calc
  self.calc_sum_m
  self.calc_intervals
  self.get_n
  self.set_active
  self
end

#calc_intervalsObject



32
33
34
35
36
37
38
39
# File 'lib/fortune/event_list.rb', line 32

def calc_intervals
  x = 0
  self.map{|event|
    next if event.m <= 0
    self.intervals[event.name] = [x + 1, x + event.m]
    x = x + event.m
  }
end

#calc_sum_mObject



26
27
28
29
30
31
# File 'lib/fortune/event_list.rb', line 26

def calc_sum_m
  self.map{|event|
    next if event.m <= 0
    self.sum_m += event.m
  }
end

#get_nObject



23
24
25
# File 'lib/fortune/event_list.rb', line 23

def get_n
  @n = P.n(self.sum_m)
end

#set_activeObject



17
18
19
20
21
22
# File 'lib/fortune/event_list.rb', line 17

def set_active
  self.map{|event|
    next if event.m <= 0
    event.set_come if self.n >= intervals[event.name][0] && self.n <= intervals[event.name][1]
  }
end