diff options
author | Saku Ytti <saku@ytti.fi> | 2013-05-01 13:45:02 +0300 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2013-05-01 14:06:44 +0300 |
commit | c252a589b6b0274a17bfe40db3fd57ebeea3beb8 (patch) | |
tree | 0115a10db0f37506e72aaf4a873aeea93734f6ab /lib/oxidized/model/model.rb | |
parent | 387b725fcd248d052cfe68da97f03192959ad6a4 (diff) |
Add Model#expect, support block at post/pre config
Now we can deal with pager and additional PW prompts, such as 'enable'
Examples in IOS model how to use.
The Telnet implementation is particularly fugly, I just need one line in
'waitfor' to handle pager while waiting for prompt, but couldn't figure
out clean way to do it, so needed to rewrit whole Telnet#waitfor just to
add that line.
Diffstat (limited to 'lib/oxidized/model/model.rb')
-rw-r--r-- | lib/oxidized/model/model.rb | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index 8f7b14f..2a6cbc4 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -2,22 +2,24 @@ module Oxidized class Model class << self def inherited klass - klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] } - klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] } + klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] } + klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] } + klass.instance_variable_set '@expect', [] + klass.const_set :CFG, CFG Oxidized.mgr.loader = { :class => klass } end def comment _comment='# ' return @comment if @comment @comment = block_given? ? yield : _comment end + def prompt _prompt=nil + @prompt or @prompt = _prompt + end def cfg *methods, &block [methods].flatten.each do |method| @cfg[method.to_s] << block end end - def prompt _prompt=nil - @prompt or @prompt = _prompt - end def cfgs @cfg end @@ -31,8 +33,11 @@ module Oxidized def cmds @cmd end - def post_login &block - @post_login or @post_login = block + def expect re, &block + @expect << [re, block] + end + def expects + @expect end end @@ -48,7 +53,15 @@ module Oxidized out end - def cfg + def send data + @input.send data + end + + def expect re, &block + self.class.expect re, &block + end + + def cfg self.class.cfgs end @@ -56,6 +69,19 @@ module Oxidized self.class.prompt end + def expects data + self.class.expects.each do |re, cb| + if data.match re + if cb.arity == 2 + data = instance_exec [data, re], &cb + else + data = instance_exec data, &cb + end + end + end + data + end + def get data = '' self.class.cmds[:cmd].each do |command, block| |