Class: Bandit::DateHour

Inherits:
Object
  • Object
show all
Defined in:
lib/bandit/date_hour.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, hour) ⇒ DateHour

Returns a new instance of DateHour.



8
9
10
11
# File 'lib/bandit/date_hour.rb', line 8

def initialize(date, hour)
  @date = date
  @hour = hour
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/bandit/date_hour.rb', line 6

def date
  @date
end

#hourObject

Returns the value of attribute hour.



6
7
8
# File 'lib/bandit/date_hour.rb', line 6

def hour
  @hour
end

Class Method Details

.date_inject(date, initial = 0) ⇒ Object

given a block that is called w/ each DateHour for a day, inject over all DateHours



26
27
28
29
30
31
# File 'lib/bandit/date_hour.rb', line 26

def self.date_inject(date, initial=0)
  DateHour.new(date, 0).upto(DateHour.new(date, 23)) { |dh|
    initial = yield initial, dh
  }
  initial
end

.nowObject



13
14
15
# File 'lib/bandit/date_hour.rb', line 13

def self.now
  DateHour.new Date.today, Time.now.hour
end

Instance Method Details

#+(hours) ⇒ Object



33
34
35
# File 'lib/bandit/date_hour.rb', line 33

def +(hours)
  (to_time + (hours * 3600)).to_date_hour
end

#<(other) ⇒ Object



41
42
43
# File 'lib/bandit/date_hour.rb', line 41

def <(other)
  @date < other.date or (@date == other.date and @hour < other.hour)
end

#<=(other) ⇒ Object



53
54
55
# File 'lib/bandit/date_hour.rb', line 53

def <=(other)
  self < other or self == other
end

#==(other) ⇒ Object



37
38
39
# File 'lib/bandit/date_hour.rb', line 37

def ==(other)
  @date == other.date and @hour == other.hour
end

#>(other) ⇒ Object



45
46
47
# File 'lib/bandit/date_hour.rb', line 45

def >(other)
  @date > other.date or (@date == other.date and @hour > other.hour)
end

#>=(other) ⇒ Object



49
50
51
# File 'lib/bandit/date_hour.rb', line 49

def >=(other)
  self > other or self == other
end

#dayObject



77
78
79
# File 'lib/bandit/date_hour.rb', line 77

def day
  @date.day
end

#monthObject



73
74
75
# File 'lib/bandit/date_hour.rb', line 73

def month
  @date.month
end

#to_iObject



65
66
67
# File 'lib/bandit/date_hour.rb', line 65

def to_i
  to_time.to_i
end

#to_sObject



61
62
63
# File 'lib/bandit/date_hour.rb', line 61

def to_s
  "#{@date.to_s} #{@hour}:00:00"
end

#to_timeObject



57
58
59
# File 'lib/bandit/date_hour.rb', line 57

def to_time
  Time.mktime year, month, day, hour, 0
end

#upto(other) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/bandit/date_hour.rb', line 17

def upto(other)
  n = DateHour.new(@date, @hour)
  while n <= other
    yield n
    n += 1
  end
end

#yearObject



69
70
71
# File 'lib/bandit/date_hour.rb', line 69

def year
  @date.year
end