class ASF::Base
Superclass for all classes which are backed by LDAP
data. Encapsulates the management of collections to weak references to instance data, for both performance and functional reasons. Sequentially finding the same same object will return the same instance unless the prior instance has been reclaimed by garbage collection. This often prevents large numbers of requests to fetch the same data from LDAP
.
This class also contains a number of helper classes that will construct various LDAP
mod
objects.
Attributes
Public Class Methods
Source
# File lib/whimsy/asf/ldap.rb, line 496 def self.[](name) new(name) end
Find an instance of this class, given a name
Source
# File lib/whimsy/asf/ldap.rb, line 479 def self.base @base end
return the LDAP
base for this object: identifies the subtree where this object can be found.
Source
# File lib/whimsy/asf/ldap.rb, line 491 def self.collection @collection ||= {} end
return the collection of instances of this class, as a hash. Note the values are weak references, so may have already been reclaimed.
Source
# File lib/whimsy/asf/ldap.rb, line 501 def self.find(name) new(name) end
Find an instance of this class, given a name
Source
# File lib/whimsy/asf/ldap.rb, line 568 def self.ldap_search(filter, attributes=['dn']) raise ArgumentError.new "Cannot be used for #{self.name} instances" unless base ASF.search_one(base, filter, [attributes].flatten) end
Low-level search for retrieving LDAP
entries. Assumes scope ONE Returns list of hashes, where the keys are the attributes Optionally provide a list of attributes to return, e.g. [‘uid’,‘mail’] Always includes ‘dn’ in the hashes
Source
# File lib/whimsy/asf/ldap.rb, line 545 def self.mod_add(attr, vals) ::LDAP::Mod.new(::LDAP::LDAP_MOD_ADD, attr.to_s, Array(vals)) end
helper method to construct LDAP_MOD_ADD objects
Source
# File lib/whimsy/asf/ldap.rb, line 556 def self.mod_delete(attr, vals) ::LDAP::Mod.new(::LDAP::LDAP_MOD_DELETE, attr.to_s, Array(vals)) end
helper method to construct LDAP_MOD_DELETE objects
Source
# File lib/whimsy/asf/ldap.rb, line 550 def self.mod_replace(attr, vals) vals = Array(vals) unless vals.is_a? Hash ::LDAP::Mod.new(::LDAP::LDAP_MOD_REPLACE, attr.to_s, vals) end
helper method to construct LDAP_MOD_REPLACE objects
Source
# File lib/whimsy/asf/ldap.rb, line 507 def self.new(name) begin object = collection[name] return object.reference if object&.weakref_alive? rescue end super end
Create an instance of this class, given a name. Note: if an instance already exists, it will return a handle to the existing object.
Source
# File lib/whimsy/asf/ldap.rb, line 520 def initialize(name) self.class.collection[name] = WeakRef.new(self) @name = name end
create an instance of this class, returning a weak reference to the object for reuse. Note: self.new will check for such a reference and return it in favor of allocating a new object.
Public Instance Methods
Source
# File lib/whimsy/asf/ldap.rb, line 473 def <=>(other) @name <=> other.name end
define default sort key (make Base
objects sortable)
Source
# File lib/whimsy/asf/ldap.rb, line 485 def base self.class.base end
return the LDAP
base for this object: identifies the subtree where this object can be found.
Source
# File lib/whimsy/asf/ldap.rb, line 560 def hasLDAP? ASF.search_one(base, "cn=#{name}", 'cn').any? end
Source
# File lib/whimsy/asf/ldap.rb, line 540 def id @name end
Return the simple name for this LDAP
object. This is the value of uid
for people objects, and the value of cn
for all other objects.
Source
# File lib/whimsy/asf/ldap.rb, line 527 def reference self end
returns a reference to the underlying object. Useful for converting weak references to strong references.
Source
# File lib/whimsy/asf/ldap.rb, line 533 def weakref(attr, &block) ASF.dereference_weakref(self, attr, &block) end
construct a weak reference to this object N.B. weakref(:XYZ) stores the reference in @XYZ