Skip to content

Commit 36c2d4b

Browse files
author
emiel
committed
Fixes and test cases for Net::LDAP::Entry
* Ensure Entry attributes can be appended using << * Ensure Entry can be marshalled * Add test cases for the above * Re-factor a bit git-svn-id: http://net-ldap.rubyforge.org/svn/trunk@288 005445c4-6811-0410-8301-cae4f0071d1d
1 parent 063e261 commit 36c2d4b

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

lib/net/ldap/entry.rb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,27 @@ class Entry
7575

7676
# This constructor is not generally called by user code.
7777
#--
78-
# Originally, myhash took a block so we wouldn't have to
78+
# Originally, attributes took a block so we wouldn't have to
7979
# make sure its elements returned empty arrays when necessary.
8080
# Got rid of that to enable marshalling of Entry objects,
8181
# but that doesn't work anyway, because Entry objects have
8282
# singleton methods. So we define a custom dump and load.
83-
def initialize dn = nil # :nodoc:
84-
@myhash = {} # originally: Hash.new {|k,v| k[v] = [] }
85-
@myhash[:dn] = [dn]
83+
def initialize( dn = nil ) # :nodoc:
84+
@attributes = {} # originally: Hash.new {|k,v| k[v] = [] }
85+
@attributes[:dn] = [dn]
8686
end
8787

88-
def _dump depth
88+
def attributes
89+
@attributes
90+
end
91+
92+
protected :attributes
93+
94+
def ==( other )
95+
self.attributes == other.attributes
96+
end
97+
98+
def _dump( depth )
8999
to_ldif
90100
end
91101

@@ -98,21 +108,20 @@ def _load entry
98108
#--
99109
# Discovered bug, 26Aug06: I noticed that we're not converting the
100110
# incoming value to an array if it isn't already one.
101-
def []= name, value # :nodoc:
111+
def []=( name, value ) # :nodoc:
102112
sym = name.to_s.downcase.intern
103113
value = [value] unless value.is_a?(Array)
104-
@myhash[sym] = value
114+
@attributes[sym] = value
105115
end
106116

107-
108117
#--
109118
# We have to deal with this one as we do with []=
110119
# because this one and not the other one gets called
111120
# in formulations like entry["CN"] << cn.
112121
#
113-
def [] name # :nodoc:
122+
def []( name ) # :nodoc:
114123
name = name.to_s.downcase.intern unless name.is_a?(Symbol)
115-
@myhash[name] || []
124+
@attributes[name] || @attributes[name] = []
116125
end
117126

118127
# Returns the dn of the Entry as a String.
@@ -122,7 +131,7 @@ def dn
122131

123132
# Returns an array of the attribute names present in the Entry.
124133
def attribute_names
125-
@myhash.keys
134+
@attributes.keys
126135
end
127136

128137
# Accesses each of the attributes present in the Entry.
@@ -133,17 +142,15 @@ def attribute_names
133142
#
134143
def each
135144
if block_given?
136-
attribute_names.each {|a|
145+
attribute_names.each do |a|
137146
attr_name,values = a,self[a]
138147
yield attr_name, values
139-
}
148+
end
140149
end
141150
end
142151

143152
alias_method :each_attribute, :each
144153

145-
146-
147154
# Converts the Entry to a String, representing the
148155
# Entry's attributes in LDIF format.
149156
#--
@@ -232,15 +239,15 @@ def method_missing *args, &block # :nodoc:
232239
def write
233240
end
234241

235-
236242
#--
237243
# Internal convenience method. It seems like the standard
238244
# approach in most LDAP tools to base64 encode an attribute
239245
# value if its first or last byte is nonprintable, or if
240246
# it's a password. But that turns out to be not nearly good
241247
# enough. There are plenty of A/D attributes that are binary
242248
# in the middle. This is probably a nasty performance killer.
243-
def is_attribute_value_binary? value
249+
#
250+
def is_attribute_value_binary?( value )
244251
v = value.to_s
245252
v.each_byte {|byt|
246253
return true if (byt < 32) || (byt > 126)
@@ -250,11 +257,9 @@ def is_attribute_value_binary? value
250257
end
251258
false
252259
end
260+
253261
private :is_attribute_value_binary?
254262

255263
end # class Entry
256264

257-
end # class LDAP
258-
end # module Net
259-
260-
265+
end; end # class Net::LDAP

test/test_entry.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class TestEntry < Test::Unit::TestCase
4545
assert_equal ['Jensen'], @entry.sn
4646
end
4747

48+
should 'have attributes which are appendable using <<' do
49+
@entry['sn'] << 'Jensen'
50+
@entry['sn'] << 'Johnson'
51+
assert_equal %w(Jensen Johnson), @entry['sn']
52+
end
53+
4854
should 'ignore case of attribute names' do
4955
@entry['sn'] = 'Jensen'
5056
assert_equal ['Jensen'], @entry.sn
@@ -54,5 +60,15 @@ class TestEntry < Test::Unit::TestCase
5460
assert_equal ['Jensen'], @entry['Sn']
5561
assert_equal ['Jensen'], @entry['SN']
5662
end
63+
64+
should 'be capable of being (un)marshaled' do
65+
@entry['sn'] << 'Jensen'
66+
@entry['sn'] << 'Johnson'
67+
68+
entry_dump = Marshal.dump(@entry)
69+
entry_load = Marshal.load(entry_dump)
70+
71+
assert_equal(@entry, entry_load)
72+
end
5773
end
5874
end

0 commit comments

Comments
 (0)