Class: Xssh::Template
- Inherits:
-
Object
- Object
- Xssh::Template
- Defined in:
- lib/xssh/template.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
- DEFAULT_PROMPT =
/[$%#>] ?\z/n.freeze
Instance Attribute Summary collapse
-
#methods ⇒ Object
readonly
Returns the value of attribute methods.
-
#prompts ⇒ Object
readonly
Returns the value of attribute prompts.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #bind(name, &block) ⇒ Object
- #build(name, **opts) ⇒ Object
-
#initialize(name) ⇒ Template
constructor
A new instance of Template.
- #prompt(expect = nil, &block) ⇒ Object
- #prompt_patten ⇒ Object
Constructor Details
#initialize(name) ⇒ Template
Returns a new instance of Template.
14 15 16 17 18 19 |
# File 'lib/xssh/template.rb', line 14 def initialize(name) @name = name @prompts = [] @timeout = DEFAULT_TIMEOUT @methods = {} end |
Instance Attribute Details
#methods ⇒ Object (readonly)
Returns the value of attribute methods.
10 11 12 |
# File 'lib/xssh/template.rb', line 10 def methods @methods end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
11 12 13 |
# File 'lib/xssh/template.rb', line 11 def prompts @prompts end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
12 13 14 |
# File 'lib/xssh/template.rb', line 12 def timeout @timeout end |
Instance Method Details
#bind(name, &block) ⇒ Object
32 33 34 |
# File 'lib/xssh/template.rb', line 32 def bind(name, &block) @methods[name] = block end |
#build(name, **opts) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/xssh/template.rb', line 36 def build(name, **opts) klass = Class.new(Xssh.const_get('Connection')) klass.class_exec(self) do |template| template.methods.each { |name, block| define_method(name, &block) } end klass.new(self, name, **opts) end |
#prompt(expect = nil, &block) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/xssh/template.rb', line 21 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 |
#prompt_patten ⇒ Object
28 29 30 |
# File 'lib/xssh/template.rb', line 28 def prompt_patten Regexp.union(prompt.map(&:first)) end |