class ActiveLdap::DistinguishedName

def +(other)

def +(other)
  self.class.new(*(@rdns + other.rdns))
end

def -(other)

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

def <<(rdn)

def <<(rdn)
  @rdns << rdn
  self
end

def <=>(other)

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

def ==(other)

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

def blank?

def blank?
  @rdns.blank?
end

def eql?(other)

def eql?(other)
  other.is_a?(self.class) and
    normalize(@rdns).to_s.eql?(normalize(other.rdns).to_s)
end

def escape_value(value)

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

def hash

def hash
  normalize(@rdns).to_s.hash
end

def initialize(*rdns)

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

def normalize(rdns)

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

def normalize_for_comparing(rdns)

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

def parent

def parent
  return nil if @rdns.size <= 1
  self.class.new(*@rdns[1..-1])
end

def parse(source)

def parse(source)
  Parser.new(source).parse
end

def to_human_readable_format

def to_human_readable_format
  to_s.inspect
end

def to_s

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

def to_str # for backward compatibility

for backward compatibility
def to_str # for backward compatibility
  to_s
end

def unshift(rdn)

def unshift(rdn)
  @rdns.unshift(rdn)
end