Class: LunarBlessing::Date

Inherits:
Object
  • Object
show all
Includes:
DatesMaping
Defined in:
lib/lunar_blessing.rb

Constant Summary collapse

MIN_YEAR =
1900
MAX_YEAR =
2099

Constants included from DatesMaping

LunarBlessing::DatesMaping::DATES_MAPPING, LunarBlessing::DatesMaping::DAY_MAPPING, LunarBlessing::DatesMaping::MONTH_MAPPING, LunarBlessing::DatesMaping::YEAR_MAPPING

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year:, month:, day:, leap: false) ⇒ Date

Returns a new instance of Date.

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lunar_blessing.rb', line 14

def initialize(year:, month:, day:, leap: false)
  raise OutOfRange.new(year) if year < MIN_YEAR || year > MAX_YEAR

  @year = year
  @month = month
  @day = day
  @leap = leap
  @leap_month = DATES_MAPPING[offset][0].to_i

  reset_month_by_leap
  init_solar_date

  if @month > 12 || @solar_month > 12
    @solar_year += 1
    @solar_month -= 12
  end

  @month -= 1 if @leap_month > 0 && @month > @leap_month
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



12
13
14
# File 'lib/lunar_blessing.rb', line 12

def day
  @day
end

#leapObject (readonly)

Returns the value of attribute leap.



12
13
14
# File 'lib/lunar_blessing.rb', line 12

def leap
  @leap
end

#monthObject (readonly)

Returns the value of attribute month.



12
13
14
# File 'lib/lunar_blessing.rb', line 12

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



12
13
14
# File 'lib/lunar_blessing.rb', line 12

def year
  @year
end

Instance Method Details

#to_s(with_year: false) ⇒ Object



38
39
40
41
42
43
# File 'lib/lunar_blessing.rb', line 38

def to_s(with_year: false)
  mm = MONTH_MAPPING[month.to_i]
  dd = DAY_MAPPING[day.to_i]

  "#{with_year ? chinese_year : ''}#{mm}#{dd}"
end

#to_solarObject



34
35
36
# File 'lib/lunar_blessing.rb', line 34

def to_solar
  solar_date.to_s
end