Class: Month
- Includes:
- Comparable
- Defined in:
- lib/coaster/core_ext/month.rb
Class Method Summary collapse
- .current ⇒ Object
- .from(object) ⇒ Object
- .now ⇒ Object
-
.parse(str) ⇒ Object
Month.parse(‘201601’) Month.parse(‘2016-01’).
Instance Method Summary collapse
- #+(time) ⇒ Object
- #-(time) ⇒ Object
- #<=>(other) ⇒ Object
- #beginning_of_month ⇒ Object
- #date_for_day(number) ⇒ Object
- #each_date(&block) ⇒ Object
- #end_of_month ⇒ Object
- #first_date ⇒ Object
- #first_day ⇒ Object
- #last_date ⇒ Object
- #last_day ⇒ Object
- #later ⇒ Object
- #month ⇒ Object
- #previous ⇒ Object
-
#succ ⇒ Object
Range implement.
- #to_s ⇒ Object (also: #inspect)
- #to_time_range ⇒ Object
- #year ⇒ Object
Class Method Details
.current ⇒ Object
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 |
.now ⇒ Object
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. != '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_month ⇒ Object
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_month ⇒ Object
75 76 77 |
# File 'lib/coaster/core_ext/month.rb', line 75 def end_of_month last_date.end_of_day end |
#first_date ⇒ Object
51 52 53 |
# File 'lib/coaster/core_ext/month.rb', line 51 def first_date Date.new(year, month, 1) end |
#first_day ⇒ Object
63 64 65 |
# File 'lib/coaster/core_ext/month.rb', line 63 def first_day first_date.day end |
#last_date ⇒ Object
55 56 57 |
# File 'lib/coaster/core_ext/month.rb', line 55 def last_date Date.new(year, month, -1) end |
#last_day ⇒ Object
67 68 69 |
# File 'lib/coaster/core_ext/month.rb', line 67 def last_day last_date.day end |
#later ⇒ Object
91 92 93 |
# File 'lib/coaster/core_ext/month.rb', line 91 def later self.class.from(last_date + 1) end |
#month ⇒ Object
47 48 49 |
# File 'lib/coaster/core_ext/month.rb', line 47 def month Integer(@month) end |
#previous ⇒ Object
87 88 89 |
# File 'lib/coaster/core_ext/month.rb', line 87 def previous self.class.from(first_date - 1) end |
#succ ⇒ Object
Range implement
114 115 116 |
# File 'lib/coaster/core_ext/month.rb', line 114 def succ later end |
#to_s ⇒ Object Also known as: inspect
95 96 97 |
# File 'lib/coaster/core_ext/month.rb', line 95 def to_s "#{year}-#{month}" end |
#to_time_range ⇒ Object
83 84 85 |
# File 'lib/coaster/core_ext/month.rb', line 83 def to_time_range beginning_of_month...(later.beginning_of_month) end |
#year ⇒ Object
43 44 45 |
# File 'lib/coaster/core_ext/month.rb', line 43 def year Integer(@year) end |