Class: Fate::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/fate/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification, options) ⇒ Service

Returns a new instance of Service.



7
8
9
10
11
12
13
14
15
16
# File 'lib/fate/service.rb', line 7

def initialize(specification, options)
  @specification = specification
  @options = options

  @commands = process_commands(@specification[:commands])
  @names = @commands.keys
  @longest_name = @commands.keys.sort_by {|k| k.size }.last.size
  @logger = Fate::MultiLogger.new(:io => STDOUT, :width => @longest_name)
  @output_handlers = Output::Handlers.new(self, options[:output] || {})
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/fate/service.rb', line 5

def commands
  @commands
end

#completionsObject (readonly)

Returns the value of attribute completions.



5
6
7
# File 'lib/fate/service.rb', line 5

def completions
  @completions
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/fate/service.rb', line 6

def logger
  @logger
end

#longest_nameObject (readonly)

Returns the value of attribute longest_name.



5
6
7
# File 'lib/fate/service.rb', line 5

def longest_name
  @longest_name
end

#namesObject (readonly)

Returns the value of attribute names.



5
6
7
# File 'lib/fate/service.rb', line 5

def names
  @names
end

#output_handlersObject (readonly)

Returns the value of attribute output_handlers.



6
7
8
# File 'lib/fate/service.rb', line 6

def output_handlers
  @output_handlers
end

#specificationObject (readonly)

Returns the value of attribute specification.



5
6
7
# File 'lib/fate/service.rb', line 5

def specification
  @specification
end

Instance Method Details

#process_commands(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fate/service.rb', line 18

def process_commands(hash)
  hash = Squeeze::HashTree[hash]

  out = {}
  @completions ||= Set.new
  hash.each_path do |path, value|
    key = path.join(".")
    # add dot-delimited command names to the completions
    @completions += path.map {|s| s.to_s }
    @completions << key
    # register each command under the dot-delimited name
    out[key] = value
  end
  out
end

#resolve_commands(name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fate/service.rb', line 34

def resolve_commands(name)
  targets = []
  if @commands.has_key?(name)
    targets << name
  else
    @commands.each do |cname, _command|
      if cname.split(".").first == name
        targets << cname
      end
    end
  end
  targets
end

#start_order(command_names) ⇒ Object



49
50
51
52
53
54
# File 'lib/fate/service.rb', line 49

def start_order(command_names)
  # presuming the spec file ordered the commands where the dependencies
  # come before the dependers, we should stop the processes in reverse order,
  # then start them back up again in forward order.
  command_names.sort_by {|name| self.names.index(name) }
end

#stop_order(command_names) ⇒ Object



56
57
58
# File 'lib/fate/service.rb', line 56

def stop_order(command_names)
  start_order(command_names).reverse
end