Class: Shoulda::Whenever::ScheduleMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/whenever/schedule_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ ScheduleMatcher

Returns a new instance of ScheduleMatcher.



14
15
16
17
18
19
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 14

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

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



12
13
14
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12

def duration
  @duration
end

#rolesObject (readonly)

Returns the value of attribute roles.



12
13
14
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12

def roles
  @roles
end

#taskObject (readonly)

Returns the value of attribute task.



12
13
14
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12

def task
  @task
end

#timeObject (readonly)

Returns the value of attribute time.



12
13
14
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12

def time
  @time
end

Instance Method Details

#at(time) ⇒ Object



70
71
72
73
74
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 70

def at(time)
  @time = time

  self
end

#descriptionObject



83
84
85
86
87
88
89
90
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 83

def description
  [
    base_description,
    duration_description,
    time_description,
    roles_description
  ].compact.join(' ')
end

#every(duration) ⇒ Object



64
65
66
67
68
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 64

def every(duration)
  @duration = duration

  self
end

#failure_messageObject



92
93
94
95
96
97
98
99
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 92

def failure_message
  [
    base_failure_message,
    duration_description,
    time_description,
    roles_description
  ].compact.join(' ')
end

#failure_message_when_negatedObject



101
102
103
104
105
106
107
108
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 101

def failure_message_when_negated
  [
    base_failure_message_when_negated,
    duration_description,
    time_description,
    roles_description
  ].compact.join(' ')
end

#filter_jobs_by_duration(jobs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 32

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

#filter_jobs_by_roles(jobs) ⇒ Object



52
53
54
55
56
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 52

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

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

#filter_jobs_by_task(jobs) ⇒ Object



58
59
60
61
62
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 58

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

#filter_jobs_by_time(jobs) ⇒ Object



46
47
48
49
50
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 46

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

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 21

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

#with_roles(roles) ⇒ Object Also known as: with_role



76
77
78
79
80
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 76

def with_roles(roles)
  @roles = Array(roles)

  self
end