Class: Month

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject



34
35
36
# File 'lib/coaster/core_ext/month.rb', line 34

def current
  from(Date.current)
end

.from(object) ⇒ Object



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

def from(object)
  case object
    when Month then object
    when String then Month.parse(object)
    when Array then Month.new(object[0], object[1])
    else new(object.year, object.month)
  end
end

.nowObject



38
39
40
# File 'lib/coaster/core_ext/month.rb', line 38

def now
  from(Time.zone.now)
end

.parse(str) ⇒ Object

Month.parse(‘201601’) Month.parse(‘2016-01’)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/coaster/core_ext/month.rb', line 18

def parse(str)
  date = Date.parse(str)
  from(date)
rescue ArgumentError => e
  if str.instance_variable_get(:@_gsub_)
    raise e, str: str.instance_variable_get(:@_gsub_)
  elsif e.message != 'invalid date'
    raise e, str: str
  end
  str_gsub = str.gsub(/[^\d]/, '')
  str_gsub.insert(4, '0') if str_gsub.length == 5
  str_gsub += '01'
  str_gsub.instance_variable_set(:@_gsub_, str_gsub)
  parse(str_gsub)
end

Instance Method Details

#+(time) ⇒ Object



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

def +(time)
  first_date + time
end

#-(time) ⇒ Object



100
101
102
# File 'lib/coaster/core_ext/month.rb', line 100

def -(time)
  first_date - time
end

#<=>(other) ⇒ Object



109
110
111
# File 'lib/coaster/core_ext/month.rb', line 109

def <=>(other)
  first_date <=> Month.from(other).first_date
end

#beginning_of_monthObject



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

def beginning_of_month
  first_date.beginning_of_day
end

#date_for_day(number) ⇒ Object



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

def date_for_day(number)
  Date.new(year, month, number)
end

#each_date(&block) ⇒ Object



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

def each_date(&block)
   (first_date..last_date).each(&block)
end

#end_of_monthObject



75
76
77
# File 'lib/coaster/core_ext/month.rb', line 75

def end_of_month
  last_date.end_of_day
end

#first_dateObject



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

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

#first_dayObject



63
64
65
# File 'lib/coaster/core_ext/month.rb', line 63

def first_day
  first_date.day
end

#last_dateObject



55
56
57
# File 'lib/coaster/core_ext/month.rb', line 55

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

#last_dayObject



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

def last_day
  last_date.day
end

#laterObject



91
92
93
# File 'lib/coaster/core_ext/month.rb', line 91

def later
  self.class.from(last_date + 1)
end

#monthObject



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

def month
  Integer(@month)
end

#previousObject



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

def previous
  self.class.from(first_date - 1)
end

#succObject

Range implement



114
115
116
# File 'lib/coaster/core_ext/month.rb', line 114

def succ
  later
end

#to_sObject Also known as: inspect



95
96
97
# File 'lib/coaster/core_ext/month.rb', line 95

def to_s
  "#{year}-#{month}"
end

#to_time_rangeObject



83
84
85
# File 'lib/coaster/core_ext/month.rb', line 83

def to_time_range
  beginning_of_month...(later.beginning_of_month)
end

#yearObject



43
44
45
# File 'lib/coaster/core_ext/month.rb', line 43

def year
  Integer(@year)
end