annotate vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/dataset.rb @ 857:a1a87bc044b8 feature_199

Close obsolete branch feature_199
author Chris Cannam
date Wed, 13 Jul 2011 11:51:42 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 # $Id: dataset.rb 78 2006-04-26 02:57:34Z blackhedd $
Chris@0 2 #
Chris@0 3 #
Chris@0 4 #----------------------------------------------------------------------------
Chris@0 5 #
Chris@0 6 # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
Chris@0 7 #
Chris@0 8 # Gmail: garbagecat10
Chris@0 9 #
Chris@0 10 # This program is free software; you can redistribute it and/or modify
Chris@0 11 # it under the terms of the GNU General Public License as published by
Chris@0 12 # the Free Software Foundation; either version 2 of the License, or
Chris@0 13 # (at your option) any later version.
Chris@0 14 #
Chris@0 15 # This program is distributed in the hope that it will be useful,
Chris@0 16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 18 # GNU General Public License for more details.
Chris@0 19 #
Chris@0 20 # You should have received a copy of the GNU General Public License
Chris@0 21 # along with this program; if not, write to the Free Software
Chris@0 22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Chris@0 23 #
Chris@0 24 #---------------------------------------------------------------------------
Chris@0 25 #
Chris@0 26 #
Chris@0 27
Chris@0 28
Chris@0 29
Chris@0 30
Chris@0 31 module Net
Chris@0 32 class LDAP
Chris@0 33
Chris@0 34 class Dataset < Hash
Chris@0 35
Chris@0 36 attr_reader :comments
Chris@0 37
Chris@0 38
Chris@0 39 def Dataset::read_ldif io
Chris@0 40 ds = Dataset.new
Chris@0 41
Chris@0 42 line = io.gets && chomp
Chris@0 43 dn = nil
Chris@0 44
Chris@0 45 while line
Chris@0 46 io.gets and chomp
Chris@0 47 if $_ =~ /^[\s]+/
Chris@0 48 line << " " << $'
Chris@0 49 else
Chris@0 50 nextline = $_
Chris@0 51
Chris@0 52 if line =~ /^\#/
Chris@0 53 ds.comments << line
Chris@0 54 elsif line =~ /^dn:[\s]*/i
Chris@0 55 dn = $'
Chris@0 56 ds[dn] = Hash.new {|k,v| k[v] = []}
Chris@0 57 elsif line.length == 0
Chris@0 58 dn = nil
Chris@0 59 elsif line =~ /^([^:]+):([\:]?)[\s]*/
Chris@0 60 # $1 is the attribute name
Chris@0 61 # $2 is a colon iff the attr-value is base-64 encoded
Chris@0 62 # $' is the attr-value
Chris@0 63 # Avoid the Base64 class because not all Ruby versions have it.
Chris@0 64 attrvalue = ($2 == ":") ? $'.unpack('m').shift : $'
Chris@0 65 ds[dn][$1.downcase.intern] << attrvalue
Chris@0 66 end
Chris@0 67
Chris@0 68 line = nextline
Chris@0 69 end
Chris@0 70 end
Chris@0 71
Chris@0 72 ds
Chris@0 73 end
Chris@0 74
Chris@0 75
Chris@0 76 def initialize
Chris@0 77 @comments = []
Chris@0 78 end
Chris@0 79
Chris@0 80
Chris@0 81 def to_ldif
Chris@0 82 ary = []
Chris@0 83 ary += (@comments || [])
Chris@0 84
Chris@0 85 keys.sort.each {|dn|
Chris@0 86 ary << "dn: #{dn}"
Chris@0 87
Chris@0 88 self[dn].keys.map {|sym| sym.to_s}.sort.each {|attr|
Chris@0 89 self[dn][attr.intern].each {|val|
Chris@0 90 ary << "#{attr}: #{val}"
Chris@0 91 }
Chris@0 92 }
Chris@0 93
Chris@0 94 ary << ""
Chris@0 95 }
Chris@0 96
Chris@0 97 block_given? and ary.each {|line| yield line}
Chris@0 98
Chris@0 99 ary
Chris@0 100 end
Chris@0 101
Chris@0 102
Chris@0 103 end # Dataset
Chris@0 104
Chris@0 105 end # LDAP
Chris@0 106 end # Net
Chris@0 107
Chris@0 108