Skip to content

Commit

Permalink
Generate event to handlers and handler to events mappings.
Browse files Browse the repository at this point in the history
The script generates two classes: `EventToHandlers` and HandlerToEvents
in the Rails `lib` directory so it's easier to see the big picture of
all subscriptions. It also helps to navigate between subscriptions and
events.

Execute the script using following command:
bin/rails r script/big_picture.rb
  • Loading branch information
stolarczykt committed Nov 9, 2023
1 parent 1823d5c commit 9f20404
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rails_application/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ yarn-debug.log*
.ruby-version
/app/assets/builds/*
!/app/assets/builds/.keep

# Event to handlers and handler to events mappings generated by big_picture.rb script
/lib/event_to_handlers.rb
/lib/handler_to_events.rb
107 changes: 107 additions & 0 deletions rails_application/script/big_picture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

class GenerateFiles
EVENT_TO_HANDLERS_FILE = "lib/event_to_handlers.rb"
HANDLER_TO_EVENTS_FILE = "lib/handler_to_events.rb"

def generate_event_to_handlers
File.open(EVENT_TO_HANDLERS_FILE, "w") do |f|
f.puts event_to_handlers_header

all_event_types.sort.each do |event_type|
f.puts indent("#{event_type} => [", 6)

event_store
.subscribers_for(event_type)
.each { |subscription| f.puts indent("#{handler_name(subscription)}, # #{handler_type(subscription)}", 8) }
f.puts indent("],", 6)
end

f.print footer
end
end

def generate_handler_to_events
handler_to_events =
all_event_types.each_with_object({}) do |event_type, acc|
event_store
.subscribers_for(event_type)
.each do |subscription|
acc[subscription] ||= []
acc[subscription] << event_type
end
end

File.open("lib/handler_to_events.rb", "w") do |f|
f.puts handler_to_events_header

handler_to_events
.keys
.sort_by { |h| handler_name(h) }
.each do |handler|
f.puts indent("#{handler_name(handler)} => [", 6)
handler_to_events[handler].sort.each { |event_type| f.puts indent("#{event_type},", 8) }
f.puts indent("],", 6)
end

f.print footer
end
end

def handler_name(handler)
case handler
when Class
handler.name
when Method
"#{handler.receiver}.method(:#{handler.original_name})"
else
handler.class.name
end
end

def all_event_types
::Infra::Event.descendants.map(&:name)
end

def handler_type(subscription)
subscription.respond_to?(:perform_async) ? "async" : "SYNC"
end

def event_store
Rails.configuration.event_store
end

def handler_to_events_header
<<~EOF
class HandlerToEvents
# File autogenerated with script/big_picture.rb
def events
{
EOF
end

def event_to_handlers_header
<<~EOF
class EventToHandlers
# File autogenerated with script/big_picture.rb
def handlers
{
EOF
end

def footer
<<~EOF
}
end
end
EOF
end

def indent(str, spaces)
(" " * spaces) + str
end
end

script = GenerateFiles.new
script.generate_event_to_handlers
script.generate_handler_to_events

0 comments on commit 9f20404

Please sign in to comment.