Class: YearMonth

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/gitstats/yearmonth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil) ⇒ YearMonth

Returns a new instance of YearMonth.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gitstats/yearmonth.rb', line 7

def initialize(a, b = nil)
  if a.is_a? Time
    @year = a.year
    @month = a.month
  elsif b.nil?
    @year = (a / 100).to_i
    @month = a % 100
  else
    @year = a.to_i
    @month = b.to_i
  end
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



5
6
7
# File 'lib/gitstats/yearmonth.rb', line 5

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



4
5
6
# File 'lib/gitstats/yearmonth.rb', line 4

def year
  @year
end

Instance Method Details

#<=>(b) ⇒ Object



20
21
22
# File 'lib/gitstats/yearmonth.rb', line 20

def <=>(b)
  to_i <=> b.to_i
end

#eql?(b) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/gitstats/yearmonth.rb', line 32

def eql?(b)
  to_i == b.to_i
end

#hashObject



36
37
38
# File 'lib/gitstats/yearmonth.rb', line 36

def hash
  to_i
end

#to_iObject



24
25
26
# File 'lib/gitstats/yearmonth.rb', line 24

def to_i
  @year * 12 + @month - 1
end

#to_sObject



28
29
30
# File 'lib/gitstats/yearmonth.rb', line 28

def to_s
  '%04d-%02d' % [@year, @month]
end