Class: Treequel::Schema::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Loggability
Includes:
Enumerable, Constants::Patterns, Normalization
Defined in:
lib/treequel/schema/table.rb

Overview

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

Constant Summary collapse

KEYED_METHODS =

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

[ :"[]", :"[]=", :delete, :fetch, :key?, :has_key?, :include?,
:member?, :store ]

Constants included from Constants::Patterns

Constants::Patterns::ALPHA, Constants::Patterns::AMPERSAND, Constants::Patterns::ASSERTIONVALUE, Constants::Patterns::ASTERISK, Constants::Patterns::ATTRIBUTE_TYPE, Constants::Patterns::ATTRIBUTE_TYPE_AND_VALUE, Constants::Patterns::ATTRIBUTE_VALUE, Constants::Patterns::BASE64_CHAR, Constants::Patterns::BASE64_STRING, Constants::Patterns::COLON, Constants::Patterns::COMMA, Constants::Patterns::DESCR, Constants::Patterns::DIGIT, Constants::Patterns::DISTINGUISHED_NAME, Constants::Patterns::DN_ESCAPED, Constants::Patterns::DOLLAR, Constants::Patterns::DOT, Constants::Patterns::DQUOTE, Constants::Patterns::DSTRING, Constants::Patterns::EQUALS, Constants::Patterns::ESC, Constants::Patterns::ESCAPED, Constants::Patterns::EXCLAMATION, Constants::Patterns::EXTENSIONS, Constants::Patterns::FILL, Constants::Patterns::FOLD, Constants::Patterns::HEX, Constants::Patterns::HEXPAIR, Constants::Patterns::HEXSTRING, Constants::Patterns::HYPHEN, Constants::Patterns::KEYCHAR, Constants::Patterns::KEYSTRING, Constants::Patterns::KIND, Constants::Patterns::LANGLE, Constants::Patterns::LCURLY, Constants::Patterns::LDAP_ATTRIBUTE_DESCRIPTION, Constants::Patterns::LDAP_ATTRIBUTE_TYPE_DESCRIPTION, Constants::Patterns::LDAP_MATCHING_RULE_DESCRIPTION, Constants::Patterns::LDAP_MATCHING_RULE_USE_DESCRIPTION, Constants::Patterns::LDAP_MISORDERED_DESC_OBJECTCLASS_DESCRIPTION, Constants::Patterns::LDAP_MISORDERED_KIND_OBJECTCLASS_DESCRIPTION, Constants::Patterns::LDAP_MISORDERED_SYNTAX_ATTRIBUTE_TYPE_DESCRIPTION, Constants::Patterns::LDAP_OBJECTCLASS_DESCRIPTION, Constants::Patterns::LDAP_SUBSTRING_FILTER, Constants::Patterns::LDAP_SUBSTRING_FILTER_VALUE, Constants::Patterns::LDAP_SYNTAX_DESCRIPTION, Constants::Patterns::LDAP_TRAILING_KIND_OBJECTCLASS_DESCRIPTION, Constants::Patterns::LDAP_UNESCAPE_SQUOTE_ATTRIBUTE_TYPE_DESCRIPTION, Constants::Patterns::LDIF_ATTRIBUTE_DESCRIPTION, Constants::Patterns::LDIF_ATTRIBUTE_TYPE, Constants::Patterns::LDIF_ATTRTYPE_OPTION, Constants::Patterns::LDIF_ATTRTYPE_OPTIONS, Constants::Patterns::LDIF_ATTRVAL_SPEC, Constants::Patterns::LDIF_ATTR_TYPE_CHARS, Constants::Patterns::LDIF_OPT_CHAR, Constants::Patterns::LDIF_SAFE_CHAR, Constants::Patterns::LDIF_SAFE_INIT_CHAR, Constants::Patterns::LDIF_SAFE_STRING, Constants::Patterns::LDIF_VALUE_SPEC, Constants::Patterns::LDIGIT, Constants::Patterns::LEADCHAR, Constants::Patterns::LEADKEYCHAR, Constants::Patterns::LEN, Constants::Patterns::LPAREN, Constants::Patterns::LUTF1, Constants::Patterns::MALFORMED_DSTRING, Constants::Patterns::MALFORMED_QDSTRING, Constants::Patterns::NOIDLEN, Constants::Patterns::NORMAL, Constants::Patterns::NUL, Constants::Patterns::NUMBER, Constants::Patterns::NUMERICOID, Constants::Patterns::OCTET, Constants::Patterns::OID, Constants::Patterns::OIDLIST, Constants::Patterns::OIDS, Constants::Patterns::PAIR, Constants::Patterns::PLUS, Constants::Patterns::QDESCR, Constants::Patterns::QDESCRLIST, Constants::Patterns::QDESCRS, Constants::Patterns::QDSTRING, Constants::Patterns::QDSTRINGLIST, Constants::Patterns::QDSTRINGS, Constants::Patterns::QQ, Constants::Patterns::QS, Constants::Patterns::QUOTED_DESCR, Constants::Patterns::QUOTED_NUMERICOID, Constants::Patterns::QUTF1, Constants::Patterns::QUTF8, Constants::Patterns::RANGLE, Constants::Patterns::RCURLY, Constants::Patterns::RELATIVE_DISTINGUISHED_NAME, Constants::Patterns::RPAREN, Constants::Patterns::SEMI, Constants::Patterns::SEP, Constants::Patterns::SHARP, Constants::Patterns::SP, Constants::Patterns::SPACE, Constants::Patterns::SPECIAL, Constants::Patterns::SQUOTE, Constants::Patterns::STRING, Constants::Patterns::STRINGCHAR, Constants::Patterns::SUTF1, Constants::Patterns::TILDE, Constants::Patterns::TRAILCHAR, Constants::Patterns::TUTF1, Constants::Patterns::UNESCAPED, Constants::Patterns::URI_REF, Constants::Patterns::USAGE, Constants::Patterns::USCORE, Constants::Patterns::UTF0, Constants::Patterns::UTF1, Constants::Patterns::UTF1SUBSET, Constants::Patterns::UTF2, Constants::Patterns::UTF3, Constants::Patterns::UTF4, Constants::Patterns::UTF8, Constants::Patterns::UTFMB, Constants::Patterns::VALUEENCODING, Constants::Patterns::VERTBAR, Constants::Patterns::WSP, Constants::Patterns::XSTRING

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalization

