Class: Construqt::Flavour::Ciscian::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/construqt/flavour/ciscian/ciscian.rb

Defined Under Namespace

Classes: Lines

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Result

Returns a new instance of Result.



21
22
23
24
25
26
27
28
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 21

def initialize(host)
  @host = host
  @sections = {}
  throw "ciscian flavour can only be created with dialect" unless host.dialect
  require_relative "dialect_#{host.dialect}.rb"
  throw "cannot load dialect class #{host.dialect}" unless Ciscian.dialects[host.dialect]
  self.dialect=Ciscian.dialects[host.dialect].new(self)
end

Instance Attribute Details

#dialectObject

Returns the value of attribute dialect.



20
21
22
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 20

def dialect
  @dialect
end

#hostObject

Returns the value of attribute host.



20
21
22
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 20

def host
  @host
end

#sectionsObject

Returns the value of attribute sections.



20
21
22
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 20

def sections
  @sections
end

Class Method Details

.compare(nu, old) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 111

def self.compare(nu, old)
  result = Result.new(nu.host)

  nu_root=NestedSection.new("root")
  nu_root.sections.merge!(nu.sections)
  other_root=NestedSection.new("root")
  other_root.sections.merge!(old.sections)

  deltas=NestedSection.compare(nu_root, other_root)
  throw "illegal state" if deltas.length != 1
  result.sections = deltas[0]
  result
end

.normalize_section_key(key) ⇒ Object



71
72
73
74
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 71

def self.normalize_section_key(key)
  matchdata=key.match(/^\s*(no\s+|)(.*)/)
  matchdata[2]
end

.starts_with_no(key) ⇒ Object



76
77
78
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 76

def self.starts_with_no(key)
  return key.match(/^\s*no\s+/)
end

Instance Method Details

#add(section, clazz = NestedSection) {|| ... } ⇒ Object

Yields:

  • ()


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 98

def add(section, clazz=NestedSection)
  throw "section is nil" unless section
  section = Lines::Line.new(section, -1) unless section.kind_of?(Lines::Line)
  section_key=Result.normalize_section_key(section.to_s)

  @sections[section_key] ||= clazz.new(section_key)
  if Result.starts_with_no(section.to_s)
    @sections[section_key].no
  end
  yield(@sections[section_key])  if block_given?
  @sections[section_key]
end

#commitObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 87

def commit
  self.dialect.commit

  block=[]
  @sections.keys.sort.each do |key|
    section = @sections[key]
    block += section.serialize
  end
  Util.write_str(block.join("\n"), File.join(@host.name, "#{@host.fname||self.dialect.class.name}.cfg"))
end

#parse(lines) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 63

def parse(lines)
  lines = Lines.new(lines)
  while line = lines.shift
    parse_line(line, lines, self, self)
  end
  self
end

#parse_line(line, lines, section, result) ⇒ Object



80
81
82
83
84
85
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 80

def parse_line(line, lines, section, result)
  return if self.dialect.parse_line(line, lines, section, result)
  [NestedSection, SingleValueVerb].find do |clazz|
    clazz.parse_line(line, lines, section, result)
  end
end