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.
75 76 77 |
# File 'lib/astel/dispatcher.rb', line 75 def initialize @callbacks = {} end |
Instance Method Details
#on(node_type, callable = nil, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/astel/dispatcher.rb', line 79 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) registered = @callbacks[type] if registered.is_a?(CallbackList) registered << callback elsif registered @callbacks[type] = CallbackList.new([registered, callback]) else @callbacks[type] = callback end self end |