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.
9
10
11
12
|
# File 'lib/xlogin/factory.rb', line 9
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, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/xlogin/factory.rb', line 76
def method_missing(method_name, *args, &block)
super unless caller_locations.first.label == 'block in source' and args.size >= 2
type = method_name.to_s.downcase
name = args.shift
uri = args.shift
opts = args.shift || {}
super if [type, name, uri].any? { |e| e.nil? }
set_info(type: type, name: name, uri: uri, **opts)
end
|
Instance Method Details
#build(type:, uri:, **opts) ⇒ Object
64
65
66
67
|
# File 'lib/xlogin/factory.rb', line 64
def build(type:, uri:, **opts)
template = get_template(type)
template.build(uri, **opts)
end
|
#build_from_hostname(hostname, **opts) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/xlogin/factory.rb', line 69
def build_from_hostname(hostname, **opts)
hostinfo = get_info(hostname)
raise Xlogin::SessionError.new("Host not found: '#{hostname}'") unless hostinfo
build(hostinfo.merge(name: hostname, **opts))
end
|
#get_info(name) ⇒ Object
27
28
29
|
# File 'lib/xlogin/factory.rb', line 27
def get_info(name)
@inventory[name]
end
|
#get_template(name) ⇒ Object
56
57
58
|
# File 'lib/xlogin/factory.rb', line 56
def get_template(name)
@templates[name.to_s.downcase] ||= Xlogin::Template.new(name)
end
|
#list_info(*patterns) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/xlogin/factory.rb', line 31
def list_info(*patterns)
return @inventory.values if patterns.empty?
values = patterns.map do |pattern|
key, val = pattern.to_s.split(':')
key, val = 'name', key if val.nil?
val.split(',').map { |e| @inventory.values.select { |info| File.fnmatch(e, info[key.to_sym]) } }.reduce(&:|)
end
values.reduce(&:&).uniq
end
|
#list_templates ⇒ Object
60
61
62
|
# File 'lib/xlogin/factory.rb', line 60
def list_templates
@templates.keys
end
|
#set_info(**opts) ⇒ Object
21
22
23
24
25
|
# File 'lib/xlogin/factory.rb', line 21
def set_info(**opts)
name = opts[:name]
return unless name
@inventory[name] = (get_info(name) || {}).merge(opts)
end
|
#set_template(name, text) ⇒ Object
50
51
52
53
54
|
# File 'lib/xlogin/factory.rb', line 50
def set_template(name, text)
template = get_template(name)
template.instance_eval(text)
@templates[name.to_s.downcase] = template
end
|
#source(*files) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/xlogin/factory.rb', line 14
def source(*files)
files.compact.each do |file|
raise SessionError.new("Inventory file not found: #{file}") unless File.exist?(file)
instance_eval(IO.read(file)) if File.exist?(file)
end
end
|
#source_template(*files) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/xlogin/factory.rb', line 42
def source_template(*files)
files.compact.each do |file|
raise TemplateError.new("Template file not found: #{file}") unless File.exist?(file)
name = File.basename(file, '.rb').scan(/\w+/).join('_')
set_template(name, IO.read(file)) if File.exist?(file)
end
end
|