Class: Shoulda::Whenever::ScheduleMatcher
- Inherits:
-
Object
- Object
- Shoulda::Whenever::ScheduleMatcher
- Defined in:
- lib/shoulda/whenever/schedule_matcher.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
-
#task ⇒ Object
readonly
Returns the value of attribute task.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #at(time) ⇒ Object
- #description ⇒ Object
- #every(duration) ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
- #filter_jobs_by_duration(jobs) ⇒ Object
- #filter_jobs_by_roles(jobs) ⇒ Object
- #filter_jobs_by_task(jobs) ⇒ Object
- #filter_jobs_by_time(jobs) ⇒ Object
-
#initialize(task) ⇒ ScheduleMatcher
constructor
A new instance of ScheduleMatcher.
- #matches?(subject) ⇒ Boolean
- #with_roles(roles) ⇒ Object (also: #with_role)
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
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
12 13 14 |
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12 def duration @duration end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
12 13 14 |
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12 def roles @roles end |
#task ⇒ Object (readonly)
Returns the value of attribute task.
12 13 14 |
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 12 def task @task end |
#time ⇒ Object (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 |
#description ⇒ Object
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_message ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 92 def [ , duration_description, time_description, roles_description ].compact.join(' ') end |
#failure_message_when_negated ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/shoulda/whenever/schedule_matcher.rb', line 101 def [ , 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
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 |