Module: ActiveLdap::Ldif::Attribute

Defined in:
lib/active_ldap/ldif.rb

Constant Summary

SIZE =
75

Class Method Summary collapse

Class Method Details

.binary_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/active_ldap/ldif.rb', line 34

def binary_value?(value)
  if value.respond_to?(:encoding)
    return true if value.encoding == Encoding.find("ascii-8bit")
  end
  if /\A#{Parser::SAFE_STRING}\z/ =~ value
    false
  else
    true
  end
end

.encode(name, value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_ldap/ldif.rb', line 45

def encode(name, value)
  return "#{name}:\n" if value.blank?
  result = "#{name}:"

  value = value.to_s unless value.is_a?(String)
  if value[-1, 1] == ' ' or binary_value?(value)
    result << ":"
    value = [value].pack("m").gsub(/\n/, '')
  end
  result << " "

  first_line_value_size = SIZE - result.size
  if value.size > first_line_value_size
    first_line_value = value[0, first_line_value_size]
    rest_value = value[first_line_value_size..-1]
  else
    first_line_value = value
    rest_value = nil
  end

  result << "#{first_line_value}\n"
  return result if rest_value.nil?

  rest_value.scan(/.{1,#{SIZE - 1}}/).each do |line| # FIXME
    result << " #{line}\n"
  end
  result
end

.normalize_value(value, result = []) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_ldap/ldif.rb', line 74

def normalize_value(value, result=[])
  case value
  when Array
    value.each {|val| normalize_value(val, result)}
  when Hash
    value.each do |option, val|
      normalize_value(val).each do |options, v|
        result << [[option] + options, v]
      end
    end
    result
  else
    result << [[], value]
  end
  result
end