Module: Command::Common

Included in:
Command, CommandSet
Defined in:
lib/command-set/command-common.rb

Instance Method Summary collapse

Instance Method Details

#add_requirements(subject) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/command-set/command-common.rb', line 34

def add_requirements(subject)
  each_command do |command|
    subject.required_fields(*(command.subject_requirements.uniq))
    command.argument_list.each do |argument|
      subject.required_fields(*(argument.subject_requirements.uniq))
    end
  end
  return subject
end

#completion_list(terms, prefix, subject) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/command-set/command-common.rb', line 73

def completion_list(terms, prefix, subject)
  visitor = SetVisitor.new(:prefix => prefix, 
                           :subject => subject,
                           :completion_list => [],
                           :arguments => [],
                           :original_terms => terms.dup)
  def visitor.arrive_at(terms, node)
    self.arguments = node.argument_list.dup
    image = subject.get_image(node.subject_requirements)
    until(terms.empty? or arguments.empty?) do
      arguments.first.match_terms(image, terms, arguments)
    end
  end

  def visitor.set_out_of_terms(node)
    if arguments.empty? or not arguments.first.required?
      self.completion_list = node.command_names.grep(%r{^#{prefix}.*})
    end
    self.command_out_of_terms(node)
  end

  def visitor.command_out_of_terms(node)
    image = subject.get_image(node.subject_requirements)
    arguments.each do |handler|
      self.completion_list += handler.complete(original_terms, prefix, image)
      break unless handler.omittable?
    end 
  end

  def visitor.term_without_hop(terms, node)
    return []
  end

  visit(terms, visitor)
  return visitor.completion_list
end

#find_command(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/command-set/command-common.rb', line 44

def find_command(path)
  visitor = SetVisitor.new(:command => nil)
  def visitor.set_out_of_terms(node)
    self.command = node
  end

  def visitor.arrive_at(terms, node)
    self.command = node
  end

  def visitor.term_without_hop(terms, node)
    raise CommandException, "No such command #{terms.first}"
  end
    
  visit(path, visitor)
  return visitor.command
end

#pathObject



26
27
28
29
30
31
32
# File 'lib/command-set/command-common.rb', line 26

def path
  if @parent.nil?
    return []
  else
    return @parent.path + [@name]
  end
end

#process_terms(terms, subject, arg_hash = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/command-set/command-common.rb', line 62

def process_terms(terms, subject, arg_hash={})
  visitor = SetVisitor.new(:command_class => nil, :subject => subject, :arg_hash => {})
  def visitor.arrive_at(terms, node)
    self.command_class = node.factory
    subject = self.subject.get_image(node.subject_requirements)
    return node.consume_terms(terms, subject, self.arg_hash)
  end
  visit(terms, visitor)
  return visitor
end