Class: Net::LDAP::Dataset

### Home Classes Methods

### In Files

- lib/net/ldap/dataset.rb

### Parent

Hash

### Methods

- ::from_entry
- ::read_ldif
- #to_entries
- #to_ldif
- #to_ldif_string

### Files

- History.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::BER
- Net::BER::BERParser
- Net::BER::BerError
- Net::BER::BerIdentifiedArray
- Net::BER::BerIdentifiedNull
- Net::BER::BerIdentifiedOid
- Net::BER::BerIdentifiedString
- Net::BER::Extensions
- Net::BER::Extensions::Array
- Net::BER::Extensions::FalseClass
- Net::BER::Extensions::Integer
- Net::BER::Extensions::String
- Net::BER::Extensions::TrueClass
- Net::LDAP
- Net::LDAP::AlreadyOpenedError
- Net::LDAP::AuthMethodUnsupportedError
- Net::LDAP::BERInvalidError
- Net::LDAP::BadAttributeError
- Net::LDAP::BindingInformationInvalidError
- Net::LDAP::Connection
- Net::LDAP::Connection::FixSSLSocketSyncClose
- Net::LDAP::Connection::GetbyteForSSLSocket
- Net::LDAP::ConnectionRefusedError
- Net::LDAP::DN
- Net::LDAP::Dataset
- Net::LDAP::EmptyDNError
- Net::LDAP::EncMethodUnsupportedError
- Net::LDAP::EncryptionUnsupportedError
- Net::LDAP::Entry
- Net::LDAP::EntryOverflowError
- Net::LDAP::Error
- Net::LDAP::Filter
- Net::LDAP::FilterSyntaxInvalidError
- Net::LDAP::FilterTypeUnknownError
- Net::LDAP::HashTypeUnsupportedError
- Net::LDAP::Instrumentation
- Net::LDAP::LDAPControls
- Net::LDAP::LdapError
- Net::LDAP::NoBindResultError
- Net::LDAP::NoOpenSSLError
- Net::LDAP::NoSearchBaseError
- Net::LDAP::NoStartTLSResultError
- Net::LDAP::OperatorError
- Net::LDAP::PDU
- Net::LDAP::PDU::Error
- Net::LDAP::Password
- Net::LDAP::ResponseMissingOrInvalidError
- Net::LDAP::ResponseTypeInvalidError
- Net::LDAP::SASLChallengeOverflowError
- Net::LDAP::SearchFilterError
- Net::LDAP::SearchFilterTypeUnknownError
- Net::LDAP::SearchScopeInvalidError
- Net::LDAP::SearchSizeInvalidError
- Net::LDAP::SocketError
- Net::LDAP::StartTLSError
- Net::LDAP::SubstringFilterError
- OpenSSL
- OpenSSL::SSL
- OpenSSL::SSL::SSLSocket

No matching classes.

# Net::LDAP::Dataset

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

### Attributes

comments[R]
version[RW]

Dataset object version, comments.

### Public Class Methods

from_entry(entry) click to toggle source

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

    # File lib/net/ldap/dataset.rb, line 103
    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) click to toggle source

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

    # File lib/net/ldap/dataset.rb, line 117
    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 =~ /^version:[\s]*([0-9]+)$/
            ds.version = $1
            yield :version, line if block_given?
          elsif line =~ /^dn:([\:]?)[\s]*/
            # $1 is a colon if the dn-value is base-64 encoded
            # $' is the dn-value
            # Avoid the Base64 class because not all Ruby versions have it.
            dn = ($1 == ":") ? $'.unpack('m').shift : $'
            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

### Public Instance Methods

to_entries() click to toggle source

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

    # File lib/net/ldap/dataset.rb, line 58
    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_ldif() click to toggle source

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

    # File lib/net/ldap/dataset.rb, line 19
    def to_ldif
      ary = []

      if version
        ary << "version: #{version}"
        ary << ""
      end

      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/, "\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_string() click to toggle source

Outputs an LDAP Dataset as an LDIF string.

    # File lib/net/ldap/dataset.rb, line 52
    def to_ldif_string
      to_ldif.join("\n")
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.