Class: CFA::AugeasParser
- Inherits:
-
Object
- Object
- CFA::AugeasParser
- Defined in:
- lib/cfa/augeas_parser.rb
Overview
Instance Attribute Summary collapse
-
#file_name ⇒ String
Optional, used for error reporting.
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.
378 379 380 381 |
# File 'lib/cfa/augeas_parser.rb', line 378 def initialize(lens) @lens = lens @file_name = nil end |
Instance Attribute Details
#file_name ⇒ String
Returns optional, used for error reporting.
375 376 377 |
# File 'lib/cfa/augeas_parser.rb', line 375 def file_name @file_name end |
Instance Method Details
#empty ⇒ AugeasTree
Returns an empty tree that can be filled for future serialization.
426 427 428 |
# File 'lib/cfa/augeas_parser.rb', line 426 def empty AugeasTree.new end |
#parse(raw_string) ⇒ AugeasTree
Returns the parsed data.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/cfa/augeas_parser.rb', line 385 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, :parsing, file_name, raw_string) \ unless aug.text_store(@lens, "/input", "/store") return AugeasReader.read(aug, "/store") end end |
#serialize(data) ⇒ String
Returns a string to be written.
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/cfa/augeas_parser.rb', line 407 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, :serializing, file_name, data) unless res return aug.get("/output") end end |