Class: Month
- Inherits:
-
Object
- Object
- Month
- Includes:
- Comparable
- Defined in:
- lib/month.rb,
lib/month.rb,
lib/month.rb,
lib/month/yaml.rb
Defined Under Namespace
Modules: Methods
Constant Summary collapse
- NAMES =
{ 1 => :January, 2 => :February, 3 => :March, 4 => :April, 5 => :May, 6 => :June, 7 => :July, 8 => :August, 9 => :September, 10 => :October, 11 => :November, 12 => :December }
Instance Attribute Summary collapse
-
#number ⇒ Object
(also: #month)
readonly
Returns the value of attribute number.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #+(number) ⇒ Object
- #-(object) ⇒ Object
- #<<(n) ⇒ Object
- #<=>(month) ⇒ Object
- #>>(n) ⇒ Object
- #dates ⇒ Object
- #downto(min, &block) ⇒ Object
- #encode_with(coder) ⇒ Object
- #end_date ⇒ Object
- #eql?(object) ⇒ Boolean
- #hash ⇒ Object
- #include?(time) ⇒ Boolean (also: #===)
-
#initialize(year, number) ⇒ Month
constructor
A new instance of Month.
- #inspect ⇒ Object
- #length ⇒ Object
- #name ⇒ Object
- #next ⇒ Object (also: #succ, #next_month)
- #prev_month ⇒ Object
- #start_date ⇒ Object
- #step(limit, step = 1) ⇒ Object
- #to_s ⇒ Object (also: #iso8601)
- #upto(max, &block) ⇒ Object
Constructor Details
Instance Attribute Details
#number ⇒ Object (readonly) Also known as: month
Returns the value of attribute number.
30 31 32 |
# File 'lib/month.rb', line 30 def number @number end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
30 31 32 |
# File 'lib/month.rb', line 30 def year @year end |
Class Method Details
.parse(string) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/month.rb', line 192 def self.parse(string) case string when REGEXP1 Month.new($1.to_i, $2.to_i) when REGEXP2 Month.new($1.to_i, ABBREVIATIONS.fetch($2)) when REGEXP3 Month.new($2.to_i, Date::MONTHNAMES.index($1)) else raise ArgumentError, 'invalid month' end end |
Instance Method Details
#+(number) ⇒ Object
130 131 132 133 134 |
# File 'lib/month.rb', line 130 def +(number) a, b = (@number - 1 + number).divmod(12) self.class.new(@year + a, b + 1) end |
#-(object) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/month.rb', line 136 def -(object) if object.is_a?(Integer) self + (-object) else 12 * (year - object.year) + (@number - object.number) end end |
#<<(n) ⇒ Object
94 95 96 |
# File 'lib/month.rb', line 94 def <<(n) self - n end |
#<=>(month) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/month.rb', line 62 def <=>(month) return unless month.class == self.class if @year == month.year @number <=> month.number else @year <=> month.year end end |
#>>(n) ⇒ Object
90 91 92 |
# File 'lib/month.rb', line 90 def >>(n) self + n end |
#dates ⇒ Object
158 159 160 |
# File 'lib/month.rb', line 158 def dates start_date .. end_date end |
#downto(min, &block) ⇒ Object
126 127 128 |
# File 'lib/month.rb', line 126 def downto(min, &block) step(min, -1, &block) end |
#encode_with(coder) ⇒ Object
4 5 6 |
# File 'lib/month/yaml.rb', line 4 def encode_with(coder) coder.represent_scalar(nil, to_s) end |
#end_date ⇒ Object
154 155 156 |
# File 'lib/month.rb', line 154 def end_date Date.new(@year, @number, -1) end |
#eql?(object) ⇒ Boolean
58 59 60 |
# File 'lib/month.rb', line 58 def eql?(object) object.class == self.class && object.hash == self.hash end |
#hash ⇒ Object
54 55 56 |
# File 'lib/month.rb', line 54 def hash [@year, @number].hash end |
#include?(time) ⇒ Boolean Also known as: ===
144 145 146 |
# File 'lib/month.rb', line 144 def include?(time) @year == time.year && @number == time.month end |
#inspect ⇒ Object
40 41 42 |
# File 'lib/month.rb', line 40 def inspect "<Month: #{to_s}>" end |
#length ⇒ Object
162 163 164 |
# File 'lib/month.rb', line 162 def length end_date.mday end |
#name ⇒ Object
44 45 46 |
# File 'lib/month.rb', line 44 def name NAMES.fetch(@number) end |
#next ⇒ Object Also known as: succ, next_month
74 75 76 77 78 79 80 |
# File 'lib/month.rb', line 74 def next if @number == 12 self.class.new(@year + 1, 1) else self.class.new(@year, @number + 1) end end |
#prev_month ⇒ Object
86 87 88 |
# File 'lib/month.rb', line 86 def prev_month self - 1 end |
#start_date ⇒ Object
150 151 152 |
# File 'lib/month.rb', line 150 def start_date Date.new(@year, @number, 1) end |
#step(limit, step = 1) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/month.rb', line 98 def step(limit, step = 1) raise ArgumentError if step.zero? unless block_given? return enum_for(:step, limit, step) end month = self if step > 0 until month > limit yield month month += step end else until month < limit yield month month += step end end end |
#to_s ⇒ Object Also known as: iso8601
34 35 36 |
# File 'lib/month.rb', line 34 def to_s "#{@year}-#{@number.to_s.rjust(2, '0')}" end |
#upto(max, &block) ⇒ Object
122 123 124 |
# File 'lib/month.rb', line 122 def upto(max, &block) step(max, 1, &block) end |