normalize_hash, normalize_key

Constructor Details

#initialize(initial_values = {}) ⇒ Table

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



47
48
49
50
# File 'lib/treequel/schema/table.rb', line 47

def initialize( initial_values={} )
  @hash = {}
  initial_values.each {|k,v| self.append(k => v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (protected)

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/treequel/schema/table.rb', line 124

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

Class Method Details

.def_normalized_delegators(delegate, *syms) ⇒ Object

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



34
35
36
37
38
39
40
41
42
# File 'lib/treequel/schema/table.rb', line 34

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

Instance Method Details

#initialize_copy(orig_table) ⇒ Object

Make sure @hash is unique on Table duplications.



54
55
56
# File 'lib/treequel/schema/table.rb', line 54

def initialize_copy( orig_table ) # :nodoc:
  @hash = orig_table.to_hash
end

#merge(other_table, &merge_callback) ⇒ Object Also known as: update

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.



103
104
105
106
107
# File 'lib/treequel/schema/table.rb', line 103

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, &merge_callback) ⇒ Object Also known as: update!

Merge other_table into the receiver.



92
93
94
95
# File 'lib/treequel/schema/table.rb', line 92

def merge!( other_table, &merge_callback )
  nhash = normalize_hash( other_table.to_hash )
  @hash.merge!( nhash, &merge_callback )
end

#to_hObject Also known as: to_hash

Return the Table as a hash.



85
86
87
# File 'lib/treequel/schema/table.rb', line 85

def to_h
  @hash.dup
end

#to_sObject

Return the Table in LDIF format



77
78
79
80
81
# File 'lib/treequel/schema/table.rb', line 77

def to_s
  @hash.collect do |oid,value|
    "%s: %s" % [ oid, value ]
  end.flatten.sort.join( "\r\n" ) + "\r\n"
end

#values_at(*keys) ⇒ Object

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



113
114
115
# File 'lib/treequel/schema/table.rb', line 113

def values_at( *keys )
  @hash.values_at( *(keys.collect {|k| normalize_key(k)}) )
end