module ActiveLdap::Operations::Find

def all(*args)

to find(:all)
all the same arguments to this method as you can
This is an alias for find(:all). You can pass in
def all(*args)
  find(:all, *args)
end

def ensure_dn(target)

def ensure_dn(target)
  attr, value, prefix = split_search_value(target)
  "#{attr || dn_attribute}=#{value},#{prefix || base}"
end

def find(*args)

Subclass.find(:all, 'some*val')
usage: Subclass.find(:all, :attribute => "cn", :value => "some*val")
|field|, or the wildcard match. This is only useful for derived classes.
Finds the first match for value where |value| is the value of some

find
def find(*args)
  options = extract_options_from_args!(args)
  args = [:first] if args.empty? and !options.empty?
  case args.first
  when :first
    options[:value] ||= args[1]
    find_initial(options)
  when :last
    options[:value] ||= args[1]
    find_last(options)
  when :all
    options[:value] ||= args[1]
    find_every(options)
  else
    find_from_dns(args, options)
  end
end

def find_every(options)

def find_every(options)
  options = options.dup
  sort_by = options.delete(:sort_by) || self.sort_by
  order = options.delete(:order) || self.order
  limit = options.delete(:limit) if sort_by or order
  offset = options.delete(:offset) || offset
  options[:attributes] = options.delete(:attributes) || ['*']
  options[:attributes] |= ['objectClass']
  if options.delete(:include_operational_attributes)
    options[:attributes] |= ["+"]
  end
  results = search(options).collect do |dn, attrs|
    instantiate([dn, attrs, {:connection => options[:connection]}])
  end
  return results if sort_by.nil? and order.nil?
  sort_by ||= "dn"
  if sort_by.downcase == "dn"
    results = results.sort_by {|result| DN.parse(result.dn)}
  else
    results = results.sort_by {|result| result.send(sort_by)}
  end
  results.reverse! if normalize_sort_order(order || "ascend") == :descend
  results = results[offset, results.size] if offset
  results = results[0, limit] if limit
  results
end

def find_from_dns(dns, options)

def find_from_dns(dns, options)
  expects_array = dns.first.is_a?(Array)
  return [] if expects_array and dns.first.empty?
  dns = dns.flatten.compact.uniq
  case dns.size
  when 0
    raise EntryNotFound, _("Couldn't find %s without a DN") % name
  when 1
    result = find_one(dns.first, options)
    expects_array ? [result] : result
  else
    find_some(dns, options)
  end
end

def find_initial(options)

def find_initial(options)
  find_every(options.merge(:limit => 1)).first
end

def find_last(options)

def find_last(options)
  order = options[:order] || self.order || 'ascend'
  order = normalize_sort_order(order) == :ascend ? :descend : :ascend
  find_initial(options.merge(:order => order))
end

def find_one(dn, options)

def find_one(dn, options)
  attr, value, prefix = split_search_value(dn)
  filter = [attr || ensure_search_attribute,
            Escape.ldap_filter_escape(value)]
  filter = [:and, filter, options[:filter]] if options[:filter]
  options = {:prefix => prefix}.merge(options.merge(:filter => filter))
  result = find_initial(options)
  if result
    result
  else
    args = [self.is_a?(Class) ? name : self.class.name,
            dn]
    if options[:filter]
      format = _("Couldn't find %s: DN: %s: filter: %s")
      args << options[:filter].inspect
    else
      format = _("Couldn't find %s: DN: %s")
    end
    raise EntryNotFound, format % args
  end
end

def find_some(dns, options)

def find_some(dns, options)
  dn_filters = dns.collect do |dn|
    attr, value, prefix = split_search_value(dn)
    attr ||= ensure_search_attribute
    filter = [attr, value]
    if prefix
      filter = [:and,
                filter,
                [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]]
    end
    filter
  end
  filter = [:or, *dn_filters]
  filter = [:and, filter, options[:filter]] if options[:filter]
  result = find_every(options.merge(:filter => filter))
  if result.size == dns.size
    result
  else
    args = [self.is_a?(Class) ? name : self.class.name,
            dns.join(", ")]
    if options[:filter]
      format = _("Couldn't find all %s: DNs (%s): filter: %s")
      args << options[:filter].inspect
    else
      format = _("Couldn't find all %s: DNs (%s)")
    end
    raise EntryNotFound, format % args
  end
end

def first(*args)

to this method as you can to find(:first).
*args). You can pass in all the same arguments
A convenience wrapper for find(:first,
def first(*args)
  find(:first, *args)
end

def last(*args)

to this method as you can to find(:last).
*args)
. You can pass in all the same arguments
A convenience wrapper for find(:last,
def last(*args)
  find(:last, *args)
end

def normalize_sort_order(value)

def normalize_sort_order(value)
  case value.to_s
  when /\Aasc(?:end)?\z/i
    :ascend
  when /\Adesc(?:end)?\z/i
    :descend
  else
    raise ArgumentError, _("Invalid order: %s") % value.inspect
  end
end