Class: Ldaptic::RDN

Inherits:
Hash
  • Object
show all
Defined in:
lib/ldaptic/dn.rb

Constant Summary collapse

MANDATORY_ATTRIBUTE_TYPES =
%w(CN L ST O OU C STREET DC UID)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter::Conversions

#to_ldap_filter

Constructor Details

#initialize(rdn = {}) ⇒ RDN

Returns a new instance of RDN.



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ldaptic/dn.rb', line 220

def initialize(rdn = {})
  rdn = rdn.rdn if rdn.respond_to?(:rdn)
  if rdn.kind_of?(String)
    rdn = RDN.parse_string(rdn)
  end
  if rdn.kind_of?(Hash)
    super()
    update(rdn)
  else
    raise TypeError, "default value #{rdn.inspect} not allowed", caller
  end
end

Class Method Details

.parse_string(string) ⇒ Object

:nodoc:



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ldaptic/dn.rb', line 208

def self.parse_string(string) #:nodoc:

  Ldaptic.split(string, ?+).inject({}) do |hash, pair|
    k, v = Ldaptic.split(pair, ?=).map {|x| Ldaptic.unescape(x)}
    hash[k.downcase.to_sym] = v
    hash
  end

rescue
  raise RuntimeError, "error parsing RDN", caller
end

Instance Method Details

#/(*args) ⇒ Object



233
234
235
# File 'lib/ldaptic/dn.rb', line 233

def /(*args)
  Ldaptic::DN([self]).send(:/, *args)
end

#[](*args) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# File 'lib/ldaptic/dn.rb', line 270

def [](*args)
  if args.size == 1
    if args.first.respond_to?(:to_sym)
      return super(convert_key(args.first))
    elsif args.first.kind_of?(Hash)
      return self/args.first
    end
  end
  to_str[*args]
end

#[]=(key, value) ⇒ Object



313
314
315
# File 'lib/ldaptic/dn.rb', line 313

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#cloneObject



297
298
299
300
301
# File 'lib/ldaptic/dn.rb', line 297

def clone
  inject(RDN.new) do |h, (k, v)|
    h[k] = v.dup; h
  end
end

#delete(key) ⇒ Object



348
349
350
# File 'lib/ldaptic/dn.rb', line 348

def delete(key)
  super(convert_key(key))
end

#downcaseObject



259
# File 'lib/ldaptic/dn.rb', line 259

def downcase() clone.downcase! end

#downcase!Object



249
250
251
252
# File 'lib/ldaptic/dn.rb', line 249

def downcase!
  values.each {|v| v.downcase!}
  self
end

#dupObject



340
341
342
# File 'lib/ldaptic/dn.rb', line 340

def dup
  RDN.new(self)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


285
286
287
288
289
290
291
292
293
# File 'lib/ldaptic/dn.rb', line 285

def eql?(other)
  if other.respond_to?(:to_str)
    to_str.casecmp(other.to_str).zero?
  elsif other.kind_of?(Hash)
    eql?(Ldaptic::RDN(other)) rescue false
  else
    super
  end
end

#fetch(key, *extras) ⇒ Object



332
333
334
# File 'lib/ldaptic/dn.rb', line 332

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#hashObject



281
282
283
# File 'lib/ldaptic/dn.rb', line 281

def hash
  to_str.downcase.hash
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


324
325
326
# File 'lib/ldaptic/dn.rb', line 324

def key?(key)
  super(convert_key(key))
end

#merge(hash) ⇒ Object



344
345
346
# File 'lib/ldaptic/dn.rb', line 344

def merge(hash)
  dup.update(hash)
end

#regular_updateObject



311
# File 'lib/ldaptic/dn.rb', line 311

alias_method :regular_update, :update

#regular_writerObject

Based on ActiveSupport’s HashWithIndifferentAccess



310
# File 'lib/ldaptic/dn.rb', line 310

alias_method :regular_writer, '[]='

#to_berObject

Net::LDAP compatibility



304
305
306
# File 'lib/ldaptic/dn.rb', line 304

def to_ber #:nodoc:
  to_str.to_ber
end

#to_rdnObject



237
238
239
# File 'lib/ldaptic/dn.rb', line 237

def to_rdn
  self
end

#to_strObject Also known as: to_s



241
242
243
244
245
# File 'lib/ldaptic/dn.rb', line 241

def to_str
  collect do |k, v|
    "#{k.kind_of?(String) ? k : Ldaptic.encode(k).upcase}=#{Ldaptic.escape(v)}"
  end.sort.join("+")
end

#upcaseObject



260
# File 'lib/ldaptic/dn.rb', line 260

def   upcase() clone.  upcase! end

#upcase!Object



254
255
256
257
# File 'lib/ldaptic/dn.rb', line 254

def upcase!
  values.each {|v| v.upcase!}
  self
end

#update(other_hash) ⇒ Object Also known as: merge!



317
318
319
320
# File 'lib/ldaptic/dn.rb', line 317

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end

#values_at(*indices) ⇒ Object



336
337
338
# File 'lib/ldaptic/dn.rb', line 336

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end