Class: OneLogin::RubySaml::Attributes
- Inherits:
-
Object
- Object
- OneLogin::RubySaml::Attributes
- Includes:
- Enumerable
- Defined in:
- lib/onelogin/ruby-saml/attributes.rb
Overview
Wraps all attributes and provides means to query them for single or multiple values.
For backwards compatibility Attributes#[] returns first value for the attribute. Turn off compatibility to make it return all values as an array:
Attributes.single_value_compatibility = false
Constant Summary collapse
- @@single_value_compatibility =
By default Attributes#[] is backwards compatible and returns only the first value for the attribute Setting this to ‘false` returns all values for an attribute
true
Class Method Summary collapse
-
.single_value_compatibility ⇒ Object
Get current status of backwards compatibility mode.
-
.single_value_compatibility=(value) ⇒ Object
Sets the backwards compatibility mode on/off.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Make comparable to another Attributes collection based on attributes.
-
#[](name) ⇒ Object
By default returns first value for an attribute.
-
#add(name, values = []) ⇒ Object
Add new attribute or new value(s) to an existing attribute.
-
#all ⇒ Object
Return all attributes as an array.
-
#each ⇒ Object
Iterate over all attributes.
-
#include?(name) ⇒ Boolean
Test attribute presence by name.
-
#initialize(attrs = {}) ⇒ Attributes
constructor
Initialize Attributes collection, optionally taking a Hash of attribute names and values.
-
#multi(name) ⇒ Object
Return all values for an attribute.
-
#set(name, values) ⇒ Object
(also: #[]=)
Set values for an attribute, overwriting all existing values.
-
#single(name) ⇒ Object
Return first value for an attribute.
Constructor Details
#initialize(attrs = {}) ⇒ Attributes
Initialize Attributes collection, optionally taking a Hash of attribute names and values.
The attrs must be a Hash with attribute names as keys and arrays as values:
Attributes.new({
'name' => ['value1', 'value2'],
'mail' => ['value1'],
})
33 34 35 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 33 def initialize(attrs = {}) @attributes = attrs end |
Class Method Details
.single_value_compatibility ⇒ Object
Get current status of backwards compatibility mode.
17 18 19 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 17 def self.single_value_compatibility @@single_value_compatibility end |
.single_value_compatibility=(value) ⇒ Object
Sets the backwards compatibility mode on/off.
22 23 24 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 22 def self.single_value_compatibility=(value) @@single_value_compatibility = value end |
Instance Method Details
#==(other) ⇒ Object
Make comparable to another Attributes collection based on attributes
89 90 91 92 93 94 95 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 89 def ==(other) if other.is_a?(Attributes) all == other.all else super end end |
#[](name) ⇒ Object
By default returns first value for an attribute.
Depending on the single value compatibility status this returns first value
Attributes.single_value_compatibility = true # Default
response.attributes['mail'] # => '[email protected]'
Or all values:
Attributes.single_value_compatibility = false
response.attributes['mail'] # => ['[email protected]','[email protected]']
67 68 69 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 67 def [](name) self.class.single_value_compatibility ? single(canonize_name(name)) : multi(canonize_name(name)) end |
#add(name, values = []) ⇒ Object
Add new attribute or new value(s) to an existing attribute
83 84 85 86 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 83 def add(name, values = []) attributes[canonize_name(name)] ||= [] attributes[canonize_name(name)] += Array(values) end |
#all ⇒ Object
Return all attributes as an array
72 73 74 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 72 def all attributes end |
#each ⇒ Object
Iterate over all attributes
39 40 41 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 39 def each attributes.each{|name, values| yield name, values} end |
#include?(name) ⇒ Boolean
Test attribute presence by name
44 45 46 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 44 def include?(name) attributes.has_key?(canonize_name(name)) end |
#multi(name) ⇒ Object
Return all values for an attribute
54 55 56 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 54 def multi(name) attributes[canonize_name(name)] end |
#set(name, values) ⇒ Object Also known as: []=
Set values for an attribute, overwriting all existing values
77 78 79 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 77 def set(name, values) attributes[canonize_name(name)] = values end |
#single(name) ⇒ Object
Return first value for an attribute
49 50 51 |
# File 'lib/onelogin/ruby-saml/attributes.rb', line 49 def single(name) attributes[canonize_name(name)].first if include?(name) end |