Class: BahaiDate::BahaiDate

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

Constant Summary collapse

AYYAM_I_HA =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ BahaiDate

Returns a new instance of BahaiDate.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bahai_date/bahai_date.rb', line 7

def initialize(params)
  if params[:date]
    @gregorian_date = params[:date]
    year, month, day = from_gregorian
    @year = Year.new(year)
    @month = Month.new(month)
    @day = Day.new(day)
  elsif params[:year] && params[:month] && params[:day]
    @year = Year.new(params[:year])
    @month = Month.new(params[:month])
    @day = Day.new(params[:day])
    validate_ayyam_i_ha
    @gregorian_date = to_gregorian
  else
    fail ArgumentError, 'Invalid arguments. Use a hash with :date or with :year, :month, and :day.'
  end
  @weekday = Weekday.new(weekday_from_gregorian)
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



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

def day
  @day
end

#gregorian_dateObject (readonly)

Returns the value of attribute gregorian_date.



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

def gregorian_date
  @gregorian_date
end

#monthObject (readonly)

Returns the value of attribute month.



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

def month
  @month
end

#weekdayObject (readonly)

Returns the value of attribute weekday.



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

def weekday
  @weekday
end

#yearObject (readonly)

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#+(val) ⇒ Object



42
43
44
# File 'lib/bahai_date/bahai_date.rb', line 42

def +(val)
  self.class.new(date: @gregorian_date + val)
end

#-(val) ⇒ Object



46
47
48
# File 'lib/bahai_date/bahai_date.rb', line 46

def -(val)
  self.class.new(date: @gregorian_date - val)
end

#long_formatObject



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

def long_format
  "#{@weekday} #{@day.number} #{@month} #{@year.bahai_era} B.E."
end

#occasionsObject



26
27
28
# File 'lib/bahai_date/bahai_date.rb', line 26

def occasions
  OccasionFactory.new(@year.bahai_era, @month.number, @day.number).occasions
end

#short_formatObject



38
39
40
# File 'lib/bahai_date/bahai_date.rb', line 38

def short_format
  "#{@day.number} #{@month} #{@year.bahai_era}"
end

#to_sObject



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

def to_s
  "#{@year.bahai_era}.#{@month.number}.#{@day.number}"
end