Module: FSR::Cmd

Included in:
CommandSocket
Defined in:
lib/fsr/cmd.rb,
lib/fsr/cmd/fsctl.rb,
lib/fsr/cmd/sofia.rb,
lib/fsr/cmd/status.rb,
lib/fsr/cmd/originate.rb,
lib/fsr/cmd/sofia/status.rb,
lib/fsr/cmd/sofia/profile.rb,
lib/fsr/cmd/sofia_contact.rb

Defined Under Namespace

Classes: Command, Fsctl, Originate, Sofia, SofiaContact, Status

Constant Summary collapse

COMMANDS =
{}
LOAD_PATH =
[File.join(FSR::ROOT, "fsr", "cmd")]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listObject



16
17
18
# File 'lib/fsr/cmd.rb', line 16

def self.list
  COMMANDS.keys
end

.load_all(force_reload = false) ⇒ Object

Load all of the commands we find in Cmd::LOAD_PATH



45
46
47
48
49
50
# File 'lib/fsr/cmd.rb', line 45

def self.load_all(force_reload = false)
  LOAD_PATH.each do |load_path|
    Dir[File.join(load_path, "*.rb")].each { |command_file| load_command(command_file, force_reload) }
  end
  list
end

.load_command(command, force_reload = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fsr/cmd.rb', line 20

def self.load_command(command, force_reload = false)
  # If we get a path specification and it's an existing file, load it
  if File.file?(command)
    if force_reload
      return load(command)
    else
      return require(command)
    end
  end
  Log.debug "Trying load paths"
  # If we find a file named the same as the command passed in LOAD_PATH, load it
  if found_command_path = LOAD_PATH.detect { |command_path| File.file?(File.join(command_path, "#{command}.rb")) }
    command_file = Pathname.new(found_command_path).join(command)
    Log.debug "Trying to load #{command_file}"
    if force_reload
      load command_file.to_s + ".rb"
    else
      require command_file
    end
  else
    raise "#{command} not found in #{LOAD_PATH.join(":")}"
  end
end

.register(command, obj) ⇒ Object



9
10
11
12
13
14
# File 'lib/fsr/cmd.rb', line 9

def self.register(command, obj)
  COMMANDS[command.to_sym] = obj

  code = "def %s(*args, &block) COMMANDS[%p].new(self, *args, &block) end" % [command, command]
  Cmd.module_eval(code)
end

Instance Method Details

#commandsObject



52
53
54
# File 'lib/fsr/cmd.rb', line 52

def commands
  FSR::Cmd.list
end