# class ActiveLdap::DistinguishedName

### Attributes

rdns[R]

### Public Class Methods

escape_value(value) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 161
    def escape_value(value)
      if /(\A | \z)/.match(value)
        '"' + value.gsub(/([\\"])/, '\\\1') + '"'
      else
        value.gsub(/([,=\+<>#;\\"])/, '\\\1')
      end
    end

new(*rdns) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 171
    def initialize(*rdns)
      @rdns = rdns.collect do |rdn|
        if rdn.is_a?(Array) and rdn.size == 2
          {rdn[0] => rdn[1]}
        else
          rdn
        end
      end
    end

parse(source) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 157
    def parse(source)
      Parser.new(source).parse
    end

### Public Instance Methods

+(other) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 185
    def +(other)
      self.class.new(*(@rdns + other.rdns))
    end

-(other) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 189
    def -(other)
      rdns = @rdns.dup
      normalized_rdns = normalize(@rdns)
      normalize(other.rdns).reverse_each do |rdn|
        if rdn == normalized_rdns.pop
          rdns.pop
        else
          raise ArgumentError, _("%s isn't sub DN of %s") % [other, self]
        end
      end
      self.class.new(*rdns)
    end

<<(rdn) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 202
    def <<(rdn)
      @rdns << rdn
      self
    end

<=>(other) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 216
    def <=>(other)
      other = DN.parse(other) if other.is_a?(String)
      return nil unless other.is_a?(self.class)
      normalize_for_comparing(@rdns) <=>
        normalize_for_comparing(other.rdns)
    end

==(other) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 223
    def ==(other)
      case other
      when self.class
        normalize(@rdns) == normalize(other.rdns)
      when String
        parsed_other = nil
        begin
          parsed_other = self.class.parse(other)
        rescue DistinguishedNameInvalid
          return false
        end
        self == parsed_other
      else
        false
      end
    end

blank?() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 181
    def blank?
      @rdns.blank?
    end

eql?(other) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 240
    def eql?(other)
      other.is_a?(self.class) and
        normalize(@rdns).to_s.eql?(normalize(other.rdns).to_s)
    end

hash() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 245
    def hash
      normalize(@rdns).to_s.hash
    end

parent() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 211
    def parent
      return nil if @rdns.size <= 1
      self.class.new(*@rdns[1..-1])
    end

to_human_readable_format() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 264
    def to_human_readable_format
      to_s.inspect
    end

to_s() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 249
    def to_s
      klass = self.class
      @rdns.collect do |rdn|
        rdn.sort_by do |type, value|
          type.upcase
        end.collect do |type, value|
          "#{type}=#{klass.escape_value(value)}"
        end.join("+")
      end.join(",")
    end

to_str() click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 260
    def to_str # for backward compatibility
      to_s
    end

unshift(rdn) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 207
    def unshift(rdn)
      @rdns.unshift(rdn)
    end

### Private Instance Methods

normalize(rdns) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 269
    def normalize(rdns)
      rdns.collect do |rdn|
        normalized_rdn = {}
        rdn.each do |key, value|
          normalized_rdn[key.upcase] = value.upcase
        end
        normalized_rdn
      end
    end

normalize_for_comparing(rdns) click to toggle source

    # File lib/active_ldap/distinguished_name.rb, line 279
    def normalize_for_comparing(rdns)
      normalize(rdns).collect do |rdn|
        rdn.sort_by do |key, value|
          key
        end
      end.collect do |key, value|
        [key, value]
      end
    end