Module: RSchema::DSL
- Included in:
- DefaultDSL
- Defined in:
- lib/rschema/dsl.rb
Overview
Do not include your custom DSL methods into this module. Include them into the DefaultDSL class instead.
A mixin containing all the standard RSchema DSL methods.
This mixin contains only the standard RSchema DSL methods, without any of the extra ones that may have been included by third-party gems/code.
Defined Under Namespace
Classes: OptionalWrapper
Instance Method Summary collapse
-
#anything ⇒ Schemas::Anything
Returns the Schemas::Anything schema.
-
#array(*subschemas) ⇒ Schemas::VariableLengthArray, Schemas::FixedLengthArray
Creates a Schemas::VariableLengthArray if given one argument, otherwise creates a Schemas::FixedLengthArray.
-
#attributes(attribute_hash) ⇒ Array<Schemas::FixedHash::Attribute>
Turns an “attribute hash” into an array of Schemas::FixedHash::Attribute.
-
#boolean ⇒ Schemas::Boolean
Returns the Schemas::Boolean schema.
-
#convenience(schema) ⇒ Schemas::Convenience
Wraps a schema in a Schemas::Convenience.
-
#either(*subschemas) ⇒ Schemas::Sum
Creates a Schemas::Sum schema.
-
#enum(valid_values, subschema = nil) ⇒ Schemas::Enum
Creates a Schemas::Enum schema.
-
#fixed_hash(attribute_hash) ⇒ Schemas::FixedHash
(also: #hash)
Creates a Schemas::FixedHash schema.
-
#inconvenience(schema) ⇒ schema
Removes any Schemas::Convenience wrappers from a schema.
-
#maybe(subschema) ⇒ Schemas::Maybe
Creates a Schemas::Maybe schema.
-
#method_missing(sym, *args, &block) ⇒ Object
Convenient way to create Schemas::Type schemas.
-
#optional(key) ⇒ OptionalWrapper
Wraps a key in an OptionalWrapper, for use with the #fixed_hash or #attributes methods.
-
#pipeline(*subschemas) ⇒ Schemas::Pipeline
Creates a Schemas::Pipeline schema.
-
#predicate(name = nil) {|value| ... } ⇒ Schemas::Predicate
Creates a Schemas::Predicate schema.
-
#set(subschema) ⇒ Schemas::Set
Creates a Schemas::Set schema.
-
#type(type) ⇒ Schemas::Type
Creates a Schemas::Type schema.
-
#variable_hash(subschemas) ⇒ Schemas::VariableHash
Creates a Schemas::VariableHash schema.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Convenient way to create Schemas::Type schemas
See #type for details.
330 331 332 333 334 335 336 337 338 |
# File 'lib/rschema/dsl.rb', line 330 def method_missing(sym, *args, &block) type = sym.to_s if type.start_with?('_') && args.empty? && block.nil? constant = Object.const_get(type[1..-1]) type(constant) else super end end |
Instance Method Details
#anything ⇒ Schemas::Anything
Returns the Schemas::Anything schema.
260 261 262 |
# File 'lib/rschema/dsl.rb', line 260 def anything Schemas::Anything.instance end |
#array(*subschemas) ⇒ Schemas::VariableLengthArray, Schemas::FixedLengthArray
Creates a Schemas::VariableLengthArray if given one argument, otherwise creates a Schemas::FixedLengthArray
57 58 59 60 61 62 63 64 65 |
# File 'lib/rschema/dsl.rb', line 57 def array(*subschemas) subschemas = subschemas.map { |ss| inconvenience(ss) } if subschemas.count == 1 Schemas::VariableLengthArray.new(subschemas.first) else Schemas::FixedLengthArray.new(subschemas) end end |
#attributes(attribute_hash) ⇒ Array<Schemas::FixedHash::Attribute>
Turns an “attribute hash” into an array of Schemas::FixedHash::Attribute. Primarily for use with Schemas::FixedHash#merge.
163 164 165 166 167 168 169 170 171 |
# File 'lib/rschema/dsl.rb', line 163 def attributes(attribute_hash) attribute_hash.map do |dsl_key, value_schema| optional = dsl_key.is_a?(OptionalWrapper) key = optional ? dsl_key.key : dsl_key Schemas::FixedHash::Attribute.new( key, inconvenience(value_schema), optional, ) end end |
#boolean ⇒ Schemas::Boolean
Returns the Schemas::Boolean schema
74 75 76 |
# File 'lib/rschema/dsl.rb', line 74 def boolean Schemas::Boolean.instance end |
#convenience(schema) ⇒ Schemas::Convenience
Wraps a schema in a Schemas::Convenience
It is not normally necessary to do this wrapping manually. Methods like RSchema.define, RSchema.define_predicate and RSchema.define_hash already return schema objects wrapped in Schemas::Convenience.
286 287 288 |
# File 'lib/rschema/dsl.rb', line 286 def convenience(schema) Schemas::Convenience.wrap(schema) end |
#either(*subschemas) ⇒ Schemas::Sum
Creates a Schemas::Sum schema.
214 215 216 217 |
# File 'lib/rschema/dsl.rb', line 214 def either(*subschemas) subschemas = subschemas.map { |ss| inconvenience(ss) } Schemas::Sum.new(subschemas) end |
#enum(valid_values, subschema = nil) ⇒ Schemas::Enum
Creates a Schemas::Enum schema
198 199 200 201 202 203 |
# File 'lib/rschema/dsl.rb', line 198 def enum(valid_values, subschema = nil) subschema = inconvenience(subschema) if subschema Schemas::Enum.new( valid_values, subschema || type(valid_values.first.class), ) end |
#fixed_hash(attribute_hash) ⇒ Schemas::FixedHash Also known as: hash
Creates a Schemas::FixedHash schema
89 90 91 |
# File 'lib/rschema/dsl.rb', line 89 def fixed_hash(attribute_hash) Schemas::FixedHash.new(attributes(attribute_hash)) end |
#inconvenience(schema) ⇒ schema
Removes any Schemas::Convenience wrappers from a schema.
This method is only really useful when defining your own custom DSL methods.
When creating a composite schema that contains other subschemas, it is unneccessary to have the subschemas wrapped in Schemas::Convenience. Using wrapped subschemas should not cause any errors, but unwrapped subschemas will have slightly better performance. So, when your custom DSL method is creating a composite schema, use #inconvenience to unwrap all the subschemas.
319 320 321 |
# File 'lib/rschema/dsl.rb', line 319 def inconvenience(schema) Schemas::Convenience.unwrap(schema) end |
#maybe(subschema) ⇒ Schemas::Maybe
Creates a Schemas::Maybe schema
182 183 184 |
# File 'lib/rschema/dsl.rb', line 182 def maybe(subschema) Schemas::Maybe.new(inconvenience(subschema)) end |
#optional(key) ⇒ OptionalWrapper
Wraps a key in an OptionalWrapper, for use with the #fixed_hash or #attributes methods.
119 120 121 |
# File 'lib/rschema/dsl.rb', line 119 def optional(key) OptionalWrapper.new(key) end |
#pipeline(*subschemas) ⇒ Schemas::Pipeline
Creates a Schemas::Pipeline schema.
248 249 250 251 |
# File 'lib/rschema/dsl.rb', line 248 def pipeline(*subschemas) subschemas = subschemas.map { |ss| inconvenience(ss) } Schemas::Pipeline.new(subschemas) end |
#predicate(name = nil) {|value| ... } ⇒ Schemas::Predicate
Creates a Schemas::Predicate schema.
235 236 237 |
# File 'lib/rschema/dsl.rb', line 235 def predicate(name = nil, &block) Schemas::Predicate.new(name, &block) end |
#set(subschema) ⇒ Schemas::Set
Creates a Schemas::Set schema
102 103 104 |
# File 'lib/rschema/dsl.rb', line 102 def set(subschema) Schemas::Set.new(inconvenience(subschema)) end |
#type(type) ⇒ Schemas::Type
Creates a Schemas::Type schema.
The preferred way to create type schemas is using an underscore, like:
_Integer
The DSL will turn the above code into:
type(Integer)
Underscores will not work for namespaced types (types that include ‘::`). In that case, it is necessary to use the `type` method.
42 43 44 |
# File 'lib/rschema/dsl.rb', line 42 def type(type) Schemas::Type.new(type) end |
#variable_hash(subschemas) ⇒ Schemas::VariableHash
Creates a Schemas::VariableHash schema
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rschema/dsl.rb', line 135 def variable_hash(subschemas) unless subschemas.is_a?(Hash) && subschemas.size == 1 raise ArgumentError, 'argument must be a Hash of size 1' end key_schema, value_schema = subschemas.first Schemas::VariableHash.new( inconvenience(key_schema), inconvenience(value_schema), ) end |