Class: RSchema::Schemas::Convenience
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- RSchema::Schemas::Convenience
- Defined in:
- lib/rschema/schemas/convenience.rb
Overview
A wrapper that provides convenience methods for schema objects.
Because this class inherits from ‘SimpleDelegator`, convenience wrappers behave like their underlying schemas. That is, you can call methods on the underlying schema object through the convenience wrapper.
Schema objects only need to implement the ‘call` method to validate values. This small interface is simple for schema classes to implement, but not very descriptive when actually using the schema objects. So, to make schema objects nicer to use, this class provides a variety of more-descriptive methods like #validate, #validate!, #valid?, and #invalid?.
Class Method Summary collapse
-
.unwrap(schema) ⇒ schema
Removes any Convenience wrappers from a schema.
-
.wrap(schema) ⇒ Convenience
Wraps the given schema in a Convenience, if it isn’t already wrapped.
Instance Method Summary collapse
-
#error_for(value, options = Options.default) ⇒ Object
Returns the validation error for the given value.
-
#initialize(underlying_schema) ⇒ Convenience
constructor
A new instance of Convenience.
-
#invalid?(value) ⇒ Boolean
The opposite of #valid?.
-
#underlying_schema ⇒ schema
The underlying schema object.
-
#valid?(value) ⇒ Boolean
Checks whether a value is valid or not.
-
#validate(value, options = Options.default) ⇒ RSchema::Result
Applies the schema to a value.
-
#validate!(value, options = Options.default) ⇒ Object
Applies the schema to a value, raising an exception if the value is invalid.
Constructor Details
#initialize(underlying_schema) ⇒ Convenience
Returns a new instance of Convenience.
21 22 23 |
# File 'lib/rschema/schemas/convenience.rb', line 21 def initialize() super end |
Class Method Details
.unwrap(schema) ⇒ schema
Removes any RSchema::Schemas::Convenience wrappers from a schema
124 125 126 127 128 129 |
# File 'lib/rschema/schemas/convenience.rb', line 124 def self.unwrap(schema) while schema.is_a?(self) schema = schema. end schema end |
.wrap(schema) ⇒ Convenience
Wraps the given schema in a RSchema::Schemas::Convenience, if it isn’t already wrapped.
110 111 112 113 114 115 116 |
# File 'lib/rschema/schemas/convenience.rb', line 110 def self.wrap(schema) if schema.is_a?(self) schema else new(schema) end end |
Instance Method Details
#error_for(value, options = Options.default) ⇒ Object
Returns the validation error for the given value
55 56 57 58 59 60 61 62 |
# File 'lib/rschema/schemas/convenience.rb', line 55 def error_for(value, =Options.default) result = .call(value, ) if result.valid? nil else result.error end end |
#invalid?(value) ⇒ Boolean
The opposite of #valid?
100 101 102 |
# File 'lib/rschema/schemas/convenience.rb', line 100 def invalid?(value) not valid?(value) end |
#underlying_schema ⇒ schema
Returns the underlying schema object.
26 27 28 |
# File 'lib/rschema/schemas/convenience.rb', line 26 def __getobj__ end |
#valid?(value) ⇒ Boolean
Checks whether a value is valid or not
90 91 92 93 |
# File 'lib/rschema/schemas/convenience.rb', line 90 def valid?(value) result = .call(value, Options.fail_fast) result.valid? end |
#validate(value, options = Options.default) ⇒ RSchema::Result
Applies the schema to a value
This is that same as the ‘call` method available on all schema objects, except that the `options` param is optional.
41 42 43 |
# File 'lib/rschema/schemas/convenience.rb', line 41 def validate(value, =Options.default) call(value, ) end |
#validate!(value, options = Options.default) ⇒ Object
Applies the schema to a value, raising an exception if the value is invalid
75 76 77 78 79 80 81 82 |
# File 'lib/rschema/schemas/convenience.rb', line 75 def validate!(value, =Options.default) result = .call(value, ) if result.valid? result.value else raise RSchema::Invalid.new(result.error) end end |