Class: DataModel::Registry

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

Overview

Registry allows for different type implementations to be used by the scanner. It also acts as an error message registry, mostly for pragmatic reasons.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types: self.class.default_types, errors: self.class.default_error_messages) ⇒ Registry

Instanciate a new type registry. Default errors will always be used, but additional errors can be registered.

Parameters:

  • types (Hash) (defaults to: self.class.default_types)

    the type map to use

  • errors (Hash) (defaults to: self.class.default_error_messages)

    the error message map to use



38
39
40
41
42
43
44
45
46
47
# File 'lib/data_model/registry.rb', line 38

def initialize(types: self.class.default_types, errors: self.class.default_error_messages)
	@error_messages = nil

	if errors
		errors.each { |type, builder| register_error_message(type, &builder) }
	end

	@types = {}
	types.each { |(name, type)| register(name, type) }
end

Class Method Details

.default_error_messagesHash

Default error messages that will be used if alternative error messages are not given

Returns:

  • (Hash)

    the default error messages



13
14
15
# File 'lib/data_model/registry.rb', line 13

def self.default_error_messages
	Errors.error_messages
end

.default_typesHash

Default types that will be used if alternative type map is not given

Returns:

  • (Hash)

    the default type map



7
8
9
# File 'lib/data_model/registry.rb', line 7

def self.default_types
	Builtin.types
end

.instance(types: default_types, errors: default_error_messages) ⇒ Registry

Singleton instance that will be used globally unless instances given

Parameters:

  • types (Hash) (defaults to: default_types)

    the type map to use

  • errors (Hash) (defaults to: default_error_messages)

    the error message map to use

Returns:



21
22
23
# File 'lib/data_model/registry.rb', line 21

def self.instance(types: default_types, errors: default_error_messages)
	@instance ||= new(types:, errors:)
end

.register(name, type) ⇒ void

This method returns an undefined value.

Register a type on the global instance

Parameters:

  • name (Symbol)

    the name of the type

  • type (Type)

    the type to register



29
30
31
# File 'lib/data_model/registry.rb', line 29

def self.register(name, type)
	instance.register(name, type)
end

Instance Method Details

#error_message(error) ⇒ String

Build the error message for a given error

Parameters:

  • error (Error)

    the error to build the message for

Returns:

  • (String)

    the error message

Raises:

  • (RuntimeError)

    if no error message builder is registered for the error type



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/data_model/registry.rb', line 107

def error_message(error)
	type = error[0]
	ctx = error[1]

	builder = error_message_builders[type]

	if builder.nil?
		raise "no error message builder for #{type}"
	end

	builder.call(ctx)
end

#error_message_buildersHash

Get the error message builders

Returns:

  • (Hash)

    the error message builders



95
96
97
98
99
100
101
# File 'lib/data_model/registry.rb', line 95

def error_message_builders
	if @error_messages.nil?
		@error_messages ||= {}
	end

	@error_messages
end

#error_messages(error) ⇒ Hash

Build error messages from error object

Parameters:

  • error (Error)

    the error to build the messages for

Returns:

  • (Hash)

    the error messages



123
124
125
126
127
# File 'lib/data_model/registry.rb', line 123

def error_messages(error)
	error.to_h.transform_values do |error_list|
		error_list.map { |e| error_message(e) }
	end
end

#register(name, type) ⇒ void

This method returns an undefined value.

Register a type on this instance

Parameters:

  • name (Symbol)

    the name of the Type

  • type (Type)

    the type to register



53
54
55
# File 'lib/data_model/registry.rb', line 53

def register(name, type)
	@types[name] = type
end

#register_error_message(type, &block) ⇒ void

This method returns an undefined value.

Register a custom error message for use with custom errors

Parameters:

  • type (Symbol)

    the type of error to register

  • block (Proc)

    the block to use to build the error message, shoudl take the error context and return a string



89
90
91
# File 'lib/data_model/registry.rb', line 89

def register_error_message(type, &block)
	error_message_builders[type] = block
end

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

Access and configure registered type

Parameters:

  • name (Symbol)

    the name of the Type

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

    the arguments to pass to the Type

  • params (Array) (defaults to: nil)

    the parameters to configure the Type with

Returns:

  • (Type)

    the configured type



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/data_model/registry.rb', line 69

def type(name, args: {}, params: nil)
	if !type?(name)
		raise "#{name} is not registered as a type"
	end

	t = @types.fetch(name).new(args, registry: self)

	if params
		t.configure(params)
	end

	return t
end

#type?(name) ⇒ Boolean

Check if a type is registered

Parameters:

  • name (Symbol)

    the name of the type

Returns:

  • (Boolean)

    whether the type is registered



60
61
62
# File 'lib/data_model/registry.rb', line 60

def type?(name)
	@types.key?(name)
end