Commit ed792bf

mokha <mokha@cisco.com>
2019-01-07 16:28:22
start to support complex attributes
1 parent fcd59af
lib/scim/kit/v2/attributable.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    module V2
+      module Attributable
+        def define_attributes_for(types)
+          @dynamic_attributes = Hash[
+            types.map do |x|
+              [x.name.underscore, Attribute.new(type: x)]
+            end
+          ].with_indifferent_access
+        end
+
+        def method_missing(method, *args)
+          if method.match?(/=/)
+            target = method.to_s.delete('=')
+            return super unless respond_to_missing?(target)
+
+            @dynamic_attributes[target].value = args[0]
+          else
+            @dynamic_attributes[method].value
+          end
+        end
+
+        def respond_to_missing?(method, _include_private = false)
+          @dynamic_attributes.key?(method) || super
+        end
+      end
+    end
+  end
+end
lib/scim/kit/v2/attribute.rb
@@ -5,12 +5,14 @@ module Scim
     module V2
       # Represents a SCIM Attribute
       class Attribute
+        include Attributable
         attr_reader :type
         attr_reader :value
 
         def initialize(type:, value: nil)
           @type = type
           @value = value
+          define_attributes_for(type.attributes)
         end
 
         def value=(new_value)
lib/scim/kit/v2/attribute_type.rb
@@ -64,12 +64,12 @@ module Scim
           @reference_types = value
         end
 
-        private
-
         def attributes
           @attributes ||= []
         end
 
+        private
+
         def complex?
           type_is?(:complex)
         end
lib/scim/kit/v2/resource.rb
@@ -5,6 +5,7 @@ module Scim
     module V2
       # Represents a SCIM Resource
       class Resource
+        include Attributable
         include Templatable
 
         attr_accessor :id, :external_id
@@ -12,26 +13,7 @@ module Scim
 
         def initialize(schema:, location:)
           @meta = Meta.new(schema.id, location)
-          @dynamic_attributes = Hash[
-            schema.attributes.map do |x|
-              [x.name.underscore, Attribute.new(type: x)]
-            end
-          ].with_indifferent_access
-        end
-
-        def method_missing(method, *args)
-          if method.match?(/=/)
-            target = method.to_s.delete('=')
-            return super unless respond_to_missing?(target)
-
-            @dynamic_attributes[target].value = args[0]
-          else
-            @dynamic_attributes[method].value
-          end
-        end
-
-        def respond_to_missing?(method, _include_private = false)
-          @dynamic_attributes.key?(method) || super
+          define_attributes_for(schema.attributes)
         end
       end
     end
lib/scim/kit.rb
@@ -9,6 +9,7 @@ require 'scim/kit/templatable'
 require 'scim/kit/template'
 require 'scim/kit/version'
 
+require 'scim/kit/v2/attributable'
 require 'scim/kit/v2/attribute'
 require 'scim/kit/v2/attribute_type'
 require 'scim/kit/v2/authentication_scheme'
spec/scim/kit/v2/attribute_spec.rb
@@ -134,4 +134,21 @@ RSpec.describe Scim::Kit::V2::Attribute do
 
     specify { expect(subject.value).to eql(uri) }
   end
+
+  context 'with complex type' do
+    let(:type) do
+      x = Scim::Kit::V2::AttributeType.new(name: 'name', type: :complex)
+      x.add_attribute(name: 'familyName')
+      x.add_attribute(name: 'givenName')
+      x
+    end
+
+    before do
+      subject.family_name = 'mo'
+      subject.given_name = 'khan'
+    end
+
+    specify { expect(subject.family_name).to eql('mo') }
+    specify { expect(subject.given_name).to eql('khan') }
+  end
 end