Class: Scrab::CommandSet

Inherits:
Object
  • Object
show all
Defined in:
lib/scrab/command_set.rb

Defined Under Namespace

Classes: CommandNotFoundError

Instance Method Summary collapse

Constructor Details

#initialize(namespace, commands_path) ⇒ CommandSet

Returns a new instance of CommandSet.



8
9
10
11
# File 'lib/scrab/command_set.rb', line 8

def initialize(namespace, commands_path)
  @namespace     = namespace
  @commands_path = Pathname.new(commands_path)
end

Instance Method Details

#get(command_name, out) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scrab/command_set.rb', line 13

def get(command_name, out)
  begin
    Kernel.load(@commands_path.join("#{command_name}.rb").to_s)
  rescue LoadError => e
    raise CommandNotFoundError.new(command_name)
  end

  const = @namespace

  command_name.split("/").each do |command_part|
    const_name = command_part.capitalize.gsub(/_(.)/) { $1.upcase }
    const      = const.const_get(const_name)
  end

  const.new(out)
end