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
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
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
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
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
Return the Table
as a hash.
# File lib/treequel/schema/table.rb, line 84 def to_h @hash.dup end
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
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
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
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
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