module ActiveLdap::DynamicFinders

Constants

VERSION

Public Class Methods

respond_to?(meth) click to toggle source

Respond to dynamic finders

Calls superclass method
# File lib/active_ldap/dynamic_finders.rb, line 14
def respond_to?(meth)
  if meth.to_s =~ /^find_by_.*$/
    true
  else
    super
  end
end

Private Class Methods

method_missing(meth, *args, &block) click to toggle source

Override method_missing to run dynamic finder

Calls superclass method
# File lib/active_ldap/dynamic_finders.rb, line 25
def method_missing(meth, *args, &block)
  if meth.to_s =~ /^find_by_(.+)$/
    run_find_by_method(Regexp.last_match[1], *args)
  else
    super               
  end
end
run_find_by_method(attrs, *args) click to toggle source

Run dynamic finder

# File lib/active_ldap/dynamic_finders.rb, line 34
def run_find_by_method(attrs, *args)
  attrs = attrs.split('_and_')

  raise(ArgumentError, "wrong number of arguments (#{args.size} for #{attrs.size})") if attrs.size != args.size

  attrs_with_args = [attrs, args].transpose
  conditions = Hash[attrs_with_args]
  self.find(:all, filter: [:and, conditions])
end