<commit_before>module Kvlr #:nodoc:

  module ReportsAsSparkline #:nodoc:

    # A ReportingPeriod is  - depending on the Grouping - either a specific hour, a day, a month or a year. All records falling into this period will be grouped together.
    class ReportingPeriod

      attr_reader :date_time, :grouping

      # ==== Parameters
      # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
      # * <tt>date_time</tt> - The DateTime that reporting period is created for
      def initialize(grouping, date_time = DateTime.now)
        @grouping  = grouping
        @date_time = parse_date_time(date_time)
      end

      # Returns the first reporting period for a grouping and a limit; e.g. the first reporting period for Grouping :day and limit 2 would be Time.now - 2.days
      #
      # ==== Parameters
      # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
      # * <tt>limit</tt> - The number of reporting periods until the first one
      def self.first(grouping, limit)
        return case grouping.identifier
          when :hour
            self.new(grouping, DateTime.now - limit.hours)
          when :day
            self.new(grouping, DateTime.now - limit.days)
          when :week
            self.new(grouping, DateTime.now - limit.weeks)
          when :month
            date = DateTime.now - limit.months
            self.new(grouping, Date.new(date.year, date.month, 1))
        end
      end

      def self.from_db_string(grouping, db_string) #:nodoc:
        parts = grouping.date_parts_from_db_string(db_string)
        result = case grouping.identifier
          when :hour
            self.new(grouping, DateTime.new(parts[0], parts[1], parts[2], parts[3], 0, 0))
          when :day
            self.new(grouping, Date.new(parts[0], parts[1], parts[2]))
          when :week
            self.new(grouping, Date.commercial(parts[0], parts[1], 1))
          when :month
            self.new(grouping, Date.new(parts[0], parts[1], 1))
        end
        result
      end

      # Returns the next reporting period (that is next hour/day/month/year)
      def next
        return case @grouping.identifier
          when :hour
            self.class.new(@grouping, @date_time + 1.hour)
          when :day
            self.class.new(@grouping, @date_time + 1.day)
          when :week
            self.class.new(@grouping, @date_time + 1.week)
          when :month
            self.class.new(@grouping, @date_time + 1.month)
        end
      end

      # Returns the previous reporting period (that is next hour/day/month/year)
      def previous
        return case @grouping.identifier
          when :hour
            self.class.new(@grouping, @date_time - 1.hour)
          when :day
            self.class.new(@grouping, @date_time - 1.day)
          when :week
            self.class.new(@grouping, @date_time - 1.week)
          when :month
            self.class.new(@grouping, @date_time - 1.month)
        end
      end

      def ==(other) #:nodoc:
        if other.class == Kvlr::ReportsAsSparkline::ReportingPeriod
          return @date_time.to_s == other.date_time.to_s && @grouping.identifier.to_s == other.grouping.identifier.to_s
        end
        false
      end<commit_msg>Simplified and DRYed up logic for ReportingPeriod#first, #next and #previous.<commit_after>module Kvlr #:nodoc:

  module ReportsAsSparkline #:nodoc:

    # A ReportingPeriod is  - depending on the Grouping - either a specific hour, a day, a month or a year. All records falling into this period will be grouped together.
    class ReportingPeriod

      attr_reader :date_time, :grouping

      # ==== Parameters
      # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
      # * <tt>date_time</tt> - The DateTime that reporting period is created for
      def initialize(grouping, date_time = DateTime.now)
        @grouping  = grouping
        @date_time = parse_date_time(date_time)
      end

      # Returns the first reporting period for a grouping and a limit; e.g. the first reporting period for Grouping :day and limit 2 would be Time.now - 2.days
      #
      # ==== Parameters
      # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
      # * <tt>limit</tt> - The number of reporting periods until the first one
      def self.first(grouping, limit)
        self.new(grouping, DateTime.now).offset(-limit)
      end

      def self.from_db_string(grouping, db_string) #:nodoc:
        parts = grouping.date_parts_from_db_string(db_string)
        result = case grouping.identifier
          when :hour
            self.new(grouping, DateTime.new(parts[0], parts[1], parts[2], parts[3], 0, 0))
          when :day
            self.new(grouping, Date.new(parts[0], parts[1], parts[2]))
          when :week
            self.new(grouping, Date.commercial(parts[0], parts[1], 1))
          when :month
            self.new(grouping, Date.new(parts[0], parts[1], 1))
        end
        result
      end

      # Returns the next reporting period (that is next hour/day/month/year)
      def next
        self.offset(1)
      end

      # Returns the previous reporting period (that is next hour/day/month/year)
      def previous
        self.offset(-1)
      end
      
      def offset(val)
        self.class.new(@grouping, @date_time + val.send(@grouping.identifier))
      end

      def ==(other) #:nodoc:
        if other.class == Kvlr::ReportsAsSparkline::ReportingPeriod
          return @date_time.to_s == other.date_time.to_s && @grouping.identifier.to_s == other.grouping.identifier.to_s
        end
        false
      end
{'fraction_non_alphanumeric': 0.10260443978325468, 'fraction_numerical': 0.007865757734661772, 'mean_word_length': 2.290396779758482, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6264860', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1686, 'n_words': 474}