Skip to content

Instantly share code, notes, and snippets.

@jeremyruppel
Created January 3, 2011 03:21
Show Gist options
  • Save jeremyruppel/763083 to your computer and use it in GitHub Desktop.
Save jeremyruppel/763083 to your computer and use it in GitHub Desktop.
expanding on stray's idea for context event mapping synopsis
#!/usr/bin/env ruby -wKU
require "yaml"
=begin
TODO this could all use a little DRYing up
=end
class Event
TYPE_PATTERN = /public\s+static\s+(?:const|var)\s+([A-Z_]+)\s+:\s+String\s+=\s+(.*?);/
CLASS_PATTERN = /public\s+class\s+(\w+)\s+extends\s+Event/
PACKAGE_PATTERN = /package\s+([\w\d\.]+)/
attr_reader :types, :class_name, :fqcn
def initialize( file )
@types = file.scan( TYPE_PATTERN ).map { |t| { :type => t[ 0 ], :string => t[ 1 ] } } || [ ]
@class_name = file[ CLASS_PATTERN, 1 ]
@fqcn = file[ PACKAGE_PATTERN, 1 ] << ".#{@class_name}"
end
end
=begin
TODO this could all use a little DRYing up
=end
class Mediator
EVENT_MAP_PATTERN = /mapListener\(\s*eventDispatcher\s*,\s*(\w+Event)\.([A-Z_]+)\s*,\s*(\w+)\s*\)/
CONTEXT_LISTENER_PATTERN = /addContextListener\(\s*eventDispatcher\s*,\s*(\w+Event)\.([A-Z_]+)\s*,\s*(\w+)\s*\)/
DISPATCHER_LISTENER_PATTERN = /eventDispatcher.addEventListener\(\s*(\w+Event)\.([A-Z_]+)\s*,\s*(\w+)\s*\)/
CLASS_PATTERN = /public\s+class\s+(\w+)/
PACKAGE_PATTERN = /package\s+([\w\d\.]+)/
attr_reader :handlers, :class_name, :fqcn
def initialize( file )
@handlers = patterns.map { |p| file.scan( p ).map { |h| { :event => h[ 0 ], :type => h[ 1 ], :handler => h[ 2 ] } } }.flatten
@class_name = file[ CLASS_PATTERN, 1 ]
@fqcn = file[ PACKAGE_PATTERN, 1 ] << ".#{@class_name}"
end
def patterns
[ EVENT_MAP_PATTERN, CONTEXT_LISTENER_PATTERN, DISPATCHER_LISTENER_PATTERN ]
end
end
if __FILE__ == $PROGRAM_NAME
# TODO specify source_dir from ARGV? STDIN?
source_dir = 'src'
# set up our collections
@events = [ ]
@mediators = [ ]
# iterate over matching files
Dir[ "#{source_dir}/**/*{Event,Mediator}.as" ].each do |uri|
if uri[ /Mediator\.as$/ ]
@mediators << Mediator.new( IO.read( uri ) )
end
if uri[ /Event\.as$/ ]
@events << Event.new( IO.read( uri ) )
end
end
# by mediator:
puts '-' * 50
puts 'By Mediator:'
puts @mediators.select{ |m| m.handlers.any? }.to_yaml
# by event:
puts '-' * 50
puts 'By Event:'
# TODO
end
@Stray
Copy link

Stray commented Jan 3, 2011

Thanks Jeremy - this is excellent!

I'd written it just as a simple script because I'm learning Python and I wanted a proper task to translate ruby basics (read a file line-by-line for example) into python basics. I needed to trace out each step as I went to be sure that I'd recreated the behaviour between the two languages - for example finding Mediator / Event files.

When I run this against my project I only find a fraction of the mappings that the original finds, and also those mappings (by mediator) aren't then classified by eventClass (the eventClass is repeated rather than the types being 'children' of the eventClass). But I'll try to work out why that's happening! Nice to see it rubified - I only ever hack in Ruby, haven't yet had a reason to learn best practices from scratch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment