Class: Scatter::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scatter/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = nil) ⇒ Config

Returns a new instance of Config.



27
28
29
# File 'lib/scatter/config.rb', line 27

def initialize(file_name = nil)
  self.file_name = file_name || "#{ENV['HOME']}/.scatter/config"
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



25
26
27
# File 'lib/scatter/config.rb', line 25

def file_name
  @file_name
end

Class Method Details

.clearObject



8
9
10
# File 'lib/scatter/config.rb', line 8

def clear
  @instance = nil
end

.instanceObject



4
5
6
# File 'lib/scatter/config.rb', line 4

def instance
  @instance ||= new
end

.method_missing(method, *args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/scatter/config.rb', line 16

def method_missing(method, *args)
  if instance.respond_to?(method)
    instance.send(method, *args)
  else
    super
  end
end

.reloadObject



12
13
14
# File 'lib/scatter/config.rb', line 12

def reload
  @instance = new(@instance.file_name)
end

Instance Method Details

#find_destination(name) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/scatter/config.rb', line 64

def find_destination(name)
  if name =~ /\//
    remote, node = *(name.split('/'))
    remote_object = find_remote(remote)
    remote_object.find_node(node) if remote_object
  else
    (remotes + nodes).find { |dest| dest.name == name }
  end
end

#find_node(name) ⇒ Object



60
61
62
# File 'lib/scatter/config.rb', line 60

def find_node(name)
  nodes.find { |node| node.name == name || node.full_name == name }
end

#find_remote(name) ⇒ Object



56
57
58
# File 'lib/scatter/config.rb', line 56

def find_remote(name)
  remotes.find { |remote| remote.name == name }
end

#load!Object



40
41
42
43
44
45
46
47
# File 'lib/scatter/config.rb', line 40

def load!
  @loaded ||= begin
    @config = YAML.load(File.read(file_name)) || {}
    true
  rescue
    raise Scatter::CommandLineError, "Configuration file not found. Run: scatter init"
  end
end

#nodesObject



36
37
38
# File 'lib/scatter/config.rb', line 36

def nodes
  @nodes ||= remotes.collect { |remote| remote.nodes }.flatten
end

#remotesObject



31
32
33
34
# File 'lib/scatter/config.rb', line 31

def remotes
  load!
  @remotes ||= (@config['remotes'] || {}).collect { |name, config| Scatter::Remote.decode_from_config(name, config) }
end

#save!Object



49
50
51
52
53
54
# File 'lib/scatter/config.rb', line 49

def save!
  config = {
    'remotes' => self.remotes.inject({}) { |hash, remote| hash[remote.name] = remote.encode_for_config; hash }
  }
  File.open(file_name, 'w') { |file| file.puts config.to_yaml }
end