Class: Month
- Includes:
- Comparable
- Defined in:
- lib/coaster/core_ext/month.rb
Instance Attribute Summary collapse
-
#_month ⇒ Object
readonly
Returns the value of attribute _month.
-
#_year ⇒ Object
readonly
Returns the value of attribute _year.
-
#timezone ⇒ Object
Returns the value of attribute timezone.
Class Method Summary collapse
- .current ⇒ Object
- .from(object, timezone: nil) ⇒ Object
- .now ⇒ Object
-
.parse(str, timezone: nil) ⇒ Object
Month.parse(‘201601’) Month.parse(‘2016-01’).
Instance Method Summary collapse
- #+(time) ⇒ Object
- #-(time) ⇒ Object
- #<=>(other) ⇒ Object
- #_timezone(timezone) ⇒ Object
- #beginning_of_month(timezone = nil) ⇒ Object
- #beginning_of_range(timezone = nil) ⇒ Object
- #cover?(t) ⇒ Boolean
- #date_for_day(number) ⇒ Object
- #each_date(&block) ⇒ Object
- #end_of_month ⇒ Object
- #end_of_range(timezone = nil) ⇒ Object
- #eql?(other) ⇒ Boolean
- #first_date ⇒ Object
- #first_day ⇒ Object
- #hash ⇒ Object
-
#initialize(year, month, timezone: nil) ⇒ Month
constructor
A new instance of Month.
- #last_date ⇒ Object
- #last_day ⇒ Object
- #later ⇒ Object
- #month ⇒ Object
- #previous ⇒ Object
-
#succ ⇒ Object
Range implement.
- #to_date_range ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #to_time_range(timezone = nil) ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(year, month, timezone: nil) ⇒ Month
Returns a new instance of Month.
46 47 48 49 50 |
# File 'lib/coaster/core_ext/month.rb', line 46 def initialize(year, month, timezone: nil) @_year = year @_month = month self.timezone = timezone end |
Instance Attribute Details
#_month ⇒ Object (readonly)
Returns the value of attribute _month.
4 5 6 |
# File 'lib/coaster/core_ext/month.rb', line 4 def _month @_month end |
#_year ⇒ Object (readonly)
Returns the value of attribute _year.
4 5 6 |
# File 'lib/coaster/core_ext/month.rb', line 4 def _year @_year end |
#timezone ⇒ Object
Returns the value of attribute timezone.
5 6 7 |
# File 'lib/coaster/core_ext/month.rb', line 5 def timezone @timezone end |
Class Method Details
.current ⇒ Object
37 38 39 |
# File 'lib/coaster/core_ext/month.rb', line 37 def current from(Date.current) end |
.from(object, timezone: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/coaster/core_ext/month.rb', line 8 def from(object, timezone: nil) case object when Month object.timezone = timezone object when String then Month.parse(object, timezone: timezone) when Array then Month.new(object[0], object[1], timezone: timezone) else new(object.year, object.month, timezone: timezone) end end |
.now ⇒ Object
41 42 43 |
# File 'lib/coaster/core_ext/month.rb', line 41 def now from(Time.zone.now) end |
.parse(str, timezone: nil) ⇒ Object
Month.parse(‘201601’) Month.parse(‘2016-01’)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/coaster/core_ext/month.rb', line 21 def parse(str, timezone: nil) date = Date.parse(str) from(date, timezone: timezone) rescue ArgumentError => e if str.instance_variable_defined?(:@_gsub_) && 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, timezone: timezone) end |
Instance Method Details
#+(time) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/coaster/core_ext/month.rb', line 143 def +(time) case time when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) + time) else Month.from(first_date + time) end end |
#-(time) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/coaster/core_ext/month.rb', line 135 def -(time) case time when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) - time) else Month.from(first_date - time) end end |
#<=>(other) ⇒ Object
156 157 158 |
# File 'lib/coaster/core_ext/month.rb', line 156 def <=>(other) first_date <=> Month.from(other).first_date end |
#_timezone(timezone) ⇒ Object
85 86 87 88 89 |
# File 'lib/coaster/core_ext/month.rb', line 85 def _timezone(timezone) tz = timezone || self.timezone tz = ActiveSupport::TimeZone[tz] if tz.is_a?(String) tz end |
#beginning_of_month(timezone = nil) ⇒ Object
91 92 93 94 |
# File 'lib/coaster/core_ext/month.rb', line 91 def beginning_of_month(timezone = nil) tz = _timezone(timezone) first_date.in_time_zone(tz) end |
#beginning_of_range(timezone = nil) ⇒ Object
110 111 112 |
# File 'lib/coaster/core_ext/month.rb', line 110 def beginning_of_range(timezone = nil) to_time_range(timezone).begin end |
#cover?(t) ⇒ Boolean
151 152 153 |
# File 'lib/coaster/core_ext/month.rb', line 151 def cover?(t) to_time_range.cover?(t) end |
#date_for_day(number) ⇒ Object
101 102 103 |
# File 'lib/coaster/core_ext/month.rb', line 101 def date_for_day(number) Date.new(year, month, number) end |
#each_date(&block) ⇒ Object
73 74 75 |
# File 'lib/coaster/core_ext/month.rb', line 73 def each_date(&block) (first_date..last_date).each(&block) end |
#end_of_month ⇒ Object
96 97 98 99 |
# File 'lib/coaster/core_ext/month.rb', line 96 def end_of_month tz = _timezone(timezone) last_date.in_time_zone(tz).end_of_day end |
#end_of_range(timezone = nil) ⇒ Object
114 115 116 |
# File 'lib/coaster/core_ext/month.rb', line 114 def end_of_range(timezone = nil) to_time_range(timezone).end end |
#eql?(other) ⇒ Boolean
169 170 171 |
# File 'lib/coaster/core_ext/month.rb', line 169 def eql?(other) other.is_a?(Month) && first_date == other.first_date end |
#first_date ⇒ Object
65 66 67 |
# File 'lib/coaster/core_ext/month.rb', line 65 def first_date @first_date ||= Date.new(year, month, 1) end |
#first_day ⇒ Object
77 78 79 |
# File 'lib/coaster/core_ext/month.rb', line 77 def first_day first_date.day end |
#hash ⇒ Object
165 166 167 |
# File 'lib/coaster/core_ext/month.rb', line 165 def hash first_date.hash end |
#last_date ⇒ Object
69 70 71 |
# File 'lib/coaster/core_ext/month.rb', line 69 def last_date @last_date ||= Date.new(year, month, -1) end |
#last_day ⇒ Object
81 82 83 |
# File 'lib/coaster/core_ext/month.rb', line 81 def last_day last_date.day end |
#later ⇒ Object
126 127 128 |
# File 'lib/coaster/core_ext/month.rb', line 126 def later self.class.from(last_date + 1) end |
#month ⇒ Object
61 62 63 |
# File 'lib/coaster/core_ext/month.rb', line 61 def month Integer(@_month) end |
#previous ⇒ Object
122 123 124 |
# File 'lib/coaster/core_ext/month.rb', line 122 def previous self.class.from(first_date - 1) end |
#succ ⇒ Object
Range implement
161 162 163 |
# File 'lib/coaster/core_ext/month.rb', line 161 def succ later end |
#to_date_range ⇒ Object
118 119 120 |
# File 'lib/coaster/core_ext/month.rb', line 118 def to_date_range first_date..last_date end |
#to_s ⇒ Object Also known as: inspect
130 131 132 |
# File 'lib/coaster/core_ext/month.rb', line 130 def to_s first_date.strftime('%Y-%m') end |
#to_time_range(timezone = nil) ⇒ Object
105 106 107 108 |
# File 'lib/coaster/core_ext/month.rb', line 105 def to_time_range(timezone = nil) tz = _timezone(timezone) beginning_of_month(tz)...(later.beginning_of_month(tz)) end |
#year ⇒ Object
57 58 59 |
# File 'lib/coaster/core_ext/month.rb', line 57 def year Integer(@_year) end |