class Treequel::Schema::Table

This is an object that is used to store LDAP schema information in a case-insensitive table.

Constants

KEYED_METHODS

The list of methods that should be delegated through the key-normalization method.

Public Class Methods

def_normalized_delegators( delegate, *syms ) click to toggle source

Auto-generate methods which call the given delegate after normalizing their first argument via normalize_key

# File lib/treequel/schema/table.rb, line 29
def self::def_normalized_delegators( delegate, *syms )
        syms.each do |methodname|
                define_method( methodname ) do |key, *args|
                        nkey = normalize_key( key )
                        instance_variable_get( delegate ).
                                __send__( methodname, nkey, *args )
                end
        end
end
new( initial_values={} ) click to toggle source

Create a new Treequel::Table using the given hash for initial values.

# File lib/treequel/schema/table.rb, line 42
def initialize( initial_values={} )
        @hash = {}
        initial_values.each {|k,v| self.append(k => v) }
end

Public Instance Methods

merge( other_table ) { |key, original_value, new_value| ... } click to toggle source

Return a new table which is the result of merging the receiver with other_table in the same fashion as Hash#merge. If the optional merge_callback block is provided, it is called whenever there is a key collision between the two.

# File lib/treequel/schema/table.rb, line 98
def merge( other_table, &merge_callback ) # :yields: key, original_value, new_value
        other = self.dup
        other.merge!( other_table, &merge_callback )
        return other
end
Also aliased as: update
merge!( other_table, &merge_callback ) click to toggle source

Merge other_table into the receiver.

# File lib/treequel/schema/table.rb, line 87
def merge!( other_table, &merge_callback )
        nhash = normalize_hash( other_table.to_hash )
        @hash.merge!( nhash, &merge_callback )
end
Also aliased as: update!
to_h() click to toggle source

Return the Table as a hash.

# File lib/treequel/schema/table.rb, line 80
def to_h
        @hash.dup
end
Also aliased as: to_hash
to_hash() click to toggle source
Alias for: to_h
to_s() click to toggle source

Return the Table in LDIF format

# File lib/treequel/schema/table.rb, line 72
def to_s
        @hash.collect do |oid,value|
                "%s: %s" % [ oid, value ]
        end.flatten.sort.join( "\r\n" ) + "\r\n"
end
update( other_table ) click to toggle source
Alias for: merge
update!( other_table, &merge_callback ) click to toggle source
Alias for: merge!
values_at( *keys ) click to toggle source

Return an array containing the values associated with the given keys.

# File lib/treequel/schema/table.rb, line 108
def values_at( *keys )
        @hash.values_at( *(keys.collect {|k| normalize_key(k)}) )
end

Protected Instance Methods

make_getter( key ) click to toggle source

Create a Proc that will act as a getter for the given key

# File lib/treequel/schema/table.rb, line 145
def make_getter( key )
        return Proc.new { self[key] }
end
make_setter( key ) click to toggle source

Create a Proc that will act as a setter for the given key

# File lib/treequel/schema/table.rb, line 139
def make_setter( key )
        return Proc.new {|new_value| self[ key ] = new_value }
end
method_missing( sym, *args ) click to toggle source

Proxy method: handle getting/setting headers via methods instead of the index operator.

# File lib/treequel/schema/table.rb, line 119
def method_missing( sym, *args )
        # work magic
        return super unless sym.to_s =~ /^([a-z]\w+)(=)?$/

        # If it's an assignment, the (=)? will have matched
        key, assignment = $1, $2

        method_body = nil
        if assignment
                method_body = self.make_setter( key )
        else
                method_body = self.make_getter( key )
        end

        self.class.send( :define_method, sym, &method_body )
        return self.method( sym ).call( *args )
end