Class: UCB::LDAP::Schema::Attribute
- Inherits:
-
Object
- Object
- UCB::LDAP::Schema::Attribute
- 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
-
#aliases ⇒ Object
Returns Array of aliases as found in schema.
-
#apply_type_to_array(array) ⇒ Object
Cast each element to correct type.
-
#apply_type_to_scalar(string) ⇒ Object
Case element to correct type.
-
#boolean? ⇒ Boolean
Return
true
if attribute type is boolean. -
#description ⇒ Object
Returns attribute description.
-
#get_value(array) ⇒ Object
Takes a value returned from an LDAP attribute (
Array
ofString
) and returns value with correct cardinality (array or scalar) cast to correct #type. -
#initialize(args) ⇒ Attribute
constructor
Constructor called by UCB::LDAP::Entry.set_schema_attributes().
-
#integer? ⇒ Boolean
Return
true
if attribute type is integer. -
#ldap_value(value) ⇒ Object
Returns a value in LDAP attribute value format (
Array
ofString
). -
#multi_valued? ⇒ Boolean
Returns
true
if attribute is multi-valued, elsefalse
. -
#name ⇒ Object
Returns attribute name as found in the schema.
-
#required? ⇒ Boolean
Returns
true
if attribute is required, elsefalse
. -
#string? ⇒ Boolean
Return
true
if attribute type is string. -
#timestamp? ⇒ Boolean
Return
true
if attribute type is timestamp. -
#type ⇒ Object
Returns (data) type.
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
#aliases ⇒ Object
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 raise "unknown type '#{type}' for attribute '#{name}'" end |
#boolean? ⇒ Boolean
Return true
if attribute type is boolean.
125 126 127 |
# File 'lib/ucb_ldap/schema_attribute.rb', line 125 def boolean? type == "boolean" end |
#description ⇒ Object
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.
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.
83 84 85 |
# File 'lib/ucb_ldap/schema_attribute.rb', line 83 def multi_valued? @multi_valued end |
#name ⇒ Object
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
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.
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
130 131 132 |
# File 'lib/ucb_ldap/schema_attribute.rb', line 130 def type == "timestamp" end |
#type ⇒ Object
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 |