class ROM::LDAP::Directory::Entry
A Hash-like object wrapping the DN and attributes returned by the server. Contains the canonical attributes hash and a formatted version. BER format converted to primitive String ensures clean output in to_yaml. Accessed when iterating over dataset during modify and delete. Exposes methods fetch
, first
, each_value
and include?
. All other method calls are forwarded to the formatted tuple.
@see Directory#query
@api public
Public Instance Methods
Iterate over the values of a given attribute.
@param key [Symbol] canonical attribute name
@example
entry.each_value(:object_class, &:to_sym) entry.each_value(:object_class) { |o| o.to_sym }
# File lib/rom/ldap/directory/entry.rb, line 83 def each_value(key, &block) fetch(key).map(&block) end
Retrieve values for a given attribute.
@see Directory::Root
@return [Array<String>]
@param key [String, Symbol]
# File lib/rom/ldap/directory/entry.rb, line 57 def fetch(key) formatted.fetch(rename(key), canonical[key]) end
Find the first (only) value for an attribute.
@see Directory::Root
@param [Symbol, String] key Attribute
name.
@return [String]
# File lib/rom/ldap/directory/entry.rb, line 70 def first(key) fetch(key)&.first end
Mostly used by the test suite.
@example
expect(relation.first).to include(attr: %w[val1 val2])
@param tuple [Hash] keys and array of values
@return [Boolean]
# File lib/rom/ldap/directory/entry.rb, line 97 def include?(tuple) tuple.flat_map { |attr, vals| vals.map { |v| fetch(attr).include?(v) } }.all? rescue NoMethodError false end
@return [String]
# File lib/rom/ldap/directory/entry.rb, line 121 def inspect %(#<#{self.class} #{dn.empty? ? 'rootDSE' : dn} />) end
Defer to enumerable hash methods before entry values.
# File lib/rom/ldap/directory/entry.rb, line 115 def method_missing(meth, *args, &block) formatted.send(meth, *args, &block) if formatted.respond_to?(meth) || super end
Compatibility method with Ruby < 2.5
@return [Hash]
# File lib/rom/ldap/directory/entry.rb, line 109 def slice(*keys) formatted.select { |k, _v| keys.include?(k) } end
Private Instance Methods
Create canonical tuple
@example
# => { 'dn' => [''], 'objectClass' => ['', ''] }
@return [Hash] canonical camelCase keys ordered alphabetically
@see Dataset#export
@api private
# File lib/rom/ldap/directory/entry.rb, line 172 def canonical stringify_keys[stringify_values[with_dn]] end
Convert keys of the canonical tuple using the chosen formatting proc.
@api private
# File lib/rom/ldap/directory/entry.rb, line 148 def formatted Functions[:map_keys, LDAP.formatter][canonical] end
Cache renamed key to improve performance two fold in benchmarks.
@param key [String, Symbol]
@api private
# File lib/rom/ldap/directory/entry.rb, line 141 def rename(key) fetch_or_store(key) { LDAP.formatter[key] } end
@param meth [Symbol]
@return [Boolean]
@api private
# File lib/rom/ldap/directory/entry.rb, line 132 def respond_to_missing?(meth, include_private = false) formatted.respond_to?(meth) || super end
Covert hash keys to strings.
@return [Proc]
@api private
# File lib/rom/ldap/directory/entry.rb, line 181 def stringify_keys Functions[:map_keys, Functions[:to_string]] end
Convert hash whose values are arrays of strings.
@return [Proc]
@api private
# File lib/rom/ldap/directory/entry.rb, line 190 def stringify_values Functions[:map_values, Functions[:map_array, Functions[:to_string]]] end
DN combined with attributes array
@return [Array]
@api private
# File lib/rom/ldap/directory/entry.rb, line 157 def with_dn attributes.dup.sort.unshift(['dn', dn]) end