Class: Xlogin::Template
- Inherits:
-
Object
show all
- Includes:
- RelayTemplate
- Defined in:
- lib/xlogin/template.rb,
lib/xlogin/delegator.rb
Defined Under Namespace
Modules: RelayTemplate
Constant Summary
collapse
- DEFAULT_TIMEOUT =
10
- DEFAULT_PROMPT =
/[$%#>] ?\z/n
- RESERVED_METHODS =
%i( login logout enable disable delegate )
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name) ⇒ Template
Returns a new instance of Template.
13
14
15
16
17
18
19
20
|
# File 'lib/xlogin/template.rb', line 13
def initialize(name)
@name = name
@scopes = Hash.new
@timeout = DEFAULT_TIMEOUT
@prompts = Array.new
@methods = Hash.new
@interrupt = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, &block) ⇒ Object
65
66
67
68
|
# File 'lib/xlogin/template.rb', line 65
def method_missing(name, *, &block)
super unless RESERVED_METHODS.include?(name)
bind(name) { |*args| instance_exec(*args, &block) }
end
|
Instance Attribute Details
#methods ⇒ Object
Returns the value of attribute methods.
11
12
13
|
# File 'lib/xlogin/template.rb', line 11
def methods
@methods
end
|
#name ⇒ Object
Returns the value of attribute name.
11
12
13
|
# File 'lib/xlogin/template.rb', line 11
def name
@name
end
|
#scopes ⇒ Object
Returns the value of attribute scopes.
11
12
13
|
# File 'lib/xlogin/template.rb', line 11
def scopes
@scopes
end
|
Instance Method Details
#bind(name = nil, &block) ⇒ Object
37
38
39
|
# File 'lib/xlogin/template.rb', line 37
def bind(name = nil, &block)
@methods[name] = block
end
|
#build(uri, **opts) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/xlogin/template.rb', line 46
def build(uri, **opts)
klass = Class.new(Xlogin.const_get(uri.scheme.capitalize))
klass.class_exec(self) do |template|
scopes = [*opts[:scope]].compact
scopes.each { |scope| template.instance_eval(&template.scopes[scope]) }
template.methods.each do |name, block|
case name.to_s
when 'enable'
define_method(name) { |args = nil| instance_exec(args || opts[:enable], &block) }
else
define_method(name, &block)
end
end
end
klass.new(self, uri, **opts)
end
|
#interrupt!(&block) ⇒ Object
41
42
43
44
|
# File 'lib/xlogin/template.rb', line 41
def interrupt!(&block)
return @interrupt unless block
@interrupt = block
end
|
#prompt(expect = nil, &block) ⇒ Object
27
28
29
30
31
|
# File 'lib/xlogin/template.rb', line 27
def prompt(expect = nil, &block)
return [[DEFAULT_PROMPT, nil]] if expect.nil? && @prompts.empty?
@prompts << [Regexp.new(expect.to_s), block] if expect
@prompts
end
|
#scope(name = nil, &block) ⇒ Object
33
34
35
|
# File 'lib/xlogin/template.rb', line 33
def scope(name = nil, &block)
@scopes[name] = block
end
|
#timeout(val = nil) ⇒ Object
22
23
24
25
|
# File 'lib/xlogin/template.rb', line 22
def timeout(val = nil)
@timeout = val.to_i if val
@timeout
end
|