Class: Samlsso::Attributes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/samlsso/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

Instance Method Summary collapse

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'],
})


32
33
34
# File 'lib/samlsso/attributes.rb', line 32

def initialize(attrs = {})
  @attributes = attrs
end

Class Method Details

.single_value_compatibilityObject

Get current status of backwards compatibility mode.



16
17
18
# File 'lib/samlsso/attributes.rb', line 16

def self.single_value_compatibility
  @@single_value_compatibility
end

.single_value_compatibility=(value) ⇒ Object

Sets the backwards compatibility mode on/off.



21
22
23
# File 'lib/samlsso/attributes.rb', line 21

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



88
89
90
91
92
93
94
# File 'lib/samlsso/attributes.rb', line 88

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]']


66
67
68
# File 'lib/samlsso/attributes.rb', line 66

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



82
83
84
85
# File 'lib/samlsso/attributes.rb', line 82

def add(name, values = [])
  attributes[canonize_name(name)] ||= []
  attributes[canonize_name(name)] += Array(values)
end

#allObject

Return all attributes as an array



71
72
73
# File 'lib/samlsso/attributes.rb', line 71

def all
  attributes
end

#eachObject

Iterate over all attributes



38
39
40
# File 'lib/samlsso/attributes.rb', line 38

def each
  attributes.each{|name, values| yield name, values}
end

#include?(name) ⇒ Boolean

Test attribute presence by name

Returns:

  • (Boolean)


43
44
45
# File 'lib/samlsso/attributes.rb', line 43

def include?(name)
  attributes.has_key?(canonize_name(name))
end

#multi(name) ⇒ Object

Return all values for an attribute



53
54
55
# File 'lib/samlsso/attributes.rb', line 53

def multi(name)
  attributes[canonize_name(name)]
end

#set(name, values) ⇒ Object Also known as: []=

Set values for an attribute, overwriting all existing values



76
77
78
# File 'lib/samlsso/attributes.rb', line 76

def set(name, values)
  attributes[canonize_name(name)] = values
end

#single(name) ⇒ Object

Return first value for an attribute



48
49
50
# File 'lib/samlsso/attributes.rb', line 48

def single(name)
  attributes[canonize_name(name)].first if include?(name)
end