module RSchema
Schema-based validation and coercion
Constants
- VERSION
Public Class Methods
@return The default DSL
object. @see DefaultDSL
# File lib/rschema.rb, line 119 def self.default_dsl @default_dsl ||= DefaultDSL.new end
Creates a schema object using a DSL
@param dsl (see .dsl_eval) @yield (see .dsl_eval) @return [Schemas::Convenience] The schema object returned from the block,
wrapped in a {Schemas::Convenience}.
@example (see Schemas::FixedHash
)
# File lib/rschema.rb, line 26 def self.define(dsl = nil, &block) schema = dsl_eval(dsl, &block) Schemas::Convenience.wrap(schema) end
A convenience method for creating {Schemas::FixedHash} schemas
This method is a shorter way to write:
RSchema.define do fixed_hash(...) end
@yield (see .dsl_eval) @yieldreturn The attributes of the hash schema
(the argument to {DSL#fixed_hash}).
@return [Schemas::Convenience] A {Schemas::FixedHash} schema wrapped in a
{Schemas::Convenience}.
@example A typical fixed hash schema
person_schema = RSchema.define_hash {{ name: _String, age: _Integer, }}
# File lib/rschema.rb, line 81 def self.define_hash(&block) Schemas::Convenience.wrap( default_dsl.fixed_hash(dsl_eval(&block)), ) end
A convenience method for creating {Schemas::Predicate} schemas.
This method is a shorter way to write:
RSchema.define do predicate(name) { ... } end
@param name (see DSL#predicate
) @yield (see DSL#predicate
) @yieldreturn (see DSL#predicate
) @return [Schemas::Convenience] A {Schemas::Predicate} schema wrapped in a
{Schemas::Convenience}.
@example A predicate schema that only allows `odd?` objects.
odd_schema = RSchema.define_predicate('odd') do |x| x.odd? end
@see DSL#predicate
# File lib/rschema.rb, line 109 def self.define_predicate(name = nil, &block) Schemas::Convenience.wrap( default_dsl.predicate(name, &block), ) end
Runs a block using a DSL
.
@param dsl [Object] An optional DSL
object to run the block with.
Uses {RSchema#default_dsl} if nil.
@yield Invokes the given block with access to the methods on `dsl`. @return The return value of the given block (usually some kind of schema
object)
@example Creating a typical fixed hash schema
person_schema = RSchema.dsl_eval do fixed_hash( name: _String, age: _Integer, ) end
# File lib/rschema.rb, line 48 def self.dsl_eval(dsl = nil, &block) if block.nil? raise ArgumentError, 'Must provide a block for the RSchema DSL' end Docile::Execution.exec_in_proxy_context( dsl || default_dsl, Docile::FallbackContextProxy, &block ) end