Class: Temporal::PlainDate

Inherits:
Object
  • Object
show all
Defined in:
lib/temporal/plain_date.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month, day, calendar_id: nil) ⇒ PlainDate

Returns a new instance of PlainDate.



7
8
9
10
11
12
13
# File 'lib/temporal/plain_date.rb', line 7

def initialize(year, month, day, calendar_id: nil)
  @year = Units::Year.new(year)
  @month = Units::Month.new(month, @year)
  @day = Units::Day.new(day, @month)
  @month.lower = @day
  self.calendar_id = calendar_id
end

Instance Attribute Details

#calendar_idObject

Returns the value of attribute calendar_id.



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

def calendar_id
  @calendar_id
end

#dayObject (readonly)

Returns the value of attribute day.



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

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month.



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

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
# File 'lib/temporal/plain_date.rb', line 45

def ==(other)
  @year == other.year &&
    @month == other.month &&
    @day == other.day
end

#add(hash = {}, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/temporal/plain_date.rb', line 21

def add(hash = {}, **kwargs)
  raise ArgumentError unless hash.instance_of? Hash

  values = hash.merge kwargs
  raise ArgumentError if values.empty?

  unit_keys = i[years months weeks days hours minutes
                 seconds milliseconds microseconds nanoseconds]

  units = values.slice(*unit_keys)

  raise ArgumentError, "No valid unit found" if units.empty?

  raise RangeError, "Can't mix positive and negative numbers" unless
    units.values.reject(&:zero?).all?(&:positive?) ||
    units.values.reject(&:zero?).all?(&:negative?)

  @day += values[:days] || 0
  @month += values[:months] || 0
  @year += values[:years] || 0

  self
end

#eraObject



19
# File 'lib/temporal/plain_date.rb', line 19

def era = nil

#era_yearObject



17
# File 'lib/temporal/plain_date.rb', line 17

def era_year = nil

#month_codeObject



15
# File 'lib/temporal/plain_date.rb', line 15

def month_code = :"#{format("M%02d", month)}"