Class: CommandBuilder::CodeGenerator::Command

Inherits:
Node
  • Object
show all
Defined in:
lib/command_builder/code_generator/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#args, #branch?, #child_nodes, #fragments, #leaf?

Constructor Details

#initialize(command_def) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
16
17
18
19
# File 'lib/command_builder/code_generator/command.rb', line 11

def initialize(command_def)
  hash = Command.command_hash command_def
  command_text = hash.keys[0]
  command_name = Command.command_name command_text
  node_def = command_text[command_name.length, command_text.length]
  super node_def
  Command.process_array hash.values[0], self
  @command_name = command_name
end

Instance Attribute Details

#command_nameObject (readonly)

Returns the value of attribute command_name.



9
10
11
# File 'lib/command_builder/code_generator/command.rb', line 9

def command_name
  @command_name
end

Class Method Details

.command_hash(command_text) ⇒ Object



31
32
33
34
35
# File 'lib/command_builder/code_generator/command.rb', line 31

def Command.command_hash(command_text)
  yaml = command_text.split(String::NEW_LINE).map { |l| l.gsub(/^( *)(\S.+)$/, '\1- "\2":') }.join String::NEW_LINE
  array = YAML.load yaml
  array[0]
end

.command_name(command_text) ⇒ Object



27
28
29
# File 'lib/command_builder/code_generator/command.rb', line 27

def Command.command_name(command_text)
  command_text[/^([-\.\w]+)/, 1]
end

.process_array(array, parent_node = nil) ⇒ Object



37
38
39
40
# File 'lib/command_builder/code_generator/command.rb', line 37

def Command.process_array(array, parent_node=nil)
  array ||= []
  array.each { |hash| Command.process_hash hash, parent_node }
end

.process_hash(hash, parent_node) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/command_builder/code_generator/command.rb', line 42

def Command.process_hash(hash, parent_node)
  hash.each_pair do |node_text, child_nodes|
    node = Node.new " #{node_text}"
    parent_node.child_nodes << node
    process_array child_nodes, node
  end
end

Instance Method Details

#node_nameObject



21
22
23
# File 'lib/command_builder/code_generator/command.rb', line 21

def node_name
  @node_name ||= node_alias || @command_name
end