Class: Solid::Result::Contract::Evaluator
- Inherits:
-
Object
- Object
- Solid::Result::Contract::Evaluator
- Includes:
- Interface
- Defined in:
- lib/solid/result/contract/evaluator.rb
Instance Attribute Summary collapse
-
#allowed_types ⇒ Object
readonly
Returns the value of attribute allowed_types.
-
#failure ⇒ Object
readonly
Returns the value of attribute failure.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#initialize(success, failure) ⇒ Evaluator
constructor
A new instance of Evaluator.
- #type!(type) ⇒ Object
- #type?(type) ⇒ Boolean
- #type_and_value!(data) ⇒ Object
Constructor Details
#initialize(success, failure) ⇒ Evaluator
Returns a new instance of Evaluator.
9 10 11 12 13 14 |
# File 'lib/solid/result/contract/evaluator.rb', line 9 def initialize(success, failure) @success = success @failure = failure @allowed_types = (success.allowed_types | failure.allowed_types).freeze end |
Instance Attribute Details
#allowed_types ⇒ Object (readonly)
Returns the value of attribute allowed_types.
7 8 9 |
# File 'lib/solid/result/contract/evaluator.rb', line 7 def allowed_types @allowed_types end |
#failure ⇒ Object (readonly)
Returns the value of attribute failure.
7 8 9 |
# File 'lib/solid/result/contract/evaluator.rb', line 7 def failure @failure end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
7 8 9 |
# File 'lib/solid/result/contract/evaluator.rb', line 7 def success @success end |
Instance Method Details
#type!(type) ⇒ Object
25 26 27 28 29 |
# File 'lib/solid/result/contract/evaluator.rb', line 25 def type!(type) return type if type?(type) raise Contract::Error::UnexpectedType.build(type: type, allowed_types: allowed_types) end |
#type?(type) ⇒ Boolean
16 17 18 19 20 21 22 23 |
# File 'lib/solid/result/contract/evaluator.rb', line 16 def type?(type) success_disabled = success == Contract::Disabled failure_disabled = failure == Contract::Disabled return Contract::Disabled.type?(type) if success_disabled && failure_disabled (!success_disabled && success.type?(type)) || (!failure_disabled && failure.type?(type)) end |
#type_and_value!(data) ⇒ Object
31 32 33 |
# File 'lib/solid/result/contract/evaluator.rb', line 31 def type_and_value!(data) self.for(data).type_and_value!(data) end |