Class: NetConfGen::BlockEngine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath) ⇒ BlockEngine

Returns a new instance of BlockEngine.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/netconfgen/netconfgen.rb', line 42

def initialize(basepath)
  @basepath = basepath
  if !File.directory?(basepath)
    raise "Basepath #{basepath} does not exists"
  end
  @suffix = '.txt'

  @blocks = {}

  @context = BlockContext.new(self)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



41
42
43
# File 'lib/netconfgen/netconfgen.rb', line 41

def context
  @context
end

Instance Method Details

#load(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/netconfgen/netconfgen.rb', line 58

def load(name)
  if @blocks[name]
    return @blocks[name]
  end

  @code = '';
  File.open(@basepath + '/' + name + @suffix, "r") do |f|
    code_started = false
    code = ''
    f.each_line do |line|
      if line == "<code>\n"
        code_started = true
      elsif line == "</code>\n" || line == "</code>"
        code_started = false
      elsif code_started == true
        code += line
      end
    end

    block = Block.new(name)
    block.code = code
    block.blockengine = self
    @blocks[name] = block

    return block
  end
end

#set(key, value) ⇒ Object



54
55
56
# File 'lib/netconfgen/netconfgen.rb', line 54

def set(key, value)
  @context.instance_variable_set("@" + key, value)
end