Class: Solid::Result::Contract::ForTypes
- Inherits:
-
Object
- Object
- Solid::Result::Contract::ForTypes
- Includes:
- Interface
- Defined in:
- lib/solid/result/contract/for_types.rb
Instance Attribute Summary collapse
-
#allowed_types ⇒ Object
readonly
Returns the value of attribute allowed_types.
Instance Method Summary collapse
-
#initialize(types) ⇒ ForTypes
constructor
A new instance of ForTypes.
- #type!(type) ⇒ Object
- #type?(type) ⇒ Boolean
- #type_and_value!(data) ⇒ Object
Constructor Details
#initialize(types) ⇒ ForTypes
Returns a new instance of ForTypes.
9 10 11 |
# File 'lib/solid/result/contract/for_types.rb', line 9 def initialize(types) @allowed_types = Array(types).map(&:to_sym).to_set.freeze end |
Instance Attribute Details
#allowed_types ⇒ Object (readonly)
Returns the value of attribute allowed_types.
7 8 9 |
# File 'lib/solid/result/contract/for_types.rb', line 7 def allowed_types @allowed_types end |
Instance Method Details
#type!(type) ⇒ Object
17 18 19 20 21 |
# File 'lib/solid/result/contract/for_types.rb', line 17 def type!(type) return type if type?(type) raise Contract::Error::UnexpectedType.build(type: type, allowed_types: allowed_types) end |
#type?(type) ⇒ Boolean
13 14 15 |
# File 'lib/solid/result/contract/for_types.rb', line 13 def type?(type) IgnoredTypes.include?(type) || allowed_types.member?(type) end |
#type_and_value!(data) ⇒ Object
23 24 25 26 27 |
# File 'lib/solid/result/contract/for_types.rb', line 23 def type_and_value!(data) type!(data.type) nil end |