Skip to content

Commit

Permalink
avoid dynamically defnining constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Bulat Shakirzyanov committed Dec 29, 2013
1 parent c985401 commit b745f85
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/uv/listener.rb
Expand Up @@ -2,25 +2,27 @@

module UV
module Listener
private
def callbacks
@callbacks ||= Set.new
@@callbacks = Hash.new { |hash, object_id| hash[object_id] = Hash.new }

class << self
def define_callback(object_id, name, callback)
@@callbacks[object_id][name] ||= callback
end

def undefine_callbacks(object_id)
@@callbacks.delete(object_id)
nil
end
end

private

def callback(name)
const_name = "#{name.upcase}_#{object_id}"
unless self.class.const_defined?(const_name)
callbacks << const_name
self.class.const_set(const_name, method(name))
end
self.class.const_get(const_name)
Listener.define_callback(object_id, name, method(name))
end

def clear_callbacks
callbacks.each do |name|
self.class.send(:remove_const, name)
end
callbacks.clear
Listener.undefine_callbacks(object_id)
end
end
end

0 comments on commit b745f85

Please sign in to comment.