| Module | ActiveLdap::Attributes::Normalize |
| In: |
lib/active_ldap/attributes.rb
|
Enforce typing: Hashes are for subtypes Arrays are for multiple entries
# File lib/active_ldap/attributes.rb, line 32
32: def normalize_attribute(name, value)
33: if name.nil?
34: raise RuntimeError, _('The first argument, name, must not be nil. ' \
35: 'Please report this as a bug!')
36: end
37:
38: name = normalize_attribute_name(name)
39: rubyish_class_name = Inflector.underscore(value.class.name)
40: handler = "normalize_attribute_value_of_#{rubyish_class_name}"
41: if respond_to?(handler, true)
42: [name, send(handler, name, value)]
43: else
44: [name, [schema.attribute(name).normalize_value(value)]]
45: end
46: end
# File lib/active_ldap/attributes.rb, line 25
25: def normalize_attribute_name(name)
26: name.to_s.downcase
27: end
# File lib/active_ldap/attributes.rb, line 56
56: def unnormalize_attribute(name, values, result={})
57: if values.empty?
58: result[name] = []
59: else
60: values.each do |value|
61: if value.is_a?(Hash)
62: suffix, real_value = extract_attribute_options(value)
63: new_name = name + suffix
64: result[new_name] ||= []
65: result[new_name].concat(real_value)
66: else
67: result[name] ||= []
68: result[name] << value.dup
69: end
70: end
71: end
72: result
73: end
# File lib/active_ldap/attributes.rb, line 48
48: def unnormalize_attributes(attributes)
49: result = {}
50: attributes.each do |name, values|
51: unnormalize_attribute(name, values, result)
52: end
53: result
54: end
Extracts all of the subtypes from a given set of nested hashes and returns the attribute suffix and the final true value
# File lib/active_ldap/attributes.rb, line 172
172: def extract_attribute_options(value)
173: options = ''
174: ret_val = value
175: if value.class == Hash
176: options = ';' + value.keys[0]
177: ret_val = value[value.keys[0]]
178: if ret_val.class == Hash
179: sub_options, ret_val = extract_attribute_options(ret_val)
180: options += sub_options
181: end
182: end
183: ret_val = [ret_val] unless ret_val.class == Array
184: [options, ret_val]
185: end
Makes the Hashized value from the full attribute name e.g. userCertificate;binary => "some_bin"
becomes userCertificate => {"binary" => "some_bin"}
# File lib/active_ldap/attributes.rb, line 160
160: def normalize_attribute_options(attr, value)
161: return [attr, value] unless attr.match(/;/)
162:
163: ret_attr, *options = attr.split(/;/)
164: [ret_attr,
165: [options.reverse.inject(value) {|result, option| {option => result}}]]
166: end
# File lib/active_ldap/attributes.rb, line 76
76: def normalize_attribute_value_of_array(name, value)
77: attribute = schema.attribute(name)
78: if value.size > 1 and attribute.single_value?
79: format = _("Attribute %s can only have a single value")
80: message = format % self.class.human_attribute_name(attribute)
81: raise TypeError, message
82: end
83: if value.empty?
84: if schema.attribute(name).binary_required?
85: [{'binary' => value}]
86: else
87: value
88: end
89: else
90: value.collect do |entry|
91: normalize_attribute(name, entry)[1][0]
92: end
93: end
94: end
# File lib/active_ldap/attributes.rb, line 134
134: def normalize_attribute_value_of_date(name, value)
135: new_value = sprintf('%.04d%.02d%.02d%.02d%.02d%.02d%s',
136: value.year, value.month, value.mday, 0, 0, 0,
137: '+0000')
138: normalize_attribute_value_of_string(name, new_value)
139: end
# File lib/active_ldap/attributes.rb, line 148
148: def normalize_attribute_value_of_date_time(name, value)
149: new_value = sprintf('%.04d%.02d%.02d%.02d%.02d%.02d%s',
150: value.year, value.month, value.mday, value.hour,
151: value.min, value.sec, value.zone)
152: normalize_attribute_value_of_string(name, new_value)
153: end
# File lib/active_ldap/attributes.rb, line 96
96: def normalize_attribute_value_of_hash(name, value)
97: if value.keys.size > 1
98: format = _("Hashes must have one key-value pair only: %s")
99: raise TypeError, format % value.inspect
100: end
101: unless value.keys[0].match(/^(lang-[a-z][a-z]*)|(binary)$/)
102: logger.warn do
103: format = _("unknown option did not match lang-* or binary: %s")
104: format % value.keys[0]
105: end
106: end
107: # Contents MUST be a String or an Array
108: if !value.has_key?('binary') and schema.attribute(name).binary_required?
109: suffix, real_value = extract_attribute_options(value)
110: name, values =
111: normalize_attribute_options("#{name}#{suffix};binary", real_value)
112: values
113: else
114: [value]
115: end
116: end
# File lib/active_ldap/attributes.rb, line 118
118: def normalize_attribute_value_of_nil_class(name, value)
119: if schema.attribute(name).binary_required?
120: [{'binary' => []}]
121: else
122: []
123: end
124: end
# File lib/active_ldap/attributes.rb, line 126
126: def normalize_attribute_value_of_string(name, value)
127: if schema.attribute(name).binary_required?
128: [{'binary' => [value]}]
129: else
130: [value]
131: end
132: end