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('berkeleyEduUasEligFlag') # case doesn't matter
uas_attr.name #=> 'berkeleyEduUasEligFlag'
uas_attr.aliases #=> ['ucbvalidflag']
uas_attr.description #=> 'UAS Eligibility Flag'
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().
36 37 38 39 40 41 42 43 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 36 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.
53 54 55 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 53 def aliases @aliases end |
#apply_type_to_array(array) ⇒ Object
Cast each element to correct type.
102 103 104 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 102 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
107 108 109 110 111 112 113 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 107 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.
126 127 128 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 126 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”.
73 74 75 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 73 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.
91 92 93 94 95 96 97 98 99 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 91 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.
121 122 123 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 121 def integer? type == "integer" end |
#ldap_value(value) ⇒ Object
Returns a value in LDAP attribute value format (Array
of String
).
136 137 138 139 140 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 136 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.
84 85 86 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 84 def multi_valued? @multi_valued end |
#name ⇒ Object
Returns attribute name as found in the schema
46 47 48 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 46 def name @name end |
#required? ⇒ Boolean
Returns true
if attribute is required, else false
78 79 80 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 78 def required? @required end |
#string? ⇒ Boolean
Return true
if attribute type is string.
116 117 118 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 116 def string? type == "string" end |
#timestamp? ⇒ Boolean
Return true
if attribute type is timestamp
131 132 133 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 131 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)
66 67 68 |
# File 'lib/ucb_ldap_schema_attribute.rb', line 66 def type @type end |