Module: Treequel::Model::ObjectClass
- Extended by:
- Delegation
- Includes:
- Enumerable, HashUtilities
- Defined in:
- lib/treequel/model/objectclass.rb
Overview
Mixin that provides Treequel::Model characteristics to a mixin module.
Class Method Summary collapse
-
.extended(mod) ⇒ Object
Extension callback – add data structures to the extending
mod. -
.included(mod) ⇒ Object
Inclusion callback – Methods should be applied to the module rather than an instance.
Instance Method Summary collapse
-
#create(directory, dn = nil, entryhash = {}) ⇒ Object
:call-seq: ObjectClassModule.create( dn, entryhash={} ) ObjectClassModule.create( directory, dn, entryhash={} ).
-
#model_bases(*base_dns) ⇒ Object
Set or get base DNs that the mixin applies to.
-
#model_class(mclass = nil) ⇒ Object
Declare which Treequel::Model subclasses the mixin will register itself with.
-
#model_objectclasses(*objectclasses) ⇒ Object
Set or get objectClasses that the mixin requires.
-
#search(directory = nil) ⇒ Object
Return a Branchset (or BranchCollection if the receiver has more than one base) that can be used to search the given
directoryfor entries to which the receiver applies.
Methods included from Delegation
def_ivar_delegators, def_method_delegators
Methods included from HashUtilities
merge_recursively, normalize_attributes, stringify_keys, symbolify_keys
Class Method Details
.extended(mod) ⇒ Object
Extension callback – add data structures to the extending mod.
20 21 22 23 24 25 |
# File 'lib/treequel/model/objectclass.rb', line 20 def self::extended( mod ) mod.instance_variable_set( :@model_class, Treequel::Model ) mod.instance_variable_set( :@model_objectclasses, [] ) mod.instance_variable_set( :@model_bases, [] ) super end |
.included(mod) ⇒ Object
Inclusion callback – Methods should be applied to the module rather than an instance. Warn the user if they use include() and extend() instead.
30 31 32 33 |
# File 'lib/treequel/model/objectclass.rb', line 30 def self::included( mod ) warn "extending %p rather than appending features to it" % [ mod ] mod.extend( self ) end |
Instance Method Details
#create(directory, dn = nil, entryhash = {}) ⇒ Object
:call-seq:
ObjectClassModule.create( dn, entryhash={} )
ObjectClassModule.create( directory, dn, entryhash={} )
In the first form, creates a new instance of the mixin’s model_class in the model_class’s default directory with the given dn and the objectclasses specified by the mixin.
In the second form, creates a new instance of the mixin’s model_class in the specified directory with the given dn and the objectclasses specified by the mixin.
If the optional entryhash is given (in either form), it will be used as the initial attributes of the new entry.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/treequel/model/objectclass.rb', line 102 def create( directory, dn=nil, entryhash={} ) # Shift the arguments if the first one isn't a directory unless directory.is_a?( Treequel::Directory ) entryhash = dn || {} dn = directory directory = self.model_class.directory end entryhash = stringify_keys( entryhash ) # Add the objectclasses from the mixin entryhash['objectClass'] ||= [] entryhash['objectClass'].collect!( &:to_s ) entryhash['objectClass'] |= self.model_objectclasses.map( &:to_s ) # Add all the attribute pairs from the RDN bit of the DN to the entry rdn_pair, _ = dn.split( /\s*,\s*/, 2 ) rdn_pair.split( /\+/ ).each do |attrpair| k, v = attrpair.split( /\s*=\s*/ ) entryhash[ k ] ||= [] entryhash[ k ] << v unless entryhash[ k ].include?( v ) end return self.model_class.new( directory, dn, entryhash ) end |
#model_bases(*base_dns) ⇒ Object
Set or get base DNs that the mixin applies to.
80 81 82 83 84 85 86 87 |
# File 'lib/treequel/model/objectclass.rb', line 80 def model_bases( *base_dns ) unless base_dns.empty? @model_bases = base_dns.collect {|dn| dn.gsub(/\s+/, '') } @model_class.register_mixin( self ) end return @model_bases.dup end |
#model_class(mclass = nil) ⇒ Object
Declare which Treequel::Model subclasses the mixin will register itself with. If this is used, it should be declared before declaring the mixin’s bases and/or objectClasses.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/treequel/model/objectclass.rb', line 50 def model_class( mclass=nil ) if mclass # If there were already registered objectclasses, remove them from the previous # model class unless @model_objectclasses.empty? && @model_bases.empty? Treequel.log.warn "%p: model_class should come before model_objectclasses" % [ self ] @model_class.unregister_mixin( self ) mclass.register_mixin( self ) end @model_class = mclass end return @model_class end |
#model_objectclasses(*objectclasses) ⇒ Object
Set or get objectClasses that the mixin requires. Also registers the mixin with Treequel::Model. If objectclasses are given, they are set as the objectClasses the mixin will apply to, as an array of Symbols (or objects that respond to #to_sym).
70 71 72 73 74 75 76 |
# File 'lib/treequel/model/objectclass.rb', line 70 def model_objectclasses( *objectclasses ) unless objectclasses.empty? @model_objectclasses = objectclasses.map( &:to_sym ) @model_class.register_mixin( self ) end return @model_objectclasses.dup end |
#search(directory = nil) ⇒ Object
Return a Branchset (or BranchCollection if the receiver has more than one base) that can be used to search the given directory for entries to which the receiver applies.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/treequel/model/objectclass.rb', line 133 def search( directory=nil ) directory ||= self.model_class.directory bases = self.model_bases objectclasses = self.model_objectclasses raise Treequel::ModelError, "%p has no search criteria defined" % [ self ] if bases.empty? && objectclasses.empty? Treequel.log.debug "Creating search for %p using model class %p" % [ self, self.model_class ] # Start by making a Branchset or BranchCollection for the mixin's bases. If # the mixin doesn't have any bases, just use the base DN of the directory # to be searched bases = [directory.base_dn] if bases.empty? search = bases. map {|base| self.model_class.new(directory, base).branchset }. inject {|branch1,branch2| branch1 + branch2 } Treequel.log.debug "Search branch after applying bases is: %p" % [ search ] return self.model_objectclasses.inject( search ) do |branchset, oid| Treequel.log.debug " adding filter for objectClass=%s to %p" % [ oid, branchset ] branchset.filter( :objectClass => oid ) end end |