Class: UCB::LDAP::Schema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/ucb_ldap/schema_attribute.rb

Overview

UCB::LDAP::SchemaAttribute

This class models schema information about an LDAP attribute.

This class is used internally by various UCB::LDAP classes. Users of UCB::LDAP probably won’t need to interact with this class directly.

The LDAP entity classes have access to their Attribute’s.

uid_attr = UCB::LDAP::Person.attribute(:uid) # :symbol ok as attribute name

uid_attr.name             #=> 'uid'
uid_attr.aliases          #=> ['userid']
uid_attr.description      #=> 'Standard LDAP attribute type'
uid_attr.multi_valued?    #=> true
uid_attr.required?        #=> true
uid_attr.type             #=> 'string'

uas_attr = UCB::LDAP::Person.attribute('berkeleyEduNameGenerational') # case doesn't matter

uas_attr.name             #=> 'berkeleyEduNameGenerational'
uas_attr.aliases          #=> ['ucbvalidflag']
uas_attr.description      #=> 'Generational Name'
uas_attr.multi_valued?    #=> false
uas_attr.required?        #=> false
uas_attr.type             #=> 'boolean'

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Attribute

Constructor called by UCB::LDAP::Entry.set_schema_attributes().



35
36
37
38
39
40
41
42
# File 'lib/ucb_ldap/schema_attribute.rb', line 35

def initialize(args) #:nodoc:
  @name = args["name"]
  @type = args["syntax"]
  @aliases = args["aliases"] || []
  @description = args["description"]
  @required = args["required"]
  @multi_valued = args["multi"]
end

Instance Method Details

#aliasesObject

Returns Array of aliases as found in schema. Returns empty Array ([]) if no aliases.



52
53
54
# File 'lib/ucb_ldap/schema_attribute.rb', line 52

def aliases
  @aliases
end

#apply_type_to_array(array) ⇒ Object

Cast each element to correct type.



101
102
103
# File 'lib/ucb_ldap/schema_attribute.rb', line 101

def apply_type_to_array(array) #:nodoc:
  array.map{|scalar| apply_type_to_scalar scalar}
end

#apply_type_to_scalar(string) ⇒ Object

Case element to correct type



106
107
108
109
110
111
112
# File 'lib/ucb_ldap/schema_attribute.rb', line 106

def apply_type_to_scalar(string) #:nodoc:
  return string if string?
  return string.to_i if integer?
  return %w{true 1}.include?(string) ? true : false if boolean?
  return UCB::LDAP.local_datetime_parse(string) if timestamp?
  raise "unknown type '#{type}' for attribute '#{name}'"
end

#boolean?Boolean

Return true if attribute type is boolean.

Returns:

  • (Boolean)


125
126
127
# File 'lib/ucb_ldap/schema_attribute.rb', line 125

def boolean?
  type == "boolean"
end

#descriptionObject

Returns attribute description. Of limited value since all standard LDAP attributes have a description of “Standard LDAP attribute type”.



72
73
74
# File 'lib/ucb_ldap/schema_attribute.rb', line 72

def description
  @description
end

#get_value(array) ⇒ Object

Takes a value returned from an LDAP attribute (Array of String) and returns value with correct cardinality (array or scalar) cast to correct #type.



90
91
92
93
94
95
96
97
98
# File 'lib/ucb_ldap/schema_attribute.rb', line 90

def get_value(array)
  if array.nil?
    return false if boolean?
    return [] if multi_valued?
    return nil
  end
  typed_array = apply_type_to_array(array)
  multi_valued? ? typed_array : typed_array.first
end

#integer?Boolean

Return true if attribute type is integer.

Returns:

  • (Boolean)


120
121
122
# File 'lib/ucb_ldap/schema_attribute.rb', line 120

def integer?
  type == "integer"
end

#ldap_value(value) ⇒ Object

Returns a value in LDAP attribute value format (Array of String).



135
136
137
138
139
# File 'lib/ucb_ldap/schema_attribute.rb', line 135

def ldap_value(value)
  return nil if value.nil?
  return value.map{|v| ldap_value_stripped(v)} if value.instance_of?(Array)
  return [ldap_value_stripped(value)]
end

#multi_valued?Boolean

Returns true if attribute is multi-valued, else false. Multi-valued attribute values are returned as an Array.

Returns:

  • (Boolean)


83
84
85
# File 'lib/ucb_ldap/schema_attribute.rb', line 83

def multi_valued?
  @multi_valued
end

#nameObject

Returns attribute name as found in the schema



45
46
47
# File 'lib/ucb_ldap/schema_attribute.rb', line 45

def name
  @name
end

#required?Boolean

Returns true if attribute is required, else false

Returns:

  • (Boolean)


77
78
79
# File 'lib/ucb_ldap/schema_attribute.rb', line 77

def required?
  @required
end

#string?Boolean

Return true if attribute type is string.

Returns:

  • (Boolean)


115
116
117
# File 'lib/ucb_ldap/schema_attribute.rb', line 115

def string?
  type == "string"
end

#timestamp?Boolean

Return true if attribute type is timestamp

Returns:

  • (Boolean)


130
131
132
# File 'lib/ucb_ldap/schema_attribute.rb', line 130

def timestamp?
  type == "timestamp"
end

#typeObject

Returns (data) type. Used by get_value() to cast value to correct Ruby type.

Supported types and corresponding Ruby type:

* string      String
* integer     Fixnum
* boolean     TrueClass / FalseClass
* timestamp   DateTime (convenience methods may return Date if attribute's semantics don't include time)


65
66
67
# File 'lib/ucb_ldap/schema_attribute.rb', line 65

def type
  @type
end