Sha256: e25b12b81f4a298bd5d4bb9312a0b46aca4e283742d66743ed44d26ed2bd37fe

Contents?: true

Size: 842 Bytes

Versions: 5

Compression:

Stored size: 842 Bytes

Contents

module SlideRule
  module DistanceCalculators
    class DayOfMonth
      MAX_DAYS = 15

      #
      # Calculates distance using 15 as the max point.
      #   Does not take into account the number of days in the actual month being considered.
      #
      def calculate(first, second)
        first = cleanse_date(first)
        second = cleanse_date(second)

        difference_in_days(first, second).to_f / MAX_DAYS
      end

      def difference_in_days(first, second)
        distance = (first.mday - second.mday).abs
        return distance if distance <= MAX_DAYS
        MAX_DAYS - (distance - MAX_DAYS)
      end

      private

      def cleanse_date(date)
        date = Date.parse(date) unless date.is_a?(::Date) || date.is_a?(::Time)
        date = date.to_date if date.is_a?(::Time)

        date
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slide_rule-0.2.0 lib/slide_rule/distance_calculators/day_of_month.rb
slide_rule-0.0.4 lib/slide_rule/distance_calculators/day_of_month.rb
slide_rule-0.1.0 lib/slide_rule/distance_calculators/day_of_month.rb
slide_rule-0.0.3 lib/slide_rule/distance_calculators/day_of_month.rb
slide_rule-0.0.2 lib/slide_rule/distance_calculators/day_of_month.rb