Class: Solid::Result::Contract::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/solid/result/contract/evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typesObject (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

#failureObject (readonly)

Returns the value of attribute failure.



7
8
9
# File 'lib/solid/result/contract/evaluator.rb', line 7

def failure
  @failure
end

#successObject (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

Returns:

  • (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