Skip to content

Commit 7a0da7c

Browse files
author
blackhedd
committed
Added Dataset::to_ldif
1 parent c423ced commit 7a0da7c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/net/ldap/dataset.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def Dataset::read_ldif io
6060
# $1 is the attribute name
6161
# $2 is a colon iff the attr-value is base-64 encoded
6262
# $' is the attr-value
63-
#attrvalue = ($2 == ":") ? Base64.decode64($') : $'
63+
# Avoid the Base64 class because not all Ruby versions have it.
6464
attrvalue = ($2 == ":") ? $'.unpack('m').shift : $'
6565
ds[dn][$1.downcase.intern] << attrvalue
6666
end
@@ -78,6 +78,28 @@ def initialize
7878
end
7979

8080

81+
def to_ldif
82+
ary = []
83+
ary += (@comments || [])
84+
85+
keys.sort.each {|dn|
86+
ary << "dn: #{dn}"
87+
88+
self[dn].keys.map {|sym| sym.to_s}.sort.each {|attr|
89+
self[dn][attr.intern].each {|val|
90+
ary << "#{attr}: #{val}"
91+
}
92+
}
93+
94+
ary << ""
95+
}
96+
97+
block_given? and ary.each {|line| yield line}
98+
99+
ary
100+
end
101+
102+
81103
end # Dataset
82104

83105
end # LDAP

tests/testldif.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
$:.unshift "lib"
77

8+
require 'test/unit'
9+
require 'tests/testber'
10+
require 'tests/testldif'
11+
require 'tests/testldap'
12+
813
require 'net/ldap'
914
require 'net/ldif'
1015

@@ -51,6 +56,16 @@ def test_ldif
5156
}
5257
end
5358

59+
# TODO, need some tests.
60+
# Must test folded lines and base64-encoded lines as well as normal ones.
61+
def test_to_ldif
62+
File.open( TestLdifFilename, "r" ) {|f|
63+
ds = Net::LDAP::Dataset::read_ldif( f )
64+
ds.to_ldif
65+
assert_equal( true, false ) # REMOVE WHEN WE HAVE SOME TESTS HERE.
66+
}
67+
end
68+
5469

5570
end
5671

0 commit comments

Comments
 (0)