Class: Net::LDAP::Dataset

Inherits:
Hash
  • Object
show all
Defined in:
lib/net/ldap/dataset.rb

Overview

An LDAP Dataset. Used primarily as an intermediate format for converting to and from LDIF strings and Net::LDAP::Entry objects.

Defined Under Namespace

Classes: ChompedIO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Dataset

:nodoc:



10
11
12
13
# File 'lib/net/ldap/dataset.rb', line 10

def initialize(*args, &block) # :nodoc:
  super
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Dataset object comments.



8
9
10
# File 'lib/net/ldap/dataset.rb', line 8

def comments
  @comments
end

Class Method Details

.from_entry(entry) ⇒ Object

Creates a Dataset object from an Entry object. Used mostly to assist with the conversion of



96
97
98
99
100
101
102
103
104
105
# File 'lib/net/ldap/dataset.rb', line 96

def from_entry(entry)
  dataset = Net::LDAP::Dataset.new
  hash = { }
  entry.each_attribute do |attribute, value|
    next if attribute == :dn
    hash[attribute] = value
  end
  dataset[entry.dn] = hash
  dataset
end

.read_ldif(io) ⇒ Object

Reads an object that returns data line-wise (using #gets) and parses LDIF data into a Dataset object.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/net/ldap/dataset.rb', line 110

def read_ldif(io)
  ds = Net::LDAP::Dataset.new
  io = ChompedIO.new(io)

  line = io.gets
  dn = nil

  while line
    new_line = io.gets

    if new_line =~ /^ /
      line << $'
    else
      nextline = new_line

      if line =~ /^#/
        ds.comments << line
        yield :comment, line if block_given?
      elsif line =~ /^dn:[\s]*/i
        dn = $'
        ds[dn] = Hash.new { |k,v| k[v] = [] }
        yield :dn, dn if block_given?
      elsif line.empty?
        dn = nil
        yield :end, nil if block_given?
      elsif line =~ /^([^:]+):([\:]?)[\s]*/
        # $1 is the attribute name
        # $2 is a colon iff the attr-value is base-64 encoded
        # $' is the attr-value
        # Avoid the Base64 class because not all Ruby versions have it.
        attrvalue = ($2 == ":") ? $'.unpack('m').shift : $'
        ds[dn][$1.downcase.to_sym] << attrvalue
        yield :attr, [$1.downcase.to_sym, attrvalue] if block_given?
      end

      line = nextline
    end
  end

  ds
end

Instance Method Details

#to_entriesObject

Convert the parsed LDIF objects to Net::LDAP::Entry objects.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/net/ldap/dataset.rb', line 51

def to_entries
  ary = []
  keys.each do |dn|
    entry = Net::LDAP::Entry.new(dn)
    self[dn].each do |attr, value|
      entry[attr] = value
    end
    ary << entry
  end
  ary
end

#to_ldifObject

Outputs an LDAP Dataset as an array of strings representing LDIF entries.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/net/ldap/dataset.rb', line 18

def to_ldif
  ary = []
  ary += @comments unless @comments.empty?
  keys.sort.each do |dn|
    ary << "dn: #{dn}"

    attributes = self[dn].keys.map { |attr| attr.to_s }.sort
    attributes.each do |attr|
      self[dn][attr.to_sym].each do |value|
        if attr == "userpassword" or value_is_binary?(value)
          value = [value].pack("m").chomp.gsub(/\n/m, "\n ")
          ary << "#{attr}:: #{value}"
        else
          ary << "#{attr}: #{value}"
        end
      end
    end

    ary << ""
  end
  block_given? and ary.each { |line| yield line}

  ary
end

#to_ldif_stringObject

Outputs an LDAP Dataset as an LDIF string.



45
46
47
# File 'lib/net/ldap/dataset.rb', line 45

def to_ldif_string
  to_ldif.join("\n")
end