<commit_before>module Sleek
  class Timeframe
    REGEXP = /(this|previous)_((\d+)_)?(minute|hour|day|week|month)s?/

    class << self
      # Internal: Transform the object passed to Timeframe initializer
      # into a range of Time objects.
      #
      # timeframe - either a Range of Time objects, a two-element array
      #             of Time Objects, or a special string.
      #
      # Examples
      #
      #   Timeframe.to_range :this_2_days
      #
      #   Timeframe.to_range :previous_hour
      #
      # Raises ArgumentError if passed object can't be processed.
      def to_range(timeframe)
        case timeframe
        when proc { |tf| tf.is_a?(Range) && tf.time_range? }
          t = timeframe
        when proc { |tf| tf.is_a?(Array) && tf.size == 2 && tf.count { |_tf| _tf.is_a?(Time) } == 2 }
          t = timeframe.first..timeframe.last
        when String, Symbol
          t = parse(timeframe.to_s)
        else
          raise ArgumentError, "wrong timeframe - #{timeframe}"
        end
      end

      # Internal: Process timeframe string to make up a range.
      #
      # timeframe - the String matching
      #             (this|previous)_((\d+)_)?(minute|hour|day|week|month)s?
      def parse(timeframe)
        _, category, _, number, interval = *timeframe.match(REGEXP)

        unless category && interval
          raise ArgumentError, "special timeframe string is malformed"
        end

        number ||= 1
        number = number.to_i

        end_point = Time.now.send("end_of_#{interval}").round
        start_point = end_point - number.send(interval)

        range = start_point..end_point
        range = range - 1.send(interval) if category == 'previous'
        range
      end<commit_msg>refactor range-from-interval creation into a separate method<commit_after>module Sleek
  class Timeframe
    REGEXP = /(this|previous)_((\d+)_)?(minute|hour|day|week|month)s?/

    class << self
      # Internal: Transform the object passed to Timeframe initializer
      # into a range of Time objects.
      #
      # timeframe - either a Range of Time objects, a two-element array
      #             of Time Objects, or a special string.
      #
      # Examples
      #
      #   Timeframe.to_range :this_2_days
      #
      #   Timeframe.to_range :previous_hour
      #
      # Raises ArgumentError if passed object can't be processed.
      def to_range(timeframe)
        case timeframe
        when proc { |tf| tf.is_a?(Range) && tf.time_range? }
          t = timeframe
        when proc { |tf| tf.is_a?(Array) && tf.size == 2 && tf.count { |_tf| _tf.is_a?(Time) } == 2 }
          t = timeframe.first..timeframe.last
        when String, Symbol
          t = parse(timeframe.to_s)
        else
          raise ArgumentError, "wrong timeframe - #{timeframe}"
        end
      end

      # Internal: Process timeframe string to make up a range.
      #
      # timeframe - the String matching
      #             (this|previous)_((\d+)_)?(minute|hour|day|week|month)s?
      def parse(timeframe)
        _, category, _, number, interval = *timeframe.match(REGEXP)

        unless category && interval
          raise ArgumentError, "special timeframe string is malformed"
        end

        number ||= 1
        number = number.to_i

        range = range_from_interval(interval, number)
        range = range - 1.send(interval) if category == 'previous'
        range
      end

      # Internal: Create a time range from interval type & number of
      # intervals.
      #
      # interval - the String interval type name. Valid values are
      #            minute, hour, day, week, and month.
      # number   - the Integer number of periods.
      #
      # Returns the range of time objects.
      def range_from_interval(interval, number = 1)
        end_point = Time.now.send("end_of_#{interval}").round
        start_point = end_point - number.send(interval)

        start_point..end_point
      end
{'fraction_non_alphanumeric': 0.09921934021657014, 'fraction_numerical': 0.002770083102493075, 'mean_word_length': 2.067181467181467, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6053914', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1146, 'n_words': 326}