Table

class
Superclass
Object
Included Modules
Enumerable
Treequel::Normalization
Treequel::Constants::Patterns
Extended With
Forwardable
Loggability

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

anchor
def_normalized_delegators( delegate, *syms )

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

# File lib/treequel/schema/table.rb, line 33
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
anchor
new( initial_values={} )

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

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

Public Instance Methods

anchor
merge( other_table ) { |key, original_value, new_value| ... }

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 102
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
anchor
merge!( other_table, &merge_callback )

Merge other_table into the receiver.

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

Return the Table as a hash.

# File lib/treequel/schema/table.rb, line 84
def to_h
        @hash.dup
end
Also aliased as: to_hash
anchor
to_hash()
Alias for: to_h
anchor
to_s()

Return the Table in LDIF format

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

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

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

Protected Instance Methods

anchor
make_getter( key )

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

# File lib/treequel/schema/table.rb, line 149
def make_getter( key )
        return Proc.new { self[key] }
end
anchor
make_setter( key )

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

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

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

# File lib/treequel/schema/table.rb, line 123
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