Class: Solid::Result::Contract::ForTypes

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Returns:

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