Commit 926d43e
Changed files (3)
lib
scim
lib/scim/kit/v2/attributable.rb
@@ -3,6 +3,7 @@
module Scim
module Kit
module V2
+ # Represents a dynamic attribute
module Attributable
attr_reader :dynamic_attributes
@@ -22,7 +23,7 @@ module Scim
@dynamic_attributes[target].value = args[0]
else
attribute = @dynamic_attributes[method]
- attribute.type.complex? ? @dynamic_attributes[method] : @dynamic_attributes[method].value
+ attribute.type.complex? ? attribute : attribute.value
end
end
lib/scim/kit/v2/attribute.rb
@@ -17,29 +17,12 @@ module Scim
end
def value=(new_value)
- case type.type
- when :string
- @value = new_value.to_s
- when :boolean
- raise ArgumentError, new_value unless [true, false].include?(new_value)
+ @value = type.coerce(new_value)
- @value = new_value
- when :decimal
- @value = new_value.to_f
- when :integer
- @value = new_value.to_i
- when :datetime
- @value = new_value.is_a?(::String) ?
- DateTime.parse(new_value) :
- new_value
- when :binary
- @value = Base64.strict_encode64(new_value)
- when :reference
- @value = new_value
- end
-
- if type.canonical_values && !type.canonical_values.empty?
- raise ArgumentError, new_value unless type.canonical_values.include?(new_value)
+ if type.canonical_values &&
+ !type.canonical_values.empty? &&
+ !type.canonical_values.include?(new_value)
+ raise ArgumentError, new_value
end
end
end
lib/scim/kit/v2/attribute_type.rb
@@ -16,6 +16,13 @@ module Scim
reference: 'reference',
complex: 'complex'
}.freeze
+ COERCION = {
+ string: ->(x) { x.to_s },
+ decimal: ->(x) { x.to_f },
+ integer: ->(x) { x.to_i },
+ datetime: ->(x) { x.is_a?(::String) ? DateTime.parse(x) : x },
+ binary: ->(x) { Base64.strict_encode64(x) }
+ }.freeze
attr_accessor :canonical_values
attr_accessor :case_exact
attr_accessor :description
@@ -72,6 +79,15 @@ module Scim
type_is?(:complex)
end
+ def coerce(value)
+ if type_is?(:boolean) && ![true, false].include?(value)
+ raise ArgumentError, value
+ end
+
+ coercion = COERCION[type]
+ coercion ? coercion.call(value) : value
+ end
+
private
def string?