~singpolyma/dhall-ruby

ref: e5560786a4ddbde7b6b1fc5e869bb3ef4e8b9126 dhall-ruby/lib/dhall/visitor.rb -rw-r--r-- 386 bytes
e5560786Stephen Paul Weber Allow imports to be either binary or source form 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

module Dhall
	class Visitor
		def initialize(callbacks)
			@callbacks = callbacks
		end

		def visit(expr)
			expr.to_h.each_with_object({}) do |(attr, value), h|
				if (callback = callback_for(value))
					h[attr] = callback.call(value)
				end
			end
		end

		protected

		def callback_for(x)
			@callbacks.find { |k, _| k === x }&.last
		end
	end
end