Class: CFA::AugeasParser
- Inherits:
-
Object
- Object
- CFA::AugeasParser
- Defined in:
- lib/cfa/augeas_parser.rb
Overview
Instance Method Summary collapse
-
#empty ⇒ AugeasTree
An empty tree that can be filled for future serialization.
-
#initialize(lens) ⇒ AugeasParser
constructor
A new instance of AugeasParser.
-
#parse(raw_string) ⇒ AugeasTree
The parsed data.
-
#serialize(data) ⇒ String
A string to be written.
Constructor Details
#initialize(lens) ⇒ AugeasParser
Returns a new instance of AugeasParser.
308 309 310 |
# File 'lib/cfa/augeas_parser.rb', line 308 def initialize(lens) @lens = lens end |
Instance Method Details
#empty ⇒ AugeasTree
Returns an empty tree that can be filled for future serialization.
354 355 356 |
# File 'lib/cfa/augeas_parser.rb', line 354 def empty AugeasTree.new end |
#parse(raw_string) ⇒ AugeasTree
Returns the parsed data.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/cfa/augeas_parser.rb', line 314 def parse(raw_string) require "cfa/augeas_parser/reader" # Workaround for augeas lenses that don't handle files # without a trailing newline (bsc#1064623, bsc#1074891, bsc#1080051 # and gh#hercules-team/augeas#547) raw_string += "\n" unless raw_string.end_with?("\n") @old_content = raw_string # open augeas without any autoloading and it should not touch disk and # load lenses as needed only root = load_path = nil Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug| aug.set("/input", raw_string) report_error(aug) unless aug.text_store(@lens, "/input", "/store") return AugeasReader.read(aug, "/store") end end |
#serialize(data) ⇒ String
Returns a string to be written.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/cfa/augeas_parser.rb', line 335 def serialize(data) require "cfa/augeas_parser/writer" # open augeas without any autoloading and it should not touch disk and # load lenses as needed only root = load_path = nil Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug| aug.set("/input", @old_content || "") aug.text_store(@lens, "/input", "/store") if @old_content AugeasWriter.new(aug).write("/store", data) res = aug.text_retrieve(@lens, "/input", "/store", "/output") report_error(aug) unless res return aug.get("/output") end end |