diff --git a/lib/net/ldap/entry.rb b/lib/net/ldap/entry.rb index 10965c7c..418512f0 100644 --- a/lib/net/ldap/entry.rb +++ b/lib/net/ldap/entry.rb @@ -133,6 +133,13 @@ def attribute_names @myhash.keys end + ## + # Creates a duplicate of the internal Hash containing the attributes + # of the entry. + def to_h + @myhash.dup + end + ## # Accesses each of the attributes present in the Entry. # diff --git a/test/test_entry.rb b/test/test_entry.rb index e2184747..6667eab9 100644 --- a/test/test_entry.rb +++ b/test/test_entry.rb @@ -39,6 +39,21 @@ def test_case_insensitive_attribute_names assert_equal ['Jensen'], @entry['Sn'] assert_equal ['Jensen'], @entry['SN'] end + + def test_to_h + @entry['sn'] = 'Jensen' + expected = { + dn: ['cn=Barbara,o=corp'], + sn: ['Jensen'], + } + duplicate = @entry.to_h + assert_equal expected, duplicate + + # check that changing the duplicate + # does not affect the internal state + duplicate.delete(:sn) + assert_not_equal duplicate, @entry.to_h + end end class TestEntryLDIF < Test::Unit::TestCase