diff --git a/lib/net/ldap/dataset.rb b/lib/net/ldap/dataset.rb index db01ce4..a1c80a7 100644 --- a/lib/net/ldap/dataset.rb +++ b/lib/net/ldap/dataset.rb @@ -60,7 +60,7 @@ class Dataset < Hash # $1 is the attribute name # $2 is a colon iff the attr-value is base-64 encoded # $' is the attr-value - #attrvalue = ($2 == ":") ? Base64.decode64($') : $' + # Avoid the Base64 class because not all Ruby versions have it. attrvalue = ($2 == ":") ? $'.unpack('m').shift : $' ds[dn][$1.downcase.intern] << attrvalue end @@ -78,6 +78,28 @@ class Dataset < Hash end + def to_ldif + ary = [] + ary += (@comments || []) + + keys.sort.each {|dn| + ary << "dn: #{dn}" + + self[dn].keys.map {|sym| sym.to_s}.sort.each {|attr| + self[dn][attr.intern].each {|val| + ary << "#{attr}: #{val}" + } + } + + ary << "" + } + + block_given? and ary.each {|line| yield line} + + ary + end + + end # Dataset end # LDAP diff --git a/tests/testldif.rb b/tests/testldif.rb index d42c44f..02604e5 100644 --- a/tests/testldif.rb +++ b/tests/testldif.rb @@ -5,6 +5,11 @@ $:.unshift "lib" +require 'test/unit' +require 'tests/testber' +require 'tests/testldif' +require 'tests/testldap' + require 'net/ldap' require 'net/ldif' @@ -51,6 +56,16 @@ class TestLdif < Test::Unit::TestCase } end + # TODO, need some tests. + # Must test folded lines and base64-encoded lines as well as normal ones. + def test_to_ldif + File.open( TestLdifFilename, "r" ) {|f| + ds = Net::LDAP::Dataset::read_ldif( f ) + ds.to_ldif + assert_equal( true, false ) # REMOVE WHEN WE HAVE SOME TESTS HERE. + } + end + end