Class: FBomb::Command
- Inherits:
-
Object
- Object
- FBomb::Command
- Defined in:
- lib/fbomb/command.rb
Overview
class_methods
Defined Under Namespace
Class Method Summary collapse
- .commands ⇒ Object
- .load(*args) ⇒ Object
- .load_absolute_path(arg) ⇒ Object
- .load_relative_path(arg) ⇒ Object
- .load_string(string, __file__, __lineno__, dangerous = true) ⇒ Object
- .load_uri(arg) ⇒ Object
- .setup ⇒ Object
Instance Method Summary collapse
- #call(*args, &block) ⇒ Object
- #call=(call) ⇒ Object
-
#flow ⇒ Object
instance methods.
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #matches?(*args) ⇒ Boolean
- #most_recent_comment ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
89 90 91 |
# File 'lib/fbomb/command.rb', line 89 def initialize @call = proc{} end |
Class Method Details
.commands ⇒ Object
40 41 42 |
# File 'lib/fbomb/command.rb', line 40 def commands table.values end |
.load(*args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fbomb/command.rb', line 24 def load(*args) args.flatten.uniq.each do |arg| Command.command_paths << arg case arg.to_s when %r|^[/~]| load_absolute_path(arg) when %r|://| load_uri(arg) else load_relative_path(arg) end end setup table end |
.load_absolute_path(arg) ⇒ Object
66 67 68 69 70 |
# File 'lib/fbomb/command.rb', line 66 def load_absolute_path(arg) path = File.(arg.to_s) path += '.rb' unless path =~ /\.rb\Z/ open(path){|fd| load_string(fd.read, path, 1)} end |
.load_relative_path(arg) ⇒ Object
60 61 62 63 64 |
# File 'lib/fbomb/command.rb', line 60 def load_relative_path(arg) basename = arg.to_s basename += '.rb' unless basename =~ /\.rb\Z/ load_absolute_path(File.join(Command.dir, basename)) end |
.load_string(string, __file__, __lineno__, dangerous = true) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/fbomb/command.rb', line 72 def load_string(string, __file__, __lineno__, dangerous = true) Thread.new(string, dangerous) do |string, dangerous| Thread.current.abort_on_exception = true $SAFE = 12 unless dangerous module_eval(string, __file__, __lineno__) end.value end |
.load_uri(arg) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/fbomb/command.rb', line 53 def load_uri(arg) uri = arg.to_s open(uri) do |fd| open(path){|fd| load_string(fd.read, uri, 1)} end end |
.setup ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/fbomb/command.rb', line 44 def setup commands.each do |command| if command.setup and command.setup.respond_to?(:call) command.setup.call() command.setup = true end end end |
Instance Method Details
#call(*args, &block) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fbomb/command.rb', line 93 def call(*args, &block) arity = @call.arity argv = if arity >= 0 args[0, arity] else head = [] tail = [] n = arity.abs - 1 head = args[0...n] tail = args[n..-1] [*(head + tail)] end argv.compact! block ? @call=call : instance_exec(*argv, &@call) end |
#call=(call) ⇒ Object
113 114 115 |
# File 'lib/fbomb/command.rb', line 113 def call=(call) @call = call end |
#flow ⇒ Object
instance methods
83 |
# File 'lib/fbomb/command.rb', line 83 fattr(:flow){ self.class.flow } |
#matches?(*args) ⇒ Boolean
141 142 143 144 |
# File 'lib/fbomb/command.rb', line 141 def matches?(*args) text = Coerce.list_of_strings(args).join(' ') patterns.any?{|pattern| pattern === text} end |
#most_recent_comment ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fbomb/command.rb', line 125 def most_recent_comment = catch(:message) do FBomb..reverse.each do || next if == FBomb. case ['type'].to_s when 'TextMessage' throw :message, end end nil end ['body'] if end |