Class: Xlogin::Factory
- Inherits:
-
Object
show all
- Includes:
- Singleton
- Defined in:
- lib/xlogin/factory.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Factory.
11
12
13
14
|
# File 'lib/xlogin/factory.rb', line 11
def initialize
@inventory = Hash.new
@templates = Hash.new
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **opts, &block) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/xlogin/factory.rb', line 71
def method_missing(method_name, *args, **opts, &block)
super unless args.size == 2 && URI.regexp =~ args[1]
type = method_name.to_s.downcase
name = args[0]
uri = args[1]
set_inventory(name, type: type, uri: uri, **opts)
end
|
Instance Method Details
#build(type:, **opts) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/xlogin/factory.rb', line 53
def build(type:, **opts)
template = get_template(type)
raise Xlogin::Error.new("Template not found: '#{type}'") unless template
template.build(uri(opts), **opts)
end
|
#build_from_hostname(args, **opts) ⇒ Object
64
65
66
67
68
69
|
# File 'lib/xlogin/factory.rb', line 64
def build_from_hostname(args, **opts)
hostinfo = get_inventory(args)
raise Xlogin::Error.new("Host not found: '#{args}'") unless hostinfo
build(hostinfo.merge(name: args, **opts))
end
|
#build_pool(args, **opts) ⇒ Object
60
61
62
|
# File 'lib/xlogin/factory.rb', line 60
def build_pool(args, **opts)
Xlogin::SessionPool.new(args, **opts)
end
|
#get_inventory(name) ⇒ Object
20
21
22
|
# File 'lib/xlogin/factory.rb', line 20
def get_inventory(name)
@inventory[name]
end
|
#get_template(name) ⇒ Object
45
46
47
|
# File 'lib/xlogin/factory.rb', line 45
def get_template(name)
@templates[name.to_s.downcase]
end
|
#list_inventory(*patterns) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/xlogin/factory.rb', line 24
def list_inventory(*patterns)
return @inventory.values if patterns.empty?
values1 = patterns.map do |pattern|
values2 = pattern.split(',').map do |entry|
key, val = entry.to_s.split(':')
key, val = 'name', key if val.nil?
@inventory.values.select { |e| File.fnmatch(val, e[key.to_sym]) }
end
values2.reduce(&:&)
end
values1.reduce(&:|)
end
|
#list_templates ⇒ Object
49
50
51
|
# File 'lib/xlogin/factory.rb', line 49
def list_templates
@templates.keys
end
|
#set_inventory(name, **opts) ⇒ Object
16
17
18
|
# File 'lib/xlogin/factory.rb', line 16
def set_inventory(name, **opts)
@inventory[name] = (get_inventory(name) || {name: name}).merge(opts)
end
|
#set_template(name, text = nil, &block) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/xlogin/factory.rb', line 38
def set_template(name, text = nil, &block)
template = get_template(name) || Xlogin::Template.new(name)
template.instance_eval(text) if text
template.instance_eval(&block) if block
@templates[name.to_s.downcase] = template
end
|