Skip to content

Commit

Permalink
Copy variables from parent class
Browse files Browse the repository at this point in the history
When we're subclassing existing model, copy its variables. Without this
we need to explicitly define everything in child class, like for example
which protocols to use.

Fixes ytti#1901
  • Loading branch information
ytti committed Oct 24, 2019
1 parent 08f137d commit 034eeb4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/oxidized/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ 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 '@procs', (Hash.new { |h, k| h[k] = [] })
klass.instance_variable_set '@expect', []
klass.instance_variable_set '@comment', nil
klass.instance_variable_set '@prompt', nil
if klass.superclass == Oxidized::Model
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 '@procs', (Hash.new { |h, k| h[k] = [] })
klass.instance_variable_set '@expect', []
klass.instance_variable_set '@comment', nil
klass.instance_variable_set '@prompt', nil
else # we're subclassing some existing model, take its variables
instance_variables.each do |var|
klass.instance_variable_set var, instance_variable_get(var)
end
end
end

def comment(str = '# ')
Expand Down

0 comments on commit 034eeb4

Please sign in to comment.