Class: Astel::Dispatcher
- Inherits:
-
Object
- Object
- Astel::Dispatcher
- Defined in:
- lib/astel/dispatcher.rb
Defined Under Namespace
Classes: CallbackList, Traversal
Constant Summary collapse
- NODE_CLASSES =
Prism.constants.filter_map do |constant| value = Prism.const_get(constant) next unless value.is_a?(Class) && value < Prism::Node [NodeType.from_class_name(constant.to_s), value] rescue NameError nil end.to_h.freeze
- NODE_TYPES =
NODE_CLASSES.keys.to_set.freeze
Instance Method Summary collapse
-
#initialize ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #on(node_type, callable = nil, &block) ⇒ Object
- #run(ast) ⇒ Object
Constructor Details
#initialize ⇒ Dispatcher
Returns a new instance of Dispatcher.
85 86 87 |
# File 'lib/astel/dispatcher.rb', line 85 def initialize @callbacks = {} end |
Instance Method Details
#on(node_type, callable = nil, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/astel/dispatcher.rb', line 89 def on(node_type, callable = nil, &block) callback = callable || block raise ArgumentError, 'callback is required' unless callback type = node_type.to_sym raise ArgumentError, "unknown Prism node type: #{node_type}" unless NODE_TYPES.include?(type) raise ArgumentError, 'callback must respond to call' unless callback.respond_to?(:call) register_callback(type, callback) self end |
#run(ast) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/astel/dispatcher.rb', line 101 def run(ast) return self unless ast Traversal.new(@callbacks).run(ast) self end |