Exception: Lawyer::BrokenContract
- Inherits:
-
StandardError
- Object
- StandardError
- Lawyer::BrokenContract
- Defined in:
- lib/lawyer/broken_contract.rb
Instance Method Summary collapse
- #explain_violations(violations, type) ⇒ Object
-
#initialize(subject, contract, violations) ⇒ BrokenContract
constructor
A new instance of BrokenContract.
- #methods(count) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(subject, contract, violations) ⇒ BrokenContract
Returns a new instance of BrokenContract.
3 4 5 6 7 8 9 |
# File 'lib/lawyer/broken_contract.rb', line 3 def initialize(subject, contract, violations) @subject = subject.name @contract = contract.name @method_missing_violations = violations.select { |v| v.is_a?(MethodMissingViolation) } @wrong_arity_violations = violations.select { |v| v.is_a?(WrongArityViolation) } @wrong_signature_violations = violations.select { |v| v.is_a?(WrongSignatureViolation) } end |
Instance Method Details
#explain_violations(violations, type) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lawyer/broken_contract.rb', line 23 def explain_violations(violations, type) str = "" if violations.any? count = violations.count str << "\t(#{count} #{methods(count)} #{type})\n" str << violations.map(&:to_s).join("\n") str << "\n" end str end |
#methods(count) ⇒ Object
34 35 36 |
# File 'lib/lawyer/broken_contract.rb', line 34 def methods(count) count == 1 ? "method" : "methods" end |
#to_s ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lawyer/broken_contract.rb', line 11 def to_s str = "#{@subject} does not implement <#{@contract}>\n" str << explain_violations(@method_missing_violations, "missing") str << "\n" if @method_missing_violations.any? && (@wrong_arity_violations.any? || @wrong_signature_violations.any?) str << explain_violations(@wrong_arity_violations, "with the wrong arity") str << "\n" if (@method_missing_violations.any? || @wrong_arity_violations.any?) && @wrong_signature_violations.any? str << explain_violations(@wrong_signature_violations, "with the wrong signature") str end |