Class: Temporal::Units::Month

Inherits:
Base
  • Object
show all
Defined in:
lib/temporal/units/month.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #==, #inspect, #to_f, #to_i, #to_s

Constructor Details

#initialize(value, year = nil) ⇒ Month

Returns a new instance of Month.



7
8
9
10
11
# File 'lib/temporal/units/month.rb', line 7

def initialize(value, year = nil)
  @year = year

  super(value)
end

Instance Attribute Details

#lowerObject

Returns the value of attribute lower.



5
6
7
# File 'lib/temporal/units/month.rb', line 5

def lower
  @lower
end

Instance Method Details

#+(other) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/temporal/units/month.rb', line 37

def +(other)
  sum = @value + other

  @value = case sum
           when (..0)
             @year = @year.send(:-, 1, inplace: true)
             12 - sum.abs
           when (13..)
             @year = @year.send(:+, 1, inplace: true)
             sum - 12
           else
             sum
           end

  if @lower > max_day
    diff = max_day - @lower.to_i

    @lower += diff
  end

  self
end

#-(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/temporal/units/month.rb', line 60

def -(other)
  sum = @value - other

  @value = case sum
           when (..0)
             @year = @year.send(:+, 1, inplace: true)
             12 + sum
           when (13..)
             @year = @year.send(:-, 1, inplace: true)
             sum - 12
           else
             sum
           end

  if @lower > max_day
    diff = max_day - @lower.to_i

    @lower += diff
  end

  self.class.new(sum, @year)
end

#max_dayObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/temporal/units/month.rb', line 15

def max_day
  case [@value, on_leap_year?]
  in [1 | 3 | 5 | 7 | 8 | 10 | 12, *]
    31
  in [4 | 6 | 9 | 11, *]
    30
  in [2, true]
    29
  in [2, false]
    28
  end
end

#on_leap_year?Boolean

Returns:

  • (Boolean)


13
# File 'lib/temporal/units/month.rb', line 13

def on_leap_year? = @year.nil? || (@year.respond_to?(:leap?) && @year.leap?)