Skip to content

Commit

Permalink
chore: move next method to separate class and remove unused method
Browse files Browse the repository at this point in the history
  • Loading branch information
xkshitizx committed Nov 5, 2023
1 parent e045808 commit 913b375
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
18 changes: 12 additions & 6 deletions lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def today
formatted_miti = "[#{current_nepali_miti} BS] #{current_nepali_miti.descriptive}"
formatted_date = "[#{date} AD] " + date.strftime("%B %d, %Y %A")

@shell.say("#{formatted_miti}\n#{formatted_date}", :green)
@shell.say(formatted_miti.to_s, :green)
@shell.say(formatted_date.to_s, :green)
end

desc "to_bs ENGLISH_DATE", "converts ENGLISH_DATE to Nepali Miti"
Expand Down Expand Up @@ -65,18 +66,23 @@ def to_ad(nepali_date)

desc "next", "get remaining days for 1st of next month and last day of current month"
def next
next_month = Miti.to_bs(Date.today.to_s).next_month_first
days_left_description = "\n#{next_month[:days_left]} days left until 1st #{next_month[:month_name]}"
current_month_last_description = "Current month's last date => #{next_month[:antim_gatey]}"

days_left_description, current_month_last_description = Cli::Next.new(NepaliDate.today).calculate
@shell.say(days_left_description, :green)
@shell.say(current_month_last_description, :cyan)
@shell.say(current_month_last_description, :green)
end

desc "difference", "get remaining days for 1st of next month and last day of current month"
def difference(date1, date2)
@shell.say(Miti.differentiate(date1, date2), :cyan)
end

no_commands do
def self.exit_on_failure?
true
end
end

map ad: :to_ad
map bs: :to_bs
end
end
52 changes: 52 additions & 0 deletions lib/miti/cli/next.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module Miti
module Cli
# calculates first day of next month
class Next
def initialize(nepali_date)
@current_date = nepali_date
@current_mahina = nepali_date.mahina
@current_barsa = nepali_date.barsa
@current_gatey = nepali_date.gatey
end

def calculate
[days_left_description, current_month_last_description]
end

private

attr_reader :current_date, :current_barsa, :current_mahina, :current_gatey

def days_left_description
"\n#{days_left} days left until #{next_month_first.descriptive}"
end

def current_month_last_description
"Current month's last date => #{last_gatey_this_month}-#{end_of_month.descriptive}"
end

def days_left
NepaliDate::Difference.new(next_month_first, current_date).difference[:days]
end

def next_month_first
@next_month_first ||= begin
month, year = current_mahina < 12 ? [current_mahina + 1, current_barsa] : [1, current_barsa + 1]
NepaliDate.new(barsa: year, mahina: month, gatey: 1)
end
end

def end_of_month
@end_of_month ||= NepaliDate.new(
barsa: current_barsa, mahina: current_mahina, gatey: last_gatey_this_month
)
end

def last_gatey_this_month
Miti::Data::NEPALI_YEAR_MONTH_HASH[current_barsa][current_mahina - 1]
end
end
end
end
12 changes: 0 additions & 12 deletions lib/miti/nepali_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ def yday
days_before_month + gatey
end

def next_month_first
current_year_calendar = Miti::Data::NEPALI_YEAR_MONTH_HASH[barsa]
next_month = mahina < 12 ? mahina : 0
antim_gatey = current_year_calendar[mahina - 1]

{
days_left: antim_gatey - gatey + 1,
month_name: NepaliDate.months_in_english[next_month],
antim_gatey: "#{antim_gatey} #{NepaliDate.months_in_english[mahina - 1]}"
}
end

class << self
def today
AdToBs.new(Date.today).convert
Expand Down

0 comments on commit 913b375

Please sign in to comment.