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.
22 23 24 |
# File 'lib/rschema/schemas/convenience.rb', line 22 def initialize() super end |
Class Method Details
.unwrap(schema) ⇒ schema
Removes any RSchema::Schemas::Convenience wrappers from a schema
126 127 128 129 |
# File 'lib/rschema/schemas/convenience.rb', line 126 def self.unwrap(schema) schema = schema. while schema.is_a?(self) schema end |
.wrap(schema) ⇒ Convenience
Wraps the given schema in a RSchema::Schemas::Convenience, if it isn’t already wrapped.
112 113 114 115 116 117 118 |
# File 'lib/rschema/schemas/convenience.rb', line 112 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
56 57 58 59 60 61 62 63 |
# File 'lib/rschema/schemas/convenience.rb', line 56 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?
102 103 104 |
# File 'lib/rschema/schemas/convenience.rb', line 102 def invalid?(value) !valid?(value) end |
#underlying_schema ⇒ schema
Returns the underlying schema object.
27 28 29 |
# File 'lib/rschema/schemas/convenience.rb', line 27 def __getobj__ end |
#valid?(value) ⇒ Boolean
Checks whether a value is valid or not
92 93 94 95 |
# File 'lib/rschema/schemas/convenience.rb', line 92 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.
42 43 44 |
# File 'lib/rschema/schemas/convenience.rb', line 42 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
77 78 79 80 81 82 83 84 |
# File 'lib/rschema/schemas/convenience.rb', line 77 def validate!(value, = Options.default) result = .call(value, ) if result.valid? result.value else raise RSchema::Invalid, result.error end end |