class ActiveLdap::Schema::Attribute

Attributes

super_attribute[R]

Public Class Methods

new(name, schema) click to toggle source
Calls superclass method ActiveLdap::Schema::Entry::new
# File lib/active_ldap/schema.rb, line 398
def initialize(name, schema)
  super(name, schema, "attributeTypes")
end

Public Instance Methods

apply_encoding(value) click to toggle source

Sets binary encoding to value if the given attribute’s syntax is binary syntax. Does nothing otherwise. @return [void]

# File lib/active_ldap/schema.rb, line 430
def apply_encoding(value)
  return unless binary?
  case value
  when Hash
    value.each_value do |sub_value|
      apply_encoding(sub_value)
    end
  when Array
    value.each do |sub_value|
      apply_encoding(sub_value)
    end
  else
    return unless value.respond_to?(:force_encoding)
    value.force_encoding("ASCII-8BIT")
  end
end
binary?() click to toggle source

binary?

Returns true if the given attribute’s syntax is binary syntax, X-NOT-HUMAN-READABLE or X-BINARY-TRANSFER-REQUIRED

# File lib/active_ldap/schema.rb, line 423
def binary?
  @binary
end
binary_required?() click to toggle source

binary_required?

Returns true if the value MUST be transferred in binary

# File lib/active_ldap/schema.rb, line 450
def binary_required?
  @binary_required
end
directory_operation?() click to toggle source

directory_operation?

Returns true if an attribute is directory operation. It means that USAGE contains directoryOperation.

# File lib/active_ldap/schema.rb, line 458
def directory_operation?
  @directory_operation
end
human_attribute_description() click to toggle source
# File lib/active_ldap/schema.rb, line 497
def human_attribute_description
  self.class.human_attribute_description(self)
end
human_attribute_name() click to toggle source
# File lib/active_ldap/schema.rb, line 493
def human_attribute_name
  self.class.human_attribute_name(self)
end
normalize_value(value) click to toggle source
# File lib/active_ldap/schema.rb, line 485
def normalize_value(value)
  normalize_value_internal(value, false)
end
read_only?() click to toggle source

read_only?

Returns true if an attribute is read-only NO-USER-MODIFICATION

# File lib/active_ldap/schema.rb, line 406
def read_only?
  @read_only
end
single_value?() click to toggle source

single_value?

Returns true if an attribute can only have one value defined SINGLE-VALUE

# File lib/active_ldap/schema.rb, line 415
def single_value?
  @single_value
end
syntax() click to toggle source
# File lib/active_ldap/schema.rb, line 462
def syntax
  @derived_syntax
end
syntax_description() click to toggle source
# File lib/active_ldap/schema.rb, line 489
def syntax_description
  send_to_syntax(nil, :description)
end
to_hash() click to toggle source
# File lib/active_ldap/schema.rb, line 501
def to_hash
  {
    :read_only => read_only?,
    :single_value => single_value?,
    :binary => binary?,
    :binary_required => binary_required?,
    :directory_operation => directory_operation?,
    :syntax => syntax,
    :syntax_description => syntax_description,
  }
end
type_cast(value) click to toggle source
# File lib/active_ldap/schema.rb, line 481
def type_cast(value)
  send_to_syntax(value, :type_cast, value)
end
valid?(value) click to toggle source
# File lib/active_ldap/schema.rb, line 466
def valid?(value)
  validate(value).nil?
end
validate(value) click to toggle source
# File lib/active_ldap/schema.rb, line 470
def validate(value)
  error_info = validate_each_value(value)
  return error_info if error_info
  begin
    normalize_value(value)
    nil
  rescue AttributeValueInvalid
    [$!.message]
  end
end

Private Instance Methods

append_binary_key(hash) click to toggle source
# File lib/active_ldap/schema.rb, line 636
def append_binary_key(hash)
  key, value = hash.to_a[0]
  if value.is_a?(Hash)
    append_binary_key(value)
  else
    hash.merge(key => {"binary" => value})
  end
end
attribute(attribute_name, name=@name) click to toggle source
# File lib/active_ldap/schema.rb, line 514
def attribute(attribute_name, name=@name)
  @schema.attribute_type(name, attribute_name)
