class ROM::LDAP::LDIF::Importer

LDIF to importable tuples.

@param ldif [String]

@api private

Public Instance Methods

to_tuples() { |tuple| ... } click to toggle source

@example =>

[{
              :dn => "ou=users, dc=rom, dc=ldap",
             "ou" => "users",
    "objectClass" => "organizationalUnit"
}]

@return [Array<Hash>]

# File lib/rom/ldap/ldif/importer.rb, line 29
def to_tuples
  dataset.map do |entry|
    next if entry.any? { |l| l.match?(/^version/) }

    abort 'update statements not allowed' if entry.any? { |l| l.match?(/^changetype/) }

    tuple = parse(entry)

    block_given? ? yield(tuple) : tuple
  end.compact
end

Private Instance Methods

comment?(line) click to toggle source
# File lib/rom/ldap/ldif/importer.rb, line 52
def comment?(line)
  line.match?(/^#/)
end
dataset() click to toggle source
# File lib/rom/ldap/ldif/importer.rb, line 43
def dataset
  ldif
    .split(NEW_LINE)
    .reject(&method(:comment?))
    .chunk(&method(:divider?))
    .reject(&:first)
    .map(&:pop)
end
divider?(line) click to toggle source
# File lib/rom/ldap/ldif/importer.rb, line 56
def divider?(line)
  line.eql?(EMPTY_STRING)
end
key_pair(line) click to toggle source

@return [Hash]

# File lib/rom/ldap/ldif/importer.rb, line 71
def key_pair(line)
  _, key, _, value = line.match(LDIF_LINE_REGEX).to_a

  key = key.to_sym if key.eql?('dn')
  value = File.binread(Regexp.last_match(1)) if value.match(BIN_FILE_REGEX)
  value = Functions[:identify_value].call(value)

  { key => value }
end
merge(original, additional) click to toggle source
# File lib/rom/ldap/ldif/importer.rb, line 81
def merge(original, additional)
  key, value = additional.to_a.first

  if original.key?(key)
    original[key] = [*original[key], value]
    original
  else
    original.merge(additional)
  end
end
parse(entry) click to toggle source

@param entry [Array<String>]

@return [Hash]

# File lib/rom/ldap/ldif/importer.rb, line 64
def parse(entry)
  entry.map(&method(:key_pair)).inject(&method(:merge))
end