Class: Miti::NepaliDate
- Inherits:
-
Object
- Object
- Miti::NepaliDate
- Defined in:
- lib/miti/nepali_date.rb
Overview
Class for nepali date
Defined Under Namespace
Classes: InvalidSeparatorError
Instance Attribute Summary collapse
-
#barsa ⇒ Object
readonly
Returns the value of attribute barsa.
-
#gatey ⇒ Object
readonly
Returns the value of attribute gatey.
-
#mahina ⇒ Object
readonly
Returns the value of attribute mahina.
Class Method Summary collapse
- .months ⇒ Object
- .months_in_english ⇒ Object
-
.parse(date_string) ⇒ Miti::NepaliDate
This method parses date in yyyy/mm/dd to NepaliDate object.
- .today ⇒ Object
- .week_days ⇒ Object
- .week_days_in_english ⇒ Object
Instance Method Summary collapse
-
#bar ⇒ Integer
Get weekday for a nepali date.
-
#descriptive(nepali: false) ⇒ String
Descriptive output for current date When nepali flag is true, month is returned in nepali font and week day in Nepali.
-
#initialize(barsa:, mahina:, gatey:) ⇒ NepaliDate
constructor
require barsa, mahina and gatey as attributes.
-
#tarik ⇒ Date
Get equivalent tarik(AD) for NepaliDate.
-
#to_h ⇒ Hash
returns details of nepali date in a ruby Hash.
-
#to_s(separator: "-") ⇒ String
Returns Nepali Date in string format(yyyy/mm/dd).
- #yday ⇒ Object
Constructor Details
#initialize(barsa:, mahina:, gatey:) ⇒ NepaliDate
require barsa, mahina and gatey as attributes
13 14 15 16 17 |
# File 'lib/miti/nepali_date.rb', line 13 def initialize(barsa:, mahina:, gatey:) @barsa = @mahina = mahina @gatey = gatey end |
Instance Attribute Details
#barsa ⇒ Object (readonly)
Returns the value of attribute barsa.
9 10 11 |
# File 'lib/miti/nepali_date.rb', line 9 def @barsa end |
#gatey ⇒ Object (readonly)
Returns the value of attribute gatey.
9 10 11 |
# File 'lib/miti/nepali_date.rb', line 9 def gatey @gatey end |
#mahina ⇒ Object (readonly)
Returns the value of attribute mahina.
9 10 11 |
# File 'lib/miti/nepali_date.rb', line 9 def mahina @mahina end |
Class Method Details
.months ⇒ Object
89 90 91 |
# File 'lib/miti/nepali_date.rb', line 89 def months %w[वैशाख ज्येष्ठ आषाढ़ श्रावण भाद्र आश्विन कार्तिक मंसिर पौष माघ फाल्गुण चैत्र] end |
.months_in_english ⇒ Object
93 94 95 |
# File 'lib/miti/nepali_date.rb', line 93 def months_in_english %w[Baishakh Jestha Ashadh Shrawan Bhadra Asoj Kartik Mangsir Poush Magh Falgun Chaitra] end |
.parse(date_string) ⇒ Miti::NepaliDate
This method parses date in yyyy/mm/dd to NepaliDate object
105 106 107 108 109 110 111 112 113 |
# File 'lib/miti/nepali_date.rb', line 105 def parse(date_string) regex = %r{\A\d{4}[,-/\s]\d{1,2}[,-/\s]\d{1,2}\z} raise "Invalid Date Format" unless regex.match(date_string) delimiters = ["-", " ", "/", ","] , mahina, gatey = date_string.split(Regexp.union(delimiters)) validate_parsed_date(.to_i, mahina.to_i, gatey.to_i) NepaliDate.new(barsa: .to_i, mahina: mahina.to_i, gatey: gatey.to_i) end |
.today ⇒ Object
81 82 83 |
# File 'lib/miti/nepali_date.rb', line 81 def today AdToBs.new(Date.today).convert end |
.week_days ⇒ Object
85 86 87 |
# File 'lib/miti/nepali_date.rb', line 85 def week_days %w[आइतबार सोमबार मंगलबार बुधबार बिहिबार शुक्रबार शनिबार] end |
.week_days_in_english ⇒ Object
97 98 99 |
# File 'lib/miti/nepali_date.rb', line 97 def week_days_in_english %w[Aitabar Somabar Mangalbar Budhabar Bihibar Sukrabar Sanibar] end |
Instance Method Details
#bar ⇒ Integer
Get weekday for a nepali date.
31 32 33 |
# File 'lib/miti/nepali_date.rb', line 31 def @bar ||= tarik&.wday end |
#descriptive(nepali: false) ⇒ String
Descriptive output for current date When nepali flag is true, month is returned in nepali font and week day in Nepali
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/miti/nepali_date.rb', line 62 def descriptive(nepali: false) month_index = mahina - 1 if nepali month = NepaliDate.months[month_index] week_day = "#{NepaliDate.week_days_in_english[]}(#{NepaliDate.week_days[]})}" else month = NepaliDate.months_in_english[month_index] week_day = tarik.strftime("%A") end "#{month} #{gatey}, #{} #{week_day}" end |
#tarik ⇒ Date
Get equivalent tarik(AD) for NepaliDate
23 24 25 |
# File 'lib/miti/nepali_date.rb', line 23 def tarik @tarik ||= BsToAd.new(self).convert end |
#to_h ⇒ Hash
returns details of nepali date in a ruby Hash
39 40 41 |
# File 'lib/miti/nepali_date.rb', line 39 def to_h { barsa: , mahina: mahina, gatey: gatey, bar: , tarik: tarik } end |
#to_s(separator: "-") ⇒ String
Returns Nepali Date in string format(yyyy/mm/dd).
49 50 51 52 53 54 55 |
# File 'lib/miti/nepali_date.rb', line 49 def to_s(separator: "-") raise InvalidSeparatorError, "Invalid separator provided." unless [" ", "/", "-"].include?(separator) [, mahina, gatey].reduce("") do |final_date, date_element| "#{final_date}#{final_date.empty? ? "" : separator}#{date_element < 10 ? 0 : ""}#{date_element}" end end |
#yday ⇒ Object
75 76 77 78 |
# File 'lib/miti/nepali_date.rb', line 75 def yday days_before_month = Miti::Data::NEPALI_YEAR_MONTH_HASH[].first(mahina - 1).sum days_before_month + gatey end |