Module: ActiveLdap::Attributes::Normalizable

Included in:
EntryAttribute
Defined in:
lib/active_ldap/attributes.rb

Instance Method Summary collapse

Instance Method Details

#normalize_attribute(name, value) ⇒ Object

Enforce typing: Hashes are for subtypes Arrays are for multiple entries



67
68
69
70
71
72
73
74
75
# File 'lib/active_ldap/attributes.rb', line 67

def normalize_attribute(name, value)
  if name.nil?
    raise RuntimeError, _('The first argument, name, must not be nil. ' \
                          'Please report this as a bug!')
  end

  name = normalize_attribute_name(name)
  [name, schema.attribute(name).normalize_value(value)]
end

#normalize_attribute_name(name) ⇒ Object



60
61
62
# File 'lib/active_ldap/attributes.rb', line 60

def normalize_attribute_name(name)
  name.to_s.downcase
end

#normalize_attribute_options(attr, value) ⇒ Object

normalize_attribute_options

Makes the Hashized value from the full attribute name e.g. userCertificate;binary => “some_bin”

becomes userCertificate => {"binary" => "some_bin"}


112
113
114
115
116
117
118
# File 'lib/active_ldap/attributes.rb', line 112

def normalize_attribute_options(attr, value)
  return [attr, value] unless attr.match(/;/)

  ret_attr, *options = attr.split(/;/)
  [ret_attr,
   [options.reverse.inject(value) {|result, option| {option => result}}]]
end

#unnormalize_attribute(name, values, result = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_ldap/attributes.rb', line 85

def unnormalize_attribute(name, values, result={})
  if values.empty?
    result[name] = []
  else
    values.each do |value|
      if value.is_a?(Hash)
        suffix, real_value = unnormalize_attribute_options(value)
        new_name = name + suffix
        unnormalize_attribute(new_name, real_value, result)
      else
        result[name] ||= []
        if value.is_a?(DN)
          result[name] << value.to_s
        else
          result[name] << value.dup
        end
      end
    end
  end
  result
end

#unnormalize_attribute_options(value) ⇒ Object

unnormalize_attribute_options

Unnormalizes all of the subtypes from a given set of nested hashes and returns the attribute suffix and the final true value



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/active_ldap/attributes.rb', line 124

def unnormalize_attribute_options(value)
  options = ''
  ret_val = value
  if value.class == Hash
    options = ';' + value.keys[0]
    ret_val = value[value.keys[0]]
    if ret_val.class == Hash
      sub_options, ret_val = unnormalize_attribute_options(ret_val)
      options += sub_options
    end
  end
  ret_val = [ret_val] unless ret_val.class == Array
  [options, ret_val]
end

#unnormalize_attributes(attributes) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/active_ldap/attributes.rb', line 77

def unnormalize_attributes(attributes)
  result = {}
  attributes.each do |name, values|
    unnormalize_attribute(name, values, result)
  end
  result
end