Module: DataModel::Scanner

Extended by:
Scanner, T::Sig
Includes:
Logging, Kernel
Included in:
Scanner
Defined in:
lib/data_model/scanner.rb

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

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



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
97
98
99
100
101
# File 'lib/data_model/scanner.rb', line 36

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

	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 << 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