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



49
50
51
# File 'lib/monthify/month.rb', line 49

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

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



7
8
9
# File 'lib/monthify/month.rb', line 7

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



7
8
9
# File 'lib/monthify/month.rb', line 7

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



27
28
29
# File 'lib/monthify/month.rb', line 27

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

.currentMonth

Returns the current month.

Returns:

  • (Month)

    the current month



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

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



42
43
44
# File 'lib/monthify/month.rb', line 42

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:



34
35
36
37
# File 'lib/monthify/month.rb', line 34

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



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

def self.next
  current.next
end

.previousMonth

Returns the month before the current month.

Returns:

  • (Month)

    the month before the current month



21
22
23
# File 'lib/monthify/month.rb', line 21

def self.previous
  current.previous
end

Instance Method Details

#+(duration) ⇒ Month

Adds the duration to the month

Parameters:

  • duration (ActiveSupport::Duration)

Returns:



104
105
106
# File 'lib/monthify/month.rb', line 104

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

#-(duration) ⇒ Month

Subtracts the duration from the month

Parameters:

  • duration (ActiveSupport::Duration)

Returns:



111
112
113
# File 'lib/monthify/month.rb', line 111

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



96
97
98
99
# File 'lib/monthify/month.rb', line 96

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



85
86
87
# File 'lib/monthify/month.rb', line 85

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



54
55
56
# File 'lib/monthify/month.rb', line 54

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



64
65
66
# File 'lib/monthify/month.rb', line 64

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



59
60
61
# File 'lib/monthify/month.rb', line 59

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



69
70
71
# File 'lib/monthify/month.rb', line 69

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



79
80
81
# File 'lib/monthify/month.rb', line 79

def next
  self + 1.month
end

#previousMonth

Returns the month preceeding this one.

Returns:

  • (Month)

    the month preceeding this one



74
75
76
# File 'lib/monthify/month.rb', line 74

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



90
91
92
# File 'lib/monthify/month.rb', line 90

def time_range
  Range.new(first_moment, last_moment)
end