end
collect_info() click to toggle source
# File lib/active_ldap/schema.rb, line 518
def collect_info
  @description = attribute("DESC")[0]
  @super_attribute = attribute("SUP")[0]
  if @super_attribute
    @super_attribute = @schema.attribute(@super_attribute)
    @super_attribute = nil if @super_attribute.id.nil?
  end
  @read_only = attribute('NO-USER-MODIFICATION')[0] == 'TRUE'
  @single_value = attribute('SINGLE-VALUE')[0] == 'TRUE'
  @syntax = attribute("SYNTAX")[0]
  @syntax = @schema.ldap_syntax(@syntax) if @syntax
  if @syntax
    @binary_required = @syntax.binary_transfer_required?
    @binary = @syntax.binary?
    @derived_syntax = @syntax
  else
    @binary_required = false
    @binary = false
    @derived_syntax = nil
    @derived_syntax = @super_attribute.syntax if @super_attribute
  end
  @directory_operation = attribute("USAGE").include?("directoryOperation")
end
have_binary_key?(hash) click to toggle source
# File lib/active_ldap/schema.rb, line 629
def have_binary_key?(hash)
  key, value = hash.to_a[0]
  return true if key == "binary"
  return have_binary_key?(value) if value.is_a?(Hash)
  false
end
normalize_array_value(value, have_binary_mark) click to toggle source
# File lib/active_ldap/schema.rb, line 594
def normalize_array_value(value, have_binary_mark)
  if single_value? and value.reject {|v| v.is_a?(Hash)}.size > 1
    format = _("Attribute %s can only have a single value: %s")
    message = format % [human_attribute_name, value.inspect]
    raise AttributeValueInvalid.new(self, value, message)
  end
  if value.empty?
    if !have_binary_mark and binary_required?
      [{'binary' => value}]
    else
      value
    end
  else
    value.collect do |entry|
      normalize_value_internal(entry, have_binary_mark)[0]
    end
  end
end
normalize_hash_value(value, have_binary_mark) click to toggle source
# File lib/active_ldap/schema.rb, line 613
def normalize_hash_value(value, have_binary_mark)
  if value.size > 1
    format = _("Attribute %s: Hash must have one key-value pair only: %s")
    message = format % [human_attribute_name, value.inspect]
    raise AttributeValueInvalid.new(self, value, message)
  end

  if !have_binary_mark and binary_required? and !have_binary_key?(value)
    [append_binary_key(value)]
  else
    key = value.keys[0]
    have_binary_mark ||= key == "binary"
    [{key => normalize_value_internal(value.values[0], have_binary_mark)}]
  end
end
normalize_value_internal(value, have_binary_mark) click to toggle source
# File lib/active_ldap/schema.rb, line 574
def normalize_value_internal(value, have_binary_mark)
  case value
  when Array
    normalize_array_value(value, have_binary_mark)
  when Hash
    normalize_hash_value(value, have_binary_mark)
  else
    if value.nil?
      value = []
    else
      value = send_to_syntax(value, :normalize_value, value)
    end
    if !have_binary_mark and binary_required?
      [{'binary' => value}]
    else
      value.is_a?(Array) ? value : [value]
    end
  end
end
send_to_syntax(default_value, method_name, *args) click to toggle source
# File lib/active_ldap/schema.rb, line 542
def send_to_syntax(default_value, method_name, *args)
  _syntax = syntax
  if _syntax
    _syntax.send(method_name, *args)
  else
    default_value
  end
end
validate_each_value(value, option=nil) click to toggle source
# File lib/active_ldap/schema.rb, line 551
def validate_each_value(value, option=nil)
  failed_reason = nil
  case value
  when Hash
    original_option = option
    value.each do |sub_option, val|
      opt = [original_option, sub_option].compact.join(";")
      failed_reason, option = validate_each_value(val, opt)
      break if failed_reason
    end
  when Array
    original_option = option
    value.each do |val|
      failed_reason, option = validate_each_value(val, original_option)
      break if failed_reason
    end
  else
    failed_reason = send_to_syntax(nil, :validate, value)
  end
  return nil if failed_reason.nil?
  [failed_reason, option]
end