Class: FBomb::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/fbomb/command.rb

Overview

class_methods

Defined Under Namespace

Classes: DSL, Table

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



89
90
91
# File 'lib/fbomb/command.rb', line 89

def initialize
  @call = proc{}
end

Class Method Details

.commandsObject



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.expand_path(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

.setupObject



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

#flowObject

instance methods



83
# File 'lib/fbomb/command.rb', line 83

fattr(:flow){ self.class.flow }

#matches?(*args) ⇒ Boolean

Returns:

  • (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_commentObject



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
  message =
    catch(:message) do
      FBomb.messages.reverse.each do |message|
        next if message == FBomb.message

        case message['type'].to_s
          when 'TextMessage'
            throw :message,  message
        end
      end
      nil
    end
  message['body'] if message
end