Class: Monthify::Month

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month) ⇒ Month

Parameters:

  • year (Integer)

    the number of the year

  • month (Integer)

    the number of the month



51
52
53
# File 'lib/monthify/month.rb', line 51

def initialize(year, month)
  @year, @month = year, month
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



9
10
11
# File 'lib/monthify/month.rb', line 9

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



9
10
11
# File 'lib/monthify/month.rb', line 9

def year
  @year
end

Class Method Details

.containing(datish) ⇒ Month

Returns the month containing the given date.

Parameters:

  • datish (Date, Time, #to_date)

Returns:

  • (Month)

    the month containing the given date



29
30
31
# File 'lib/monthify/month.rb', line 29

def self.containing(datish)
  Month.new(datish.year, datish.month)
end

.currentMonth

Returns the current month.

Returns:

  • (Month)

    the current month



12
13
14
15
# File 'lib/monthify/month.rb', line 12

def self.current
  today = Date.current
  new(today.year, today.month)
end

.dump(month) ⇒ String

Used by ActiveRecord::Base.serialize

Parameters:

Returns:

  • (String)

    the YAML for the first date of the month



44
45
46
# File 'lib/monthify/month.rb', line 44

def self.dump(month)
  YAML.dump(month.first_day)
end

.load(date_yaml) ⇒ Month

Used by ActiveRecord::Base.serialize

Parameters:

  • date_yaml (String)

    the YAML for a date in the month

Returns:



36
37
38
39
# File 'lib/monthify/month.rb', line 36

def self.load(date_yaml)
  date = YAML.load(date_yaml)
  Month.containing(date)
end

.nextMonth

Returns the month after the current month.

Returns:

  • (Month)

    the month after the current month



18
19
20
# File 'lib/monthify/month.rb', line 18

def self.next
  current.next
end

.previousMonth

Returns the month before the current month.

Returns:

  • (Month)

    the month before the current month



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

def self.previous
  current.previous
end

Instance Method Details

#+(duration) ⇒ Month

Adds the duration to the month

Parameters:

  • duration (ActiveSupport::Duration)

Returns:



106
107
108
# File 'lib/monthify/month.rb', line 106

def +(duration)
  Month.containing(first_day + duration)
end

#-(duration) ⇒ Month

Subtracts the duration from the month

Parameters:

  • duration (ActiveSupport::Duration)

Returns:



113
114
115
# File 'lib/monthify/month.rb', line 113

def -(duration)
  Month.containing(first_day - duration)
end

#contains?(datish) ⇒ Boolean

Returns true if the month contains the date, false otherwise.

Parameters:

  • datish (Date, Time, #to_date)

Returns:

  • (Boolean)

    true if the month contains the date, false otherwise



98
99
100
101
# File 'lib/monthify/month.rb', line 98

def contains?(datish)
  date = datish.to_date
  year == date.year && month == date.month
end

#date_rangeRange<Date, Date>

Returns the range of dates in this month.

Returns:

  • (Range<Date, Date>)

    the range of dates in this month



87
88
89
# File 'lib/monthify/month.rb', line 87

def date_range
  Range.new(first_day, last_day)
end

#first_dayDate

Returns the first day of the month.

Returns:

  • (Date)

    the first day of the month



56
57
58
# File 'lib/monthify/month.rb', line 56

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

#first_momentTime

Returns the very beginning of the month.

Returns:

  • (Time)

    the very beginning of the month



66
67
68
# File 'lib/monthify/month.rb', line 66

def first_moment
  first_day.beginning_of_day
end

#last_dayDate

Returns the last day of the month.

Returns:

  • (Date)

    the last day of the month



61
62
63
# File 'lib/monthify/month.rb', line 61

def last_day
  first_day.end_of_month
end

#last_momentTime

Returns the very end of the month.

Returns:

  • (Time)

    the very end of the month



71
72
73
# File 'lib/monthify/month.rb', line 71

def last_moment
  last_day.end_of_day
end

#nextMonth Also known as: succ

Returns the month following this one.

Returns:

  • (Month)

    the month following this one



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

def next
  self + 1.month
end

#previousMonth

Returns the month preceeding this one.

Returns:

  • (Month)

    the month preceeding this one



76
77
78
# File 'lib/monthify/month.rb', line 76

def previous
  self - 1.month
end

#time_rangeRange<Time, Time>

Returns the range of time in this month.

Returns:

  • (Range<Time, Time>)

    the range of time in this month



92
93
94
# File 'lib/monthify/month.rb', line 92

def time_range
  Range.new(first_moment, last_moment)
end