Class: Taketo::NodeResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/taketo/node_resolver.rb

Direct Known Subclasses

GroupResolver, ServerResolver

Instance Method Summary collapse

Constructor Details

#initialize(config, path) ⇒ NodeResolver

Returns a new instance of NodeResolver.



10
11
12
13
14
15
16
17
# File 'lib/taketo/node_resolver.rb', line 10

def initialize(config, path)
  @config = config
  if String(path).empty? && !String(config.default_destination).empty?
    path = config.default_destination
  end
  @path = path
  @traverser = ConfigTraverser.new(@config)
end

Instance Method Details

#disambiguate(results) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/taketo/node_resolver.rb', line 36

def disambiguate(results)
  case results.length
  when 0
    raise NonExistentDestinationError, "Can't find such destination: #@path"
  when 1
    results.first
  else
    exact_match = results.detect { |n| n.path == @path }
    exact_match or raise AmbiguousDestinationError,
      "There are multiple possible destinations: #{results.map(&:path).join(", ")}"
  end
end

#nodesObject



28
29
30
31
32
33
34
# File 'lib/taketo/node_resolver.rb', line 28

def nodes
  @nodes ||= begin
    collector = SimpleCollector(Taketo::Constructs::BaseConstruct).new
    @traverser.visit_depth_first(collector)
    collector.result.reject { |n| Taketo::Constructs::Command === n }
  end
end

#resolveObject



19
20
21
# File 'lib/taketo/node_resolver.rb', line 19

def resolve
  resolve_by_path
end

#resolve_by_pathObject



23
24
25
26
# File 'lib/taketo/node_resolver.rb', line 23

def resolve_by_path
  matching_nodes = nodes.select { |n| n.path == @path }
  disambiguate(matching_nodes)
end