Class: JobContracts::Contract
- Inherits:
-
Object
- Object
- JobContracts::Contract
show all
- Defined in:
- lib/job_contracts/contracts/contract.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(trigger: :after, halt: false, queues: [], expected: {}) ⇒ Contract
Returns a new instance of Contract.
7
8
9
10
11
12
|
# File 'lib/job_contracts/contracts/contract.rb', line 7
def initialize(trigger: :after, halt: false, queues: [], expected: {})
@trigger = trigger.to_sym
@halt = halt
@queues = Set.new(queues.map(&:to_s))
self.expected.merge! expected
end
|
Instance Attribute Details
#queues ⇒ Object
Returns the value of attribute queues.
5
6
7
|
# File 'lib/job_contracts/contracts/contract.rb', line 5
def queues
@queues
end
|
#trigger ⇒ Object
Returns the value of attribute trigger.
5
6
7
|
# File 'lib/job_contracts/contracts/contract.rb', line 5
def trigger
@trigger
end
|
Instance Method Details
#actual ⇒ Object
18
19
20
|
# File 'lib/job_contracts/contracts/contract.rb', line 18
def actual
@actual ||= HashWithIndifferentAccess.new
end
|
#after? ⇒ Boolean
52
53
54
|
# File 'lib/job_contracts/contracts/contract.rb', line 52
def after?
trigger == :after
end
|
#before? ⇒ Boolean
48
49
50
|
# File 'lib/job_contracts/contracts/contract.rb', line 48
def before?
trigger == :before
end
|
#breached? ⇒ Boolean
40
41
42
|
# File 'lib/job_contracts/contracts/contract.rb', line 40
def breached?
!satisfied?
end
|
#enforce!(contractable) ⇒ Object
Method to be implemented by subclasses NOTE: subclasses should update ‘actual`, set `satisfied`, and call `super`
29
30
31
32
33
34
|
# File 'lib/job_contracts/contracts/contract.rb', line 29
def enforce!(contractable)
return unless should_enforce?(contractable)
return if satisfied?
contractable.breached_contracts << self
invoke_contract_breach_callback contractable
end
|
#expected ⇒ Object
14
15
16
|
# File 'lib/job_contracts/contracts/contract.rb', line 14
def expected
@expected ||= HashWithIndifferentAccess.new
end
|
#halt? ⇒ Boolean
44
45
46
|
# File 'lib/job_contracts/contracts/contract.rb', line 44
def halt?
!!@halt
end
|
#satisfied? ⇒ Boolean
36
37
38
|
# File 'lib/job_contracts/contracts/contract.rb', line 36
def satisfied?
!!satisfied
end
|
#should_enforce?(contractable) ⇒ Boolean
22
23
24
25
|
# File 'lib/job_contracts/contracts/contract.rb', line 22
def should_enforce?(contractable)
return true if queues.include?("*")
queues.include? contractable.queue_name.to_s
end
|
#to_h ⇒ Object
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/job_contracts/contracts/contract.rb', line 56
def to_h
HashWithIndifferentAccess.new(
name: self.class.name,
trigger: trigger,
halt: halt?,
queues: queues.to_a,
expected: expected,
actual: actual
)
end
|