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

[](key)
Alias for: fetch
each_value(key, &block) click to toggle source

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
fetch(key) click to toggle source

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
Also aliased as: []
first(key) click to toggle source

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
include?(tuple) click to toggle source

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
inspect() click to toggle source

@return [String]

# File lib/rom/ldap/directory/entry.rb, line 121
def inspect
  %(#<#{self.class} #{dn.empty? ? 'rootDSE' : dn} />)
end
method_missing(meth, *args, &block) click to toggle source

Defer to enumerable hash methods before entry values.

Calls superclass method
# 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
slice(*keys) click to toggle source

Compatibility method with Ruby < 2.5

@see Relation::Reading#pluck

@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

canonical() click to toggle source

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
formatted() click to toggle source

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
rename(key) click to toggle source

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
respond_to_missing?(meth, include_private = false) click to toggle source

@param meth [Symbol]

@return [Boolean]

@api private

Calls superclass method
# File lib/rom/ldap/directory/entry.rb, line 132
def respond_to_missing?(meth, include_private = false)
  formatted.respond_to?(meth) || super
end
stringify_keys() click to toggle source

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
stringify_values() click to toggle source

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
with_dn() click to toggle source

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