Class: Month

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, number) ⇒ Month

Returns a new instance of Month.


20
21
22
23
24
25
26
27
28
# File 'lib/month.rb', line 20

def initialize(year, number)
  unless NAMES.key?(number)
    raise ArgumentError, 'invalid month number'
  end

  @year, @number = year, number

  freeze
end

Instance Attribute Details

#numberObject (readonly) Also known as: month

Returns the value of attribute number.


30
31
32
# File 'lib/month.rb', line 30

def number
  @number
end

#yearObject (readonly)

Returns the value of attribute year.


30
31
32
# File 'lib/month.rb', line 30

def year
  @year
end

Class Method Details

.nowObject


217
218
219
# File 'lib/month.rb', line 217

def Month.now
  Month(Time.now)
end

.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

#datesObject


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_dateObject


154
155
156
# File 'lib/month.rb', line 154

def end_date
  Date.new(@year, @number, -1)
end

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/month.rb', line 58

def eql?(object)
  object.class == self.class && object.hash == self.hash
end

#hashObject


54
55
56
# File 'lib/month.rb', line 54

def hash
  [@year, @number].hash
end

#include?(time) ⇒ Boolean Also known as: ===

Returns:

  • (Boolean)

144
145
146
# File 'lib/month.rb', line 144

def include?(time)
  @year == time.year && @number == time.month
end

#inspectObject


40
41
42
# File 'lib/month.rb', line 40

def inspect
  "<Month: #{to_s}>"
end

#lengthObject


162
163
164
# File 'lib/month.rb', line 162

def length
  end_date.mday
end

#nameObject


44
45
46
# File 'lib/month.rb', line 44

def name
  NAMES.fetch(@number)
end

#nextObject 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_monthObject


86
87
88
# File 'lib/month.rb', line 86

def prev_month
  self - 1
end

#start_dateObject


150
151
152
# File 'lib/month.rb', line 150

def start_date
  Date.new(@year, @number, 1)
end

#step(limit, step = 1) ⇒ Object

Raises:

  • (ArgumentError)

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_sObject 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