Class: Balmora::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/balmora/extension.rb

Defined Under Namespace

Modules: FileSecret Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Extension

Returns a new instance of Extension.



11
12
13
14
15
# File 'lib/balmora/extension.rb', line 11

def initialize(state)
  @command_constant = ::Balmora::Command
  @extension_constant = ::Balmora::Extension
  @state = state
end

Instance Attribute Details

#command_constantObject

Returns the value of attribute command_constant.



5
6
7
# File 'lib/balmora/extension.rb', line 5

def command_constant
  @command_constant
end

#extension_constantObject

Returns the value of attribute extension_constant.



5
6
7
# File 'lib/balmora/extension.rb', line 5

def extension_constant
  @extension_constant
end

Class Method Details

.factory(state) ⇒ Object



7
8
9
# File 'lib/balmora/extension.rb', line 7

def self.factory(state)
  return self.new(state)
end

Instance Method Details

#create_command(state, command) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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