Class: DataModel::Type Abstract
- Inherits:
-
Object
- Object
- DataModel::Type
- Defined in:
- lib/data_model/type.rb
Overview
Base class for all types.
Types have arguments, which configure the act of reading / validating / coercing data. They also have parameters, which configure the type itself, such as generic or child specification Arguments are passed to the type when it is invoked, and parameters are passed to the type when it is configured. Parameters are really only used in complex types, such as Array or Hash. If you don’t need them, you can ignore them.
Direct Known Subclasses
Builtin::Array, Builtin::BigDecimal, Builtin::Boolean, Builtin::Date, Builtin::Float, Builtin::Hash, Builtin::Integer, Builtin::Numeric, Builtin::Object, Builtin::Or, Builtin::String, Builtin::Symbol, Builtin::Time
Instance Attribute Summary collapse
-
#type_args ⇒ Hash
readonly
The type arguments.
-
#type_registry ⇒ Registry
readonly
The type Registry.
Instance Method Summary collapse
-
#configure(params) ⇒ void
configure must be overridden to use params.
- #initialize(args, registry: Registry.instance) ⇒ void constructor
-
#instantiate(name, args: {}, params: nil) ⇒ Type
instanciate another type by name.
-
#invoke(name, val, coerce: false, args: {}, params: nil) ⇒ Array(Object, Error)
invoke another type by name.
-
#read(data, coerce: false) ⇒ Array(Object, Error)
abstract
The result of reading the value.
-
#type_name ⇒ String
name of the type without module prefix as a string useful for generating error messages.
Constructor Details
Instance Attribute Details
#type_args ⇒ Hash (readonly)
Returns the type arguments.
17 18 19 |
# File 'lib/data_model/type.rb', line 17 def type_args @type_args end |
#type_registry ⇒ Registry (readonly)
Returns the type Registry.
20 21 22 |
# File 'lib/data_model/type.rb', line 20 def type_registry @type_registry end |
Instance Method Details
#configure(params) ⇒ void
This method returns an undefined value.
configure must be overridden to use params. If you don’t need params, you can ignore this.
25 |
# File 'lib/data_model/type.rb', line 25 def configure(params); end |
#instantiate(name, args: {}, params: nil) ⇒ Type
instanciate another type by name
47 48 49 50 51 |
# File 'lib/data_model/type.rb', line 47 def instantiate(name, args: {}, params: nil) t = @type_registry.type(name, args:, params:) return t end |
#invoke(name, val, coerce: false, args: {}, params: nil) ⇒ Array(Object, Error)
invoke another type by name. This is useful for specifying types like UUIDs which are specialized strings
34 35 36 37 38 39 40 |
# File 'lib/data_model/type.rb', line 34 def invoke(name, val, coerce: false, args: {}, params: nil) t = instantiate(name, args:, params:) result = t.read(val, coerce:) return result end |
#read(data, coerce: false) ⇒ Array(Object, Error)
default reader, must be overridden for a type to be useful
Returns the result of reading the value.
57 |
# File 'lib/data_model/type.rb', line 57 def read(data, coerce: false); end |
#type_name ⇒ String
name of the type without module prefix as a string useful for generating error messages
62 63 64 |
# File 'lib/data_model/type.rb', line 62 def type_name @type_name ||= self.class.name.split("::").last end |