Added Dataset::to_ldif

This commit is contained in:
blackhedd 2006-04-18 19:46:47 +00:00
parent c423ced9e2
commit 7a0da7c9bc
2 changed files with 38 additions and 1 deletions

View file

@ -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

View file

@ -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