Commit 86e8af6

mo <mo.khan@gmail.com>
2019-02-03 21:50:02
extract complex attribute validator class
1 parent 167c05b
lib/scim/kit/v2/attributable.rb
@@ -27,6 +27,10 @@ module Scim
           end
         end
 
+        def attribute_for(name)
+          dynamic_attributes[name.to_s.underscore] || UnknownAttribute.new(name)
+        end
+
         def read_attribute(name)
           attribute = attribute_for(name)
           return attribute._value if attribute._type.multi_valued
@@ -50,10 +54,6 @@ module Scim
 
         private
 
-        def attribute_for(name)
-          dynamic_attributes[name.to_s.underscore] || UnknownAttribute.new(name)
-        end
-
         def create_module_for(type)
           name = type.name.to_sym
           Module.new do
lib/scim/kit/v2/attribute.rb
@@ -44,6 +44,10 @@ module Scim
           true
         end
 
+        def each_value(&block)
+          Array(_value).each(&block)
+        end
+
         private
 
         def server_only?
@@ -78,31 +82,7 @@ module Scim
         end
 
         def validate_complex
-          if _type.multi_valued
-            each_value do |hash|
-              validated = hash.map do |key, value|
-                attribute = attribute_for(key)
-                attribute._assign(value)
-                errors.merge!(attribute.errors) unless attribute.valid?
-
-                key.to_sym
-              end
-              not_validated = map { |x| x._type.name.to_sym } - validated
-              not_validated.each do |key|
-                attribute = attribute_for(key)
-                attribute._assign(hash[key])
-                errors.merge!(attribute.errors) unless attribute.valid?
-              end
-            end
-          else
-            each do |attribute|
-              errors.merge!(attribute.errors) unless attribute.valid?
-            end
-          end
-        end
-
-        def each_value(&block)
-          Array(_value).each(&block)
+          validates_with ComplexAttributeValidator
         end
 
         def multiple
lib/scim/kit/v2/complex_attribute_validator.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    module V2
+      class ComplexAttributeValidator < ::ActiveModel::Validator
+        def validate(item)
+          if item._type.multi_valued
+            item.each_value do |hash|
+              validated = hash.map do |key, value|
+                attribute = item.attribute_for(key)
+                attribute._assign(value)
+                item.errors.merge!(attribute.errors) unless attribute.valid?
+
+                key.to_sym
+              end
+              not_validated = item.map { |x| x._type.name.to_sym } - validated
+              not_validated.each do |key|
+                attribute = item.attribute_for(key)
+                attribute._assign(hash[key])
+                item.errors.merge!(attribute.errors) unless attribute.valid?
+              end
+            end
+          else
+            item.each do |attribute|
+              item.errors.merge!(attribute.errors) unless attribute.valid?
+            end
+          end
+        end
+      end
+    end
+  end
+end
lib/scim/kit/v2.rb
@@ -4,6 +4,7 @@ require 'scim/kit/v2/attributable'
 require 'scim/kit/v2/attribute'
 require 'scim/kit/v2/attribute_type'
 require 'scim/kit/v2/authentication_scheme'
+require 'scim/kit/v2/complex_attribute_validator'
 require 'scim/kit/v2/configuration'
 require 'scim/kit/v2/messages'
 require 'scim/kit/v2/meta'