Class: Monthra::Month

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/monthra/month.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_year, new_month) ⇒ Month



30
31
32
# File 'lib/monthra/month.rb', line 30

def initialize(new_year, new_month)
  setup_year_and_month(new_year, new_month)
end

Class Method Details

.at(generic_month) ⇒ Month

param [Object] generic_month Must respond to month and year



7
8
9
10
11
12
13
# File 'lib/monthra/month.rb', line 7

def self.at(generic_month)
  if generic_month.respond_to?(:year) && generic_month.respond_to?(:month)
    self.new(generic_month.year, generic_month.month)
  else
    raise Exception("The param must have year and month methods")
  end
end

.currentMonth



16
17
18
# File 'lib/monthra/month.rb', line 16

def self.current
  self.at(Date.today)
end

.strpmonth(month_str, format) ⇒ Month



23
24
25
# File 'lib/monthra/month.rb', line 23

def self.strpmonth(month_str, format)
  self.at(Time.strptime(month_str, format))
end

Instance Method Details

#+(offset) ⇒ Month



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/monthra/month.rb', line 92

def +(offset)
  offsets = month_year_offset(offset)

  new_year = year + offsets[:year]
  new_month = month + offsets[:month]
  
  if new_month > 12
    new_month -= 12
    new_year += 1
  end

  self.class.new(new_year, new_month)
end

#-(offset) ⇒ Month



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/monthra/month.rb', line 108

def -(offset)
  offsets = month_year_offset(offset)

  new_year = year - offsets[:year]
  new_month = month - offsets[:month]

  if new_month < 1
    new_month += 12 # note there is no month 0
    new_year -= 1
  end
  
  self.class.new(new_year, new_month)
end

#<=>(other_month) ⇒ Fixnum



36
37
38
39
40
41
42
43
44
# File 'lib/monthra/month.rb', line 36

def <=>(other_month)
  if year == other_month.year && month == other_month.month
    return 0
  elsif year > other_month.year || (year == other_month.year && month > other_month.month)
    return 1
  else # the current month is less
    return -1
  end
end

#begin_onDate



72
73
74
# File 'lib/monthra/month.rb', line 72

def begin_on
  to_date
end

#end_onDate



77
78
79
# File 'lib/monthra/month.rb', line 77

def end_on
  Date.new(year, month, -1)
end

#monthFixnum



57
58
59
# File 'lib/monthra/month.rb', line 57

def month
  @month
end

#strfmonth(format) ⇒ Object



81
82
83
# File 'lib/monthra/month.rb', line 81

def strfmonth(format)
  to_time.strftime(format)
end

#to_dateDate



67
68
69
# File 'lib/monthra/month.rb', line 67

def to_date
  Date.new(year, month, 1)
end

#to_monthra_monthMonth



47
48
49
# File 'lib/monthra/month.rb', line 47

def to_monthra_month
  self
end

#to_sString



86
87
88
# File 'lib/monthra/month.rb', line 86

def to_s
  strfmonth("%Y-%m")
end

#to_timeTime



62
63
64
# File 'lib/monthra/month.rb', line 62

def to_time
  to_date.to_time
end

#yearFixnum



52
53
54
# File 'lib/monthra/month.rb', line 52

def year
  @year
end