class ROM::LDAP::Dataset

@api private

Public Class Methods

dsl() click to toggle source

Collection of Dataset::DSL module methods.

Used by Relation to forward methods to Dataset.

@return [Array<Symbol>]

@example

# => %i{equal has lt begins excludes present}
# File lib/rom/ldap/dataset.rb, line 84
def self.dsl
  DSL.public_instance_methods(false)
end

Public Instance Methods

bind(password) click to toggle source

Validate the password against the filtered user.

@param password [String]

@return [Boolean]

@api public

# File lib/rom/ldap/dataset.rb, line 202
def bind(password)
  directory.bind_as(filter: to_ast, password: password)
end
each(*args, &block) click to toggle source

Iterate over the entries return from the server.

@return [Array <Directory::Entry>] @return [Enumerator <Directory::Entry>]

@api public

# File lib/rom/ldap/dataset.rb, line 115
def each(*args, &block)
  results = paginated? ? entries[page_range] : entries
  results = results.sort_by { rand } if random
  results = results.reverse_each if reversed?
  results = results.map { |e| apply_aliases(e) } if aliases.any?

  block_given? ? results.each(*args, &block) : results.to_enum
end
export() click to toggle source

Handle different string output formats

i.e. DSML, LDIF, JSON, YAML, MessagePack.

@return [Hash, Array<Hash>]

# File lib/rom/ldap/dataset.rb, line 211
def export
  results = map(&:canonical)
  results.one? ? results.first : results
end
grep(attrs, value) click to toggle source

Wildcard search on multiple attributes.

@example

dataset.grep([:givenname, :sn], 'foo').opts[:criteria] =>
  [:con_or, [[:op_eql, :givenname, "*foo*"], [:op_eql, :sn, "*foo*"]]]

@see Relation::Reading#grep

@param attrs [Array<Symbol>] schema attribute names

@param value [String] search parameter

@return [Relation]

@api public

# File lib/rom/ldap/dataset.rb, line 167
def grep(attrs, value)
  new_criteria = attrs.map do |attr|
    match_dsl([[attr, value]], left: WILDCARD, right: WILDCARD)
  end
  join(new_criteria, :con_or)
end
inspect() click to toggle source

Inspect dataset revealing current ast and base.

@return [String]

@api public

# File lib/rom/ldap/dataset.rb, line 138
def inspect
  %(#<#{self.class}: base="#{base}" #{to_ast} />)
end
join(new_criteria, constructor = :con_and) click to toggle source

@see Relation#where

Combine AST criteria - use AND by default

@param new_criteria [Array] @param constructor [Symbol]

@return [Relation]

@api public

# File lib/rom/ldap/dataset.rb, line 184
def join(new_criteria, constructor = :con_and)
  new_chain = join_dsl(constructor, new_criteria)

  # check because RestrictionDSL sometimes offers empty criteria
  if new_chain.empty?
    self
  else
    chain(*new_chain)
  end
end
map(attr = nil, &block) click to toggle source

Iterate over each entry or one attribute of each entry.

@return [Mixed]

@api public

# File lib/rom/ldap/dataset.rb, line 129
def map(attr = nil, &block)
  each.map { |entry| attr ? entry[attr] : entry }.map(&block)
end
opts() click to toggle source

@return [Hash] internal options

@api public

# File lib/rom/ldap/dataset.rb, line 105
def opts
  options.merge(ast: to_ast, filter: to_filter).freeze
end
total() click to toggle source

Unrestricted count of every entry under the search base

with the domain entry discounted.

@return [Integer]

@api public

# File lib/rom/ldap/dataset.rb, line 222
def total
  directory.base_total - 1
end
unfiltered() click to toggle source

This method is present in Sequel and ensures rom-sql/rom-ldap plugin interoperability.

@return [Dataset]

@api public

# File lib/rom/ldap/dataset.rb, line 147
def unfiltered
  with(criteria: EMPTY_ARRAY)
end
with(overrides) click to toggle source

Initialise a new class overriding options.

@return [ROM::LDAP::Dataset]

@param overrides [Hash] Alternative options

@api public

# File lib/rom/ldap/dataset.rb, line 98
def with(overrides)
  self.class.new(**options.merge(overrides))
end

Private Instance Methods

alias_map() click to toggle source
# File lib/rom/ldap/dataset.rb, line 280
def alias_map
  { dn: :dn }.merge(attrs.zip(aliases).to_h)
end
apply_aliases(entry) click to toggle source
# File lib/rom/ldap/dataset.rb, line 276
def apply_aliases(entry)
  Functions[:rename_keys][alias_map, entry].invert
end
entries() click to toggle source

Communicate with LDAP servers.

@see Connection::SearchRequest for #query keywords defintion.

@return [Array<Hash>] Populate with a directory search.

@api private

# File lib/rom/ldap/dataset.rb, line 234
def entries
  results =
    directory.query(
      filter: to_ast,
      base: base,
      attributes: renamed_select,
      sorted: renamed_sort,
      max: limit,
      reverse: reversed?
    )

  options[:criteria] = []
  results
end
page_range() click to toggle source

@return [Range]

@api private

# File lib/rom/ldap/dataset.rb, line 262
def page_range
  offset..(offset + limit - 1)
end
paginated?() click to toggle source

@api private

# File lib/rom/ldap/dataset.rb, line 267
def paginated?
  limit && offset
end
renamed_select() click to toggle source

@api private

# File lib/rom/ldap/dataset.rb, line 250
def renamed_select
  directory.canonical_attributes(attrs) if attrs
end
renamed_sort() click to toggle source

@api private

# File lib/rom/ldap/dataset.rb, line 255
def renamed_sort
  directory.canonical_attributes(sort_attrs) if sort_attrs
end
reversed?() click to toggle source

@api private

# File lib/rom/ldap/dataset.rb, line 272
def reversed?
  direction.eql?(:desc)
end