module Treequel::Model::ObjectClass

Mixin that provides Treequel::Model characteristics to a mixin module.

Public Class Methods

extended( mod ) click to toggle source

Extension callback -- add data structures to the extending mod.

# File lib/treequel/model/objectclass.rb, line 17
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 ) click to toggle source

Inclusion callback -- Methods should be applied to the module rather than an instance. Warn the user if they use include() and extend() instead.

# File lib/treequel/model/objectclass.rb, line 27
def self::included( mod )
        warn "extending %p rather than appending features to it" % [ mod ]
        mod.extend( self )
end

Public Instance Methods

create( dn, entryhash={} ) click to toggle source
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.

# File lib/treequel/model/objectclass.rb, line 99
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 ) click to toggle source

Set or get base DNs that the mixin applies to.

# File lib/treequel/model/objectclass.rb, line 77
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 ) click to toggle source

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.

# File lib/treequel/model/objectclass.rb, line 47
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 ) click to toggle source

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).

# File lib/treequel/model/objectclass.rb, line 67
def model_objectclasses( *objectclasses )
        unless objectclasses.empty?
                @model_objectclasses = objectclasses.map( &:to_sym )
                @model_class.register_mixin( self )
        end
        return @model_objectclasses.dup
end