Commit a6b3cc6

mokha <mokha@cisco.com>
2019-01-16 16:48:53
allow initializing resource with attributes tag: v0.2.13
1 parent 6e25b32
Changed files (2)
lib
scim
spec
scim
lib/scim/kit/v2/resource.rb
@@ -15,13 +15,14 @@ module Scim
 
         validate :schema_validations
 
-        def initialize(schemas:, location: nil)
+        def initialize(schemas:, location: nil, attributes: {})
           @meta = Meta.new(schemas[0].name, location)
           @meta.disable_timestamps
           @schemas = schemas
           schemas.each do |schema|
             define_attributes_for(self, schema.attributes)
           end
+          assign_attributes(attributes)
           yield self if block_given?
         end
 
spec/scim/kit/v2/resource_spec.rb
@@ -427,5 +427,47 @@ RSpec.describe Scim::Kit::V2::Resource do
 
       specify { expect(subject.preferred_name).to eql('hunk') }
     end
+
+    context 'when initializing the resource with attributes' do
+      subject { described_class.new(schemas: schemas, attributes: attributes) }
+
+      let(:user_name) { FFaker::Internet.user_name }
+      let(:email) { FFaker::Internet.email }
+      let(:attributes) do
+        {
+          schemas: schemas.map(&:id),
+          userName: user_name,
+          age: 34,
+          colours: %w[red green blue],
+          name: { given_name: 'Tsuyoshi', family_name: 'Garrett' },
+          emails: [{ value: email, primary: true }]
+        }
+      end
+
+      before do
+        schema.add_attribute(name: :user_name)
+        schema.add_attribute(name: :age, type: :integer)
+        schema.add_attribute(name: :colours, type: :string) do |x|
+          x.multi_valued = true
+        end
+        schema.add_attribute(name: :name) do |x|
+          x.add_attribute(name: :given_name)
+          x.add_attribute(name: :family_name)
+        end
+        schema.add_attribute(name: :emails) do |x|
+          x.multi_valued = true
+          x.add_attribute(name: :value)
+          x.add_attribute(name: :primary, type: :boolean)
+        end
+      end
+
+      specify { expect(subject.user_name).to eql(user_name) }
+      specify { expect(subject.age).to be(34) }
+      specify { expect(subject.colours).to match_array(%w[red green blue]) }
+      specify { expect(subject.name.given_name).to eql('Tsuyoshi') }
+      specify { expect(subject.name.family_name).to eql('Garrett') }
+      specify { expect(subject.emails[0][:value]).to eql(email) }
+      specify { expect(subject.emails[0][:primary]).to be(true) }
+    end
   end
 end