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.
323 324 325 326 327 328 329 330 331 |
# File 'lib/rschema/dsl.rb', line 323 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.
253 254 255 |
# File 'lib/rschema/dsl.rb', line 253 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
55 56 57 58 59 60 61 62 63 |
# File 'lib/rschema/dsl.rb', line 55 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.
160 161 162 163 164 165 166 |
# File 'lib/rschema/dsl.rb', line 160 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
72 73 74 |
# File 'lib/rschema/dsl.rb', line 72 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.
279 280 281 |
# File 'lib/rschema/dsl.rb', line 279 def convenience(schema) Schemas::Convenience.wrap(schema) end |
#either(*subschemas) ⇒ Schemas::Sum
Creates a Schemas::Sum schema.
207 208 209 210 |
# File 'lib/rschema/dsl.rb', line 207 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
193 194 195 196 |
# File 'lib/rschema/dsl.rb', line 193 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
87 88 89 |
# File 'lib/rschema/dsl.rb', line 87 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.
312 313 314 |
# File 'lib/rschema/dsl.rb', line 312 def inconvenience(schema) Schemas::Convenience.unwrap(schema) end |
#maybe(subschema) ⇒ Schemas::Maybe
Creates a Schemas::Maybe schema
177 178 179 |
# File 'lib/rschema/dsl.rb', line 177 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.
117 118 119 |
# File 'lib/rschema/dsl.rb', line 117 def optional(key) OptionalWrapper.new(key) end |
#pipeline(*subschemas) ⇒ Schemas::Pipeline
Creates a Schemas::Pipeline schema.
241 242 243 244 |
# File 'lib/rschema/dsl.rb', line 241 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.
228 229 230 |
# File 'lib/rschema/dsl.rb', line 228 def predicate(name = nil, &block) Schemas::Predicate.new(name, &block) end |
#set(subschema) ⇒ Schemas::Set
Creates a Schemas::Set schema
100 101 102 |
# File 'lib/rschema/dsl.rb', line 100 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.
40 41 42 |
# File 'lib/rschema/dsl.rb', line 40 def type(type) Schemas::Type.new(type) end |
#variable_hash(subschemas) ⇒ Schemas::VariableHash
Creates a Schemas::VariableHash schema
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rschema/dsl.rb', line 133 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 |