class Balmora::Extension

Attributes

command_constant[RW]
extension_constant[RW]

Public Class Methods

factory(state) click to toggle source
# File lib/balmora/extension.rb, line 7
def self.factory(state)
  return self.new(state)
end
new(state) click to toggle source
# File lib/balmora/extension.rb, line 11
def initialize(state)
  @command_constant = ::Balmora::Command
  @extension_constant = ::Balmora::Extension
  @state = state
end

Public Instance Methods

create_command(state, command) click to toggle source
# File lib/balmora/extension.rb, line 32
def create_command(state, command)
  if command.instance_of?(::String)
    command = {command: 'exec', exec: command}
  end

  if command[:command].nil?()
    raise Error.new("\"command\" should be defined in command " +
      "#{command.inspect()}")
  end

  command_name = @state.variables.inject(command[:command])
  command_class = get(@command_constant, command_name)
  if !(command_class < @command_constant)
    raise Error.new("Command should be instance of #{@command_constant}")
  end

  command_instance = command_class.new(state, command)

  (command[:extensions] || []).each() { |extension|
    extension_class = get(@extension_constant, extension)
    command_instance.extend(extension_class)
  }

  return command_instance
end
get(namespace, extension) click to toggle source
# File lib/balmora/extension.rb, line 17
def get(namespace, extension)
  class_name =
    extension.
    to_s().
    gsub(/(-|^)(\w)/) { |char | (char[1] || char[0]).upcase() }.
    to_sym()

  if !namespace.const_defined?(class_name, false)
    raise Error.new("Extension #{class_name.inspect()} not found in " +
      "namespace " + namespace.inspect())
  end

  return namespace.const_get(class_name, false)
end