Class: RSchema::Schemas::Enum
- Inherits:
-
Object
- Object
- RSchema::Schemas::Enum
- Defined in:
- lib/rschema/schemas/enum.rb
Overview
A schema that matches a values in a given set.
Instance Attribute Summary collapse
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#subschema ⇒ Object
readonly
Returns the value of attribute subschema.
Instance Method Summary collapse
- #call(value, options) ⇒ Object
-
#initialize(members, subschema) ⇒ Enum
constructor
A new instance of Enum.
- #with_wrapped_subschemas(wrapper) ⇒ Object
Constructor Details
#initialize(members, subschema) ⇒ Enum
Returns a new instance of Enum.
16 17 18 19 |
# File 'lib/rschema/schemas/enum.rb', line 16 def initialize(members, subschema) @members = members @subschema = subschema end |
Instance Attribute Details
#members ⇒ Object (readonly)
Returns the value of attribute members.
14 15 16 |
# File 'lib/rschema/schemas/enum.rb', line 14 def members @members end |
#subschema ⇒ Object (readonly)
Returns the value of attribute subschema.
14 15 16 |
# File 'lib/rschema/schemas/enum.rb', line 14 def subschema @subschema end |
Instance Method Details
#call(value, options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rschema/schemas/enum.rb', line 21 def call(value, ) subresult = subschema.call(value, ) if subresult.invalid? subresult elsif members.include?(subresult.value) subresult else Result.failure(Error.new( schema: self, value: subresult.value, symbolic_name: :not_a_member, )) end end |
#with_wrapped_subschemas(wrapper) ⇒ Object
36 37 38 |
# File 'lib/rschema/schemas/enum.rb', line 36 def with_wrapped_subschemas(wrapper) self.class.new(members, wrapper.wrap(subschema)) end |