# class ActiveLdap::LdifInvalid

### Constants

NEAREST_MARK

### Attributes

column[R]
ldif[R]
line[R]
nearest[R]
reason[R]

### Public Class Methods

new(ldif, reason=nil, line=nil, column=nil) click to toggle source
Calls superclass method

    # File lib/active_ldap/base.rb, line 154
    def initialize(ldif, reason=nil, line=nil, column=nil)
      @ldif = ldif
      @reason = reason
      @line = line
      @column = column
      @nearest = nil
      if @reason
        message = _("invalid LDIF: %s:") % @reason
      else
        message = _("invalid LDIF:")
      end
      if @line and @column
        @nearest = detect_nearest(@line, @column)
        snippet = generate_snippet
        message << "\n#{snippet}\n"
      end
      super("#{message}\n#{numbered_ldif}")
    end

### Private Instance Methods

detect_nearest(line, column) click to toggle source

    # File lib/active_ldap/base.rb, line 175
    def detect_nearest(line, column)
      lines = Compatible.string_to_lines(@ldif).to_a
      nearest = lines[line - 1] || ""
      if column - 1 == nearest.size # for JRuby 1.0.2 :<
        nearest << NEAREST_MARK
      else
        nearest[column - 1, 0] = NEAREST_MARK
      end
      if nearest == NEAREST_MARK
        nearest = "#{lines[line - 2]}#{nearest}"
      end
      nearest
    end

generate_snippet() click to toggle source

    # File lib/active_ldap/base.rb, line 189
    def generate_snippet
      nearest = @nearest.chomp
      column_column = ":#{@column}"
      target_position_info = "#{@line}#{column_column}: "
      if /\n/ =~ nearest
        snippet = "%#{Math.log10(@line).truncate}d" % (@line - 1)
        snippet << " " * column_column.size
        snippet << ": "
        snippet << nearest.gsub(/\n/, "\n#{target_position_info}")
      else
        snippet = "#{target_position_info}#{nearest}"
      end
      snippet
    end

numbered_ldif() click to toggle source

    # File lib/active_ldap/base.rb, line 204
    def numbered_ldif
      return @ldif if @ldif.blank?
      lines = Compatible.string_to_lines(@ldif)
      format = "%#{Math.log10(lines.size).truncate + 1}d: %s"
      i = 0
      lines.collect do |line|
        i += 1
        format % [i, line]
      end.join
    end