<commit_before>module Shoulda
  module Whenever
    def schedule(task)
      ScheduleMatcher.new(task)
    end

    alias_method :schedule_rake, :schedule
    alias_method :schedule_runner, :schedule
    alias_method :schedule_command, :schedule

    class ScheduleMatcher
      attr_reader :duration, :time, :task, :roles

      def initialize(task)
        @task = task
        @duration = nil
        @time = nil
        @roles = nil
      end

      def matches?(subject)
        jobs = subject.instance_variable_get("@jobs")

        jobs = filter_jobs_by_duration(jobs)
        jobs = filter_jobs_by_time(jobs)
        jobs = filter_jobs_by_roles(jobs)
        jobs = filter_jobs_by_task(jobs)

        jobs.any?
      end

      def filter_jobs_by_duration(jobs)
        if duration.nil?
          jobs.values.flatten
        else
          jobs[duration]
        end
      end

      def filter_jobs_by_time(jobs)
        return jobs if time.nil?

        jobs.select { |job| job.at == time }
      end

      def filter_jobs_by_roles(jobs)
        return jobs if roles.nil? || roles.empty?

        jobs.select { |job| job.roles == roles }
      end

      def filter_jobs_by_task(jobs)
        jobs.select do |job|
          job.instance_variable_get("@options")[:task] == task
        end
      end

      def every(duration)<commit_msg>Fetches proper duration, provides fallback if duration is not found.<commit_after>module Shoulda
  module Whenever
    def schedule(task)
      ScheduleMatcher.new(task)
    end

    alias_method :schedule_rake, :schedule
    alias_method :schedule_runner, :schedule
    alias_method :schedule_command, :schedule

    class ScheduleMatcher
      attr_reader :duration, :time, :task, :roles

      def initialize(task)
        @task = task
        @duration = nil
        @time = nil
        @roles = nil
      end

      def matches?(subject)
        jobs = subject.instance_variable_get("@jobs")

        jobs = filter_jobs_by_duration(jobs)
        jobs = filter_jobs_by_time(jobs)
        jobs = filter_jobs_by_roles(jobs)
        jobs = filter_jobs_by_task(jobs)

        jobs.any?
      end

      def filter_jobs_by_duration(jobs)
        if duration.nil?
          jobs.values.flatten
        else
          duration_to_fetch = if duration.is_a?(String) || duration.is_a?(Symbol)
                                duration
                              else
                                duration.to_i
                              end

          jobs.fetch(duration_to_fetch) { [] }
        end
      end

      def filter_jobs_by_time(jobs)
        return jobs if time.nil?

        jobs.select { |job| job.at == time }
      end

      def filter_jobs_by_roles(jobs)
        return jobs if roles.nil? || roles.empty?

        jobs.select { |job| job.roles == roles }
      end

      def filter_jobs_by_task(jobs)
        jobs.select do |job|
          job.instance_variable_get("@options")[:task] == task
        end
      end

      def every(duration)
{'fraction_non_alphanumeric': 0.08067729083665338, 'fraction_numerical': 0.0, 'mean_word_length': 1.9224054316197867, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1873106', 'n_tokens_mistral': 928, 'n_tokens_neox': 867, 'n_words': 200}