lib/flirt.rb in flirt-0.0.4 vs lib/flirt.rb in flirt-0.1.0
- old
+ new
@@ -9,11 +9,14 @@
class << self
def publish(event_name, event_data)
return if disabled
+ return if disabled_list && disabled_list.include?(event_name)
+ return if enabled_list && !enabled_list.include?(event_name)
raise ArgumentError.new("Event name must be a symbol") unless event_name.is_a? Symbol
+
(callbacks[event_name] || []).each do |callback|
callback.call(event_data)
end
end
@@ -32,17 +35,27 @@
callback_name: options[:with]
remove_callback(event_name, callback)
end
- def enable
+ def enable(opts = {})
+ clear_event_lists
+ if opts[:only]
+ set_enabled opts[:only]
+ end
self.disabled = false
end
- def disable
- self.disabled = true
+ def disable(opts = {})
+ clear_event_lists
+ if opts[:only]
+ set_disabled opts[:only]
+ self.disabled = false
+ else
+ self.disabled = true
+ end
end
def clear
@callbacks = {}
@@ -50,11 +63,11 @@
private
attr_reader :callbacks
- attr_accessor :disabled
+ attr_accessor :disabled, :disabled_list, :enabled_list
def callbacks
@callbacks ||= {}
end
@@ -63,9 +76,29 @@
def add_callback(event_name, callback)
callbacks[event_name] ||= []
callbacks[event_name] << callback
end
+
+ def set_disabled(events)
+ self.disabled_list = wrap_event_list(events)
+ end
+
+
+ def set_enabled(events)
+ self.enabled_list = wrap_event_list(events)
+ end
+
+
+ def clear_event_lists
+ self.enabled_list = nil
+ self.disabled_list = nil
+ end
+
+
+ def wrap_event_list(events)
+ events.is_a?(Array) ? events : [events]
+ end
def remove_callback(event_name, callback_to_delete)
return unless callbacks[event_name]
callbacks[event_name].each do |callback|