Class: S2P::Contractor
- Inherits:
-
Object
- Object
- S2P::Contractor
- Defined in:
- lib/s2p/contractor.rb
Instance Attribute Summary collapse
-
#observations ⇒ Object
readonly
Returns the value of attribute observations.
Class Method Summary collapse
- .[](description) ⇒ Object
- .conditions_disabled? ⇒ Boolean
- .disable_enforcement ⇒ Object
- .for(description) ⇒ Object
Instance Method Summary collapse
- #__fixme__ ⇒ Object
- #acknowledges(context) ⇒ Object
- #assumes(description, &b) ⇒ Object
- #broken(message) ⇒ Object
- #ensures(description, &b) ⇒ Object
-
#initialize(description) ⇒ Contractor
constructor
A new instance of Contractor.
- #watches(name, &b) ⇒ Object
- #work ⇒ Object
Constructor Details
#initialize(description) ⇒ Contractor
Returns a new instance of Contractor.
19 20 21 22 23 24 25 |
# File 'lib/s2p/contractor.rb', line 19 def initialize(description) @description = description @assumptions = [] @assurances = [] @observations = {} @context = "" end |
Instance Attribute Details
#observations ⇒ Object (readonly)
Returns the value of attribute observations.
33 34 35 |
# File 'lib/s2p/contractor.rb', line 33 def observations @observations end |
Class Method Details
.[](description) ⇒ Object
7 8 9 |
# File 'lib/s2p/contractor.rb', line 7 def self.[](description) self.for(description) end |
.conditions_disabled? ⇒ Boolean
15 16 17 |
# File 'lib/s2p/contractor.rb', line 15 def self.conditions_disabled? !!( @conditions_disabled || ENV["SKIP_CONTRACT_ENFORCEMENT"] ) end |
.disable_enforcement ⇒ Object
11 12 13 |
# File 'lib/s2p/contractor.rb', line 11 def self.disable_enforcement @conditions_disabled = true end |
.for(description) ⇒ Object
3 4 5 |
# File 'lib/s2p/contractor.rb', line 3 def self.for(description) new(description) end |
Instance Method Details
#__fixme__ ⇒ Object
45 46 47 |
# File 'lib/s2p/contractor.rb', line 45 def __fixme__ broken("Not implemented yet") end |
#acknowledges(context) ⇒ Object
35 36 37 38 39 |
# File 'lib/s2p/contractor.rb', line 35 def acknowledges(context) @context << "[#{context}]" self end |
#assumes(description, &b) ⇒ Object
49 50 51 52 53 |
# File 'lib/s2p/contractor.rb', line 49 def assumes(description, &b) @assumptions << [description, b] self end |
#broken(message) ⇒ Object
41 42 43 |
# File 'lib/s2p/contractor.rb', line 41 def broken() raise unless self.class.conditions_disabled? end |
#ensures(description, &b) ⇒ Object
55 56 57 58 59 |
# File 'lib/s2p/contractor.rb', line 55 def ensures(description, &b) @assurances << [description, b] self end |
#watches(name, &b) ⇒ Object
27 28 29 30 31 |
# File 'lib/s2p/contractor.rb', line 27 def watches(name, &b) @observations[name] = { action: b } self end |
#work ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/s2p/contractor.rb', line 61 def work(*) return yield(*) if self.class.conditions_disabled? @assumptions.each do |description, condition| if @context != "" fail "Failed expectation: [when #{@context}] #{description} (in #{@description})" unless condition.call(*) else fail "Failed expectation: #{description} (in #{@description})" unless condition.call(*) end end @observations.each do |, diff| diff[:before] = diff[:action].call end result = yield(*) @observations.each do |, diff| diff[:after] = diff[:action].call end @assurances.each do |description, condition| if @context != "" fail "Failed expectation: #{@context} #{description} (in #{@description})" unless condition.call(result, observations) else fail "Failed expectation: #{description} (in #{@description})" unless condition.call(result, observations) end end result end |