Class: Xssh::Factory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/xssh/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFactory

Returns a new instance of Factory.



12
13
14
15
# File 'lib/xssh/factory.rb', line 12

def initialize
  @inventory = {}
  @templates = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xssh/factory.rb', line 57

def method_missing(method, *args, **options)
  super unless args.size == 2

  name = args.shift
  uri  = URI.parse(args.shift)

  options[:type]       = method
  options[:uri]        = uri
  options[:name]       = name
  options[:host_name]  = uri.host
  options[:user]       = uri.user
  options[:password]   = uri.password

  set_source(name, **options)
end

Instance Attribute Details

#inventoryObject

Returns the value of attribute inventory.



9
10
11
# File 'lib/xssh/factory.rb', line 9

def inventory
  @inventory
end

#templatesObject

Returns the value of attribute templates.



10
11
12
# File 'lib/xssh/factory.rb', line 10

def templates
  @templates
end

Instance Method Details

#dsl(str) ⇒ Object



53
54
55
# File 'lib/xssh/factory.rb', line 53

def dsl(str)
  instance_eval(str)
end

#query(q) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xssh/factory.rb', line 26

def query(q)
  list = inventory.map { |_k, v| v }
  return list if q.empty?

  list.select do |h|
    q.any? do |k, v|
      case v
      when Regexp
        h[k] =~ v
      else
        h[k] == v
      end
    end
  end
end

#set_source(name, **args) ⇒ Object



17
18
19
20
# File 'lib/xssh/factory.rb', line 17

def set_source(name, **args)
  args[:name]      = name
  @inventory[name] = args
end

#set_template(name, klass) ⇒ Object



22
23
24
# File 'lib/xssh/factory.rb', line 22

def set_template(name, klass)
  @templates[name] = klass
end

#yaml(file) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
# File 'lib/xssh/factory.rb', line 42

def yaml(file)
  raise Error, 'not cofnig file' unless File.exist?(file)

  srcs = YAML.load_file(file)
  srcs.each do |src|
    src        = src.inject({}) { |h, (k, v)| h = h.merge(k.to_sym => v) }
    name       = src.delete(:host)
    set_source(name, **src)
  end
end