lib/hipbot/reaction.rb in hipbot-1.0.0.rc2 vs lib/hipbot/reaction.rb in hipbot-1.0.0
- old
+ new
@@ -1,25 +1,35 @@
module Hipbot
class Reaction < Struct.new(:plugin, :options, :block)
- def any_room?
+ include Cache
+
+ def in_any_room?
options[:room] == true
end
- def anything?
- regexps.blank?
+ def to_anything?
+ regexps.empty?
end
- def anywhere?
+ def from_anywhere?
options[:room].nil?
end
+ def condition
+ options[:if] || Proc.new{ true }
+ end
+
+ def delete
+ plugin.reactions.delete(self)
+ end
+
def desc
options[:desc]
end
def from_all?
- options[:from].blank?
+ options[:from].nil?
end
def global?
!!options[:global]
end
@@ -34,32 +44,34 @@
def match_with message
Match.new(self, message)
end
- def private_message_only?
+ def to_private_message?
options[:room] == false
end
- def readable_command
- regexps.to_s.gsub(/(?<!\\)(\/|\[|\]|\^|\\z|\$|\\)/, '')
+ attr_cache :readable_command do
+ regexps.map(&:source).join(' or ').gsub(/\^|\\z|\$|\\/, '')
end
- def regexps
- options[:regexps]
+ attr_cache :regexps do
+ Array(options[:regexps]).map do |regexp|
+ Regexp.new(regexp.source, Regexp::IGNORECASE)
+ end
end
- def rooms
- replace_symbols options[:room], Hipbot.rooms
+ attr_cache :rooms do
+ replace_symbols(options[:room], Hipbot.rooms)
end
- def users
- replace_symbols options[:from], Hipbot.teams
+ attr_cache :users do
+ replace_symbols(options[:from], Hipbot.teams)
end
protected
def replace_symbols values, replacements_hash
- Array(values).flat_map{ |v| replacements_hash[v].presence || v.to_s }
+ Array(values).flat_map{ |v| replacements_hash[v] || v }.uniq
end
end
end