Sha256: 3cd50d9751e6e8b62811d30433a6aa24688a018f920e105c838fac87e9572469

Contents?: true

Size: 1.73 KB

Versions: 103

Compression:

Stored size: 1.73 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

103 entries across 103 versions & 1 rubygems

Version Path
renalware-core-2.1.1 lib/age_calculator.rb
renalware-core-2.1.0 lib/age_calculator.rb
renalware-core-2.0.167 lib/age_calculator.rb
renalware-core-2.0.166 lib/age_calculator.rb
renalware-core-2.0.165 lib/age_calculator.rb
renalware-core-2.0.164 lib/age_calculator.rb
renalware-core-2.0.163 lib/age_calculator.rb
renalware-core-2.0.162 lib/age_calculator.rb
renalware-core-2.0.161 lib/age_calculator.rb
renalware-core-2.0.160 lib/age_calculator.rb
renalware-core-2.0.159 lib/age_calculator.rb
renalware-core-2.0.158 lib/age_calculator.rb
renalware-core-2.0.157 lib/age_calculator.rb
renalware-core-2.0.156 lib/age_calculator.rb
renalware-core-2.0.155 lib/age_calculator.rb
renalware-core-2.0.153 lib/age_calculator.rb
renalware-core-2.0.152 lib/age_calculator.rb
renalware-core-2.0.151 lib/age_calculator.rb
renalware-core-2.0.149 lib/age_calculator.rb
renalware-core-2.0.148 lib/age_calculator.rb