Sha256: 1209ca052eb0b8b40dca6cc3ebfa7106c051db2d736ecadf485d75d539054ab5

Contents?: true

Size: 1.74 KB

Versions: 39

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

# Borrowed from https://gist.github.com/amydoesntlai/74eb9aef1ea4b535f8fe

require "date"

class AgeCalculator
  # Calculates an age based on the given current date.
  #
  # @param birth_date Date
  # @param current_date Date
  # @return [Hash] containing the year, month and day
  #
  def compute(birth_date, current_date)
    calculation = build_calculation(birth_date, current_date)
    compute_days(calculation, birth_date, current_date)
    compute_months(calculation, birth_date, current_date)
    handle_future_dates(calculation)

    build_hash(calculation)
  end

  private

  def build_calculation(birth_date, current_date)
    OpenStruct.new(
      days:   current_date.day - birth_date.day,
      months: current_date.month - birth_date.month,
      years:  current_date.year - birth_date.year,
      borrowed_month: false
    )
  end

  def compute_days(calculation, birth_date, current_date)
    return unless calculation.days < 0

    days_in_month = Time.days_in_month(birth_date.month)
    calculation.days = (days_in_month - birth_date.day) + current_date.day
    calculation.months -= 1
    calculation.borrowed_month = true
  end

  def compute_months(calculation, birth_date, current_date)
    return unless calculation.months < 0

    months_in_year = 12
    calculation.months = (months_in_year - birth_date.month) + current_date.month
    calculation.months -= 1 if calculation.borrowed_month
    calculation.years -= 1
  end

  # Error-handling for future date
  #
  def handle_future_dates(calculation)
    return unless calculation.years < 0

    calculation.years = calculation.months = calculation.days = 0
  end

  def build_hash(calculation)
    calculation.to_h.delete_if { |k, _v| k == :borrowed_month }
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
renalware-core-2.0.58 lib/age_calculator.rb
renalware-core-2.0.57 lib/age_calculator.rb
renalware-core-2.0.56 lib/age_calculator.rb
renalware-core-2.0.55 lib/age_calculator.rb
renalware-core-2.0.54 lib/age_calculator.rb
renalware-core-2.0.53 lib/age_calculator.rb
renalware-core-2.0.52 lib/age_calculator.rb
renalware-core-2.0.51 lib/age_calculator.rb
renalware-core-2.0.50 lib/age_calculator.rb
renalware-core-2.0.48 lib/age_calculator.rb
renalware-core-2.0.47 lib/age_calculator.rb
renalware-core-2.0.46 lib/age_calculator.rb
renalware-core-2.0.45 lib/age_calculator.rb
renalware-core-2.0.44 lib/age_calculator.rb
renalware-core-2.0.43 lib/age_calculator.rb
renalware-core-2.0.42 lib/age_calculator.rb
renalware-core-2.0.41 lib/age_calculator.rb
renalware-core-2.0.40 lib/age_calculator.rb
renalware-core-2.0.39 lib/age_calculator.rb
renalware-core-2.0.38 lib/age_calculator.rb