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
- #beginning_of_month ⇒ Object
- #cover?(t) ⇒ Boolean
- #date_for_day(number) ⇒ Object
- #each_date(&block) ⇒ Object
- #end_of_month ⇒ 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 ⇒ 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
126 127 128 129 130 131 132 |
# File 'lib/coaster/core_ext/month.rb', line 126 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
118 119 120 121 122 123 124 |
# File 'lib/coaster/core_ext/month.rb', line 118 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
139 140 141 |
# File 'lib/coaster/core_ext/month.rb', line 139 def <=>(other) first_date <=> Month.from(other).first_date end |
#beginning_of_month ⇒ Object
85 86 87 |
# File 'lib/coaster/core_ext/month.rb', line 85 def beginning_of_month first_date.in_time_zone(timezone) end |
#cover?(t) ⇒ Boolean
134 135 136 |
# File 'lib/coaster/core_ext/month.rb', line 134 def cover?(t) to_time_range.cover?(t) end |
#date_for_day(number) ⇒ Object
93 94 95 |
# File 'lib/coaster/core_ext/month.rb', line 93 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
89 90 91 |
# File 'lib/coaster/core_ext/month.rb', line 89 def end_of_month last_date.in_time_zone(timezone).end_of_day end |
#eql?(other) ⇒ Boolean
152 153 154 |
# File 'lib/coaster/core_ext/month.rb', line 152 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
148 149 150 |
# File 'lib/coaster/core_ext/month.rb', line 148 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
109 110 111 |
# File 'lib/coaster/core_ext/month.rb', line 109 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
105 106 107 |
# File 'lib/coaster/core_ext/month.rb', line 105 def previous self.class.from(first_date - 1) end |
#succ ⇒ Object
Range implement
144 145 146 |
# File 'lib/coaster/core_ext/month.rb', line 144 def succ later end |
#to_date_range ⇒ Object
101 102 103 |
# File 'lib/coaster/core_ext/month.rb', line 101 def to_date_range first_date..last_date end |
#to_s ⇒ Object Also known as: inspect
113 114 115 |
# File 'lib/coaster/core_ext/month.rb', line 113 def to_s first_date.strftime('%Y-%m') end |
#to_time_range ⇒ Object
97 98 99 |
# File 'lib/coaster/core_ext/month.rb', line 97 def to_time_range beginning_of_month...(later.beginning_of_month) end |
#year ⇒ Object
57 58 59 |
# File 'lib/coaster/core_ext/month.rb', line 57 def year Integer(@_year) end |