| Class | ActiveLdap::Schema::Attribute |
| In: |
lib/active_ldap/schema.rb
|
| Parent: | Entry |
| super_attribute | [R] |
# File lib/active_ldap/schema.rb, line 390
390: def initialize(name, schema)
391: super(name, schema, "attributeTypes")
392: end
Returns true if the value MUST be transferred in binary
# File lib/active_ldap/schema.rb, line 422
422: def binary_required?
423: @binary_required
424: end
# File lib/active_ldap/schema.rb, line 461
461: def human_attribute_description
462: self.class.human_attribute_description(self)
463: end
# File lib/active_ldap/schema.rb, line 457
457: def human_attribute_name
458: self.class.human_attribute_name(self)
459: end
# File lib/active_ldap/schema.rb, line 449
449: def normalize_value(value)
450: normalize_value_internal(value, false)
451: end
Returns true if an attribute is read-only NO-USER-MODIFICATION
# File lib/active_ldap/schema.rb, line 398
398: def read_only?
399: @read_only
400: end
Returns true if an attribute can only have one value defined SINGLE-VALUE
# File lib/active_ldap/schema.rb, line 407
407: def single_value?
408: @single_value
409: end
# File lib/active_ldap/schema.rb, line 453
453: def syntax_description
454: send_to_syntax(nil, :description)
455: end
# File lib/active_ldap/schema.rb, line 445
445: def type_cast(value)
446: send_to_syntax(value, :type_cast, value)
447: end
# File lib/active_ldap/schema.rb, line 430
430: def valid?(value)
431: validate(value).nil?
432: end
# File lib/active_ldap/schema.rb, line 434
434: def validate(value)
435: error_info = validate_each_value(value)
436: return error_info if error_info
437: begin
438: normalize_value(value)
439: nil
440: rescue AttributeValueInvalid
441: [$!.message]
442: end
443: end
# File lib/active_ldap/schema.rb, line 587
587: def append_binary_key(hash)
588: key, value = hash.to_a[0]
589: if value.is_a?(Hash)
590: append_binary_key(value)
591: else
592: hash.merge(key => {"binary" => value})
593: end
594: end
# File lib/active_ldap/schema.rb, line 466
466: def attribute(attribute_name, name=@name)
467: @schema.attribute_type(name, attribute_name)
468: end
# File lib/active_ldap/schema.rb, line 470
470: def collect_info
471: @description = attribute("DESC")[0]
472: @super_attribute = attribute("SUP")[0]
473: if @super_attribute
474: @super_attribute = @schema.attribute(@super_attribute)
475: @super_attribute = nil if @super_attribute.id.nil?
476: end
477: @read_only = attribute('NO-USER-MODIFICATION')[0] == 'TRUE'
478: @single_value = attribute('SINGLE-VALUE')[0] == 'TRUE'
479: @syntax = attribute("SYNTAX")[0]
480: @syntax = @schema.ldap_syntax(@syntax) if @syntax
481: if @syntax
482: @binary_required = @syntax.binary_transfer_required?
483: @binary = (@binary_required or !@syntax.human_readable?)
484: @derived_syntax = @syntax
485: else
486: @binary_required = false
487: @binary = false
488: @derived_syntax = nil
489: @derived_syntax = @super_attribute.syntax if @super_attribute
490: end
491: end
# File lib/active_ldap/schema.rb, line 580
580: def have_binary_key?(hash)
581: key, value = hash.to_a[0]
582: return true if key == "binary"
583: return have_binary_key?(value) if value.is_a?(Hash)
584: false
585: end
# File lib/active_ldap/schema.rb, line 545
545: def normalize_array_value(value, have_binary_mark)
546: if single_value? and value.reject {|v| v.is_a?(Hash)}.size > 1
547: format = _("Attribute %s can only have a single value: %s")
548: message = format % [human_attribute_name, value.inspect]
549: raise AttributeValueInvalid.new(self, value, message)
550: end
551: if value.empty?
552: if !have_binary_mark and binary_required?
553: [{'binary' => value}]
554: else
555: value
556: end
557: else
558: value.collect do |entry|
559: normalize_value_internal(entry, have_binary_mark)[0]
560: end
561: end
562: end
# File lib/active_ldap/schema.rb, line 564
564: def normalize_hash_value(value, have_binary_mark)
565: if value.size > 1
566: format = _("Attribute %s: Hash must have one key-value pair only: %s")
567: message = format % [human_attribute_name, value.inspect]
568: raise AttributeValueInvalid.new(self, value, message)
569: end
570:
571: if !have_binary_mark and binary_required? and !have_binary_key?(value)
572: [append_binary_key(value)]
573: else
574: key = value.keys[0]
575: have_binary_mark ||= key == "binary"
576: [{key => normalize_value_internal(value.values[0], have_binary_mark)}]
577: end
578: end
# File lib/active_ldap/schema.rb, line 525
525: def normalize_value_internal(value, have_binary_mark)
526: case value
527: when Array
528: normalize_array_value(value, have_binary_mark)
529: when Hash
530: normalize_hash_value(value, have_binary_mark)
531: else
532: if value.blank?
533: value = []
534: else
535: value = send_to_syntax(value, :normalize_value, value)
536: end
537: if !have_binary_mark and binary_required?
538: [{'binary' => value}]
539: else
540: value.is_a?(Array) ? value : [value]
541: end
542: end
543: end
# File lib/active_ldap/schema.rb, line 493
493: def send_to_syntax(default_value, method_name, *args)
494: _syntax = syntax
495: if _syntax
496: _syntax.send(method_name, *args)
497: else
498: default_value
499: end
500: end
# File lib/active_ldap/schema.rb, line 502
502: def validate_each_value(value, option=nil)
503: failed_reason = nil
504: case value
505: when Hash
506: original_option = option
507: value.each do |sub_option, val|
508: opt = [original_option, sub_option].compact.join(";")
509: failed_reason, option = validate_each_value(val, opt)
510: break if failed_reason
511: end
512: when Array
513: original_option = option
514: value.each do |val|
515: failed_reason, option = validate_each_value(val, original_option)
516: break if failed_reason
517: end
518: else
519: failed_reason = send_to_syntax(nil, :validate, value)
520: end
521: return nil if failed_reason.nil?
522: [failed_reason, option]
523: end