Method: LDAP::LDIF.parse_file
- Defined in:
- lib/ldap/ldif.rb
.parse_file(file, sort = false) ⇒ Object
Open and parse a file containing LDIF entries. file
should be a string containing the path to the file. If sort
is true, the resulting array of LDAP::Record objects will be sorted on DN length, which can be useful to avoid a later attempt to process an entry whose parent does not yet exist. This can easily happen if your LDIF file is unordered, which is likely if it was produced with a tool such as slapcat(8).
If a block is given, each LDAP::Record object will be yielded to the block and nil will be returned instead of the array. This is much less memory-intensive when parsing a large LDIF file.
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/ldap/ldif.rb', line 434 def LDIF.parse_file( file, sort=false ) # :yield: record File.open( file ) do |f| entries = [] entry = false header = true version = false while line = f.gets if line =~ /^dn:/ header = false if entry && ! version if block_given? yield parse_entry( entry ) else entries << parse_entry( entry ) end end if version entry << line version = false else entry = [ line ] end next end if header && line.downcase =~ /^version/ entry = [ line ] version = true next end entry << line end if block_given? yield parse_entry( entry ) nil else entries << parse_entry( entry ) # Sort entries if sorting has been requested. entries.sort! { |x,y| x.dn.length <=> y.dn.length } if sort entries end end end |