Class: Xssh::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/xssh/template.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
10
DEFAULT_PROMPT =
/[$%#>] ?\z/n.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#methodsObject (readonly)

Returns the value of attribute methods.



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

def methods
  @methods
end

#promptsObject (readonly)

Returns the value of attribute prompts.



11
12
13
# File 'lib/xssh/template.rb', line 11

def prompts
  @prompts
end

#timeoutObject (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_pattenObject



28
29
30
# File 'lib/xssh/template.rb', line 28

def prompt_patten
  Regexp.union(prompt.map(&:first))
end