Module: DataModel::Scanner

Extended by:
Scanner
Includes:
Logging
Included in:
Scanner
Defined in:
lib/data_model/scanner.rb

Overview

The scanner is responsible for scanning a schema into a data structure that is easier to work with.

schema eg: [:string, { min: 1, max: 10}] [:tuple, { title: “coordinates” }, :double, :double] [:hash, { open: false }, [:first_name, :string] [:last_name, :string]]

first param is type, which is a key lookup in the registry second param is args, this is optional, but is a way to configure a type rest are type params. these are used to configure a type at the point of instantiation. Think of them as generics.

params are either symbol, for example tuple types

array, for object types to configure child properties.

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#scan(schema, registry = Registry.instance) ⇒ Node

Scan a schema, which is defined as a data structure, into a struct that is easier to work with. “Syntax” validations will be enforced at this level.

Parameters:

  • schema (Array)

    the schema to scan

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

    the registry to use

Returns:

  • (Node)

    the scanned node



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/data_model/scanner.rb', line 30

def scan(schema, registry = Registry.instance)
	# state:
	# 	nil (start) -> :type (we have a type) -> :args (we have arguments)
	scanned = Node.new
	state = nil

	log.debug("scanning schema: #{schema.inspect}")

	for pos in (0...schema.length)
		token = schema[pos]
		dbg = "pos: #{pos}, token: #{token.inspect}, state: #{state.inspect}"
		log.debug(dbg)

		# detect optional args missing
		if !token.is_a?(Hash) && state == :type
			log.debug("detected optional args missing at (#{dbg}), moving state to :args")

			# move state forward
			state = :args
		end

		# we are just collecting params at this point
		if state == :args

			if !token.is_a?(Array) && !token.is_a?(Symbol)
				raise "expected type params at (#{dbg}), which should be either a symbol or an array"
			end

			scanned.params ||= []
			scanned.params << token
			log.debug("collecting params at (#{dbg})")

			next
		end

		# we can determine meaning based on type and state
		case token
		when Symbol
			if !state.nil?
				raise "got a symbol at(#{dbg}), but validator already defined"
			end

			if !registry.type?(token)
				# TODO: need a much better error here, this is what people see when registration is not there
				raise "expected a type in (#{dbg}), but found #{token.inspect} which is not a registered type"
			end

			scanned.type = token
			state = :type
			log.debug("got a symbol, determined token is a type at (#{dbg}), moving state to :type")

		when Hash
			if state != :type
				raise "got a hash at (#{dbg}), but state is not :type (#{state.inspect})"
			end

			scanned.args = token
			state = :args
			log.debug("got a hash, determined token is args at (#{dbg}), moving state to :args")

		else
			raise "got token #{token.inspect} at (#{dbg}) which was unexpected given the scanner was in a state of #{state}"
		end
	end

	return scanned
end