Class: DataModel::Type Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/data_model/type.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, registry: Registry.instance) ⇒ void

Parameters:

  • args (Hash)

    type arguments, configures the reading process

  • registry (Registry) (defaults to: Registry.instance)

    the registry to use



11
12
13
14
# File 'lib/data_model/type.rb', line 11

def initialize(args, registry: Registry.instance)
	@type_args = args
	@type_registry = registry
end

Instance Attribute Details

#type_argsHash (readonly)

Returns the type arguments.

Returns:

  • (Hash)

    the type arguments



17
18
19
# File 'lib/data_model/type.rb', line 17

def type_args
  @type_args
end

#type_registryRegistry (readonly)

Returns the type Registry.

Returns:



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.

Parameters:

  • params (Array)

    type parameters, configures the type itself



25
# File 'lib/data_model/type.rb', line 25

def configure(params); end

#instantiate(name, args: {}, params: nil) ⇒ Type

instanciate another type by name

Parameters:

  • name (Symbol)

    the name of the type to instantiate

  • args (Hash) (defaults to: {})

    type arguments, configures the reading Process

  • params (Array) (defaults to: nil)

    type parameters, configures the type itself

Returns:

  • (Type)

    the instantiated type



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

Parameters:

  • name (Symbol)

    the name of the type to invoke

  • val (Object)

    the value to read

  • coerce (Boolean) (defaults to: false)

    whether to coerce the value

  • args (Hash) (defaults to: {})

    type arguments, configures the reading process

  • params (Array) (defaults to: nil)

    type parameters, configures the type itself

Returns:

  • (Array(Object, Error))

    the result of reading the value



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)

This method is abstract.

default reader, must be overridden for a type to be useful

Returns the result of reading the value.

Parameters:

  • data (untyped)

    the data to read

  • coerce (Boolean) (defaults to: false)

    whether to coerce the value

Returns:

  • (Array(Object, Error))

    the result of reading the value



57
# File 'lib/data_model/type.rb', line 57

def read(data, coerce: false); end

#type_nameString

name of the type without module prefix as a string useful for generating error messages

Returns:

  • (String)

    the type name



62
63
64
# File 'lib/data_model/type.rb', line 62

def type_name
	@type_name ||= self.class.name.split("::").last
end