Commit 3777ea3

mo <mo.khan@gmail.com>
2019-01-06 02:55:11
generate attr_accessor defined in schema
1 parent f207c6d
Changed files (4)
bin
lib
spec
scim
bin/console
@@ -2,7 +2,7 @@
 # frozen_string_literal: true
 
 require 'bundler/setup'
-require 'saml/kit'
+require 'scim/kit'
 
 # You can add fixtures and/or initialization code here to make experimenting
 # with your gem easier. You can also use a different console, if you like.
lib/scim/kit/v2/resource.rb
@@ -6,11 +6,30 @@ module Scim
       # Represents a SCIM Schema
       class Resource
         include Templatable
+
         attr_accessor :id, :external_id
         attr_reader :meta
 
         def initialize(schema:, location:)
           @meta = Meta.new(schema.id, location)
+          @dynamic_attributes = Hash[
+            schema.attributes.map { |x| [x.name.underscore, nil] }
+          ].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] = args[0]
+          else
+            @dynamic_attributes[method]
+          end
+        end
+
+        def respond_to_missing?(method, _include_private = false)
+          @dynamic_attributes.key?(method) || super
         end
       end
     end
lib/scim/kit.rb
@@ -2,6 +2,7 @@
 
 require 'tilt'
 require 'tilt/jbuilder'
+require 'active_support/core_ext/hash/indifferent_access'
 
 require 'scim/kit/dynamic_attributes'
 require 'scim/kit/templatable'
spec/scim/kit/v2/resource_spec.rb
@@ -2,10 +2,11 @@
 
 RSpec.describe Scim::Kit::V2::Resource do
   subject { described_class.new(schema: schema, location: resource_location) }
+
   let(:schema) { Scim::Kit::V2::Schema.new(id: 'User', name: 'User', location: FFaker::Internet.uri('https')) }
   let(:resource_location) { FFaker::Internet.uri('https') }
 
-  context "with common attributes" do
+  context 'with common attributes' do
     let(:id) { SecureRandom.uuid }
     let(:external_id) { SecureRandom.uuid }
     let(:created_at) { Time.now }
@@ -28,7 +29,7 @@ RSpec.describe Scim::Kit::V2::Resource do
     specify { expect(subject.meta.last_modified).to eql(updated_at) }
     specify { expect(subject.meta.version).to eql(version) }
 
-    describe "#as_json" do
+    describe '#as_json' do
       specify { expect(subject.as_json[:id]).to eql(id) }
       specify { expect(subject.as_json[:externalId]).to eql(external_id) }
       specify { expect(subject.as_json[:meta][:resourceType]).to eql('User') }
@@ -38,4 +39,15 @@ RSpec.describe Scim::Kit::V2::Resource do
       specify { expect(subject.as_json[:meta][:version]).to eql(version) }
     end
   end
+
+  context 'with custom string attribute' do
+    let(:user_name) { FFaker::Internet.user_name }
+
+    before do
+      schema.add_attribute(name: 'userName')
+      subject.user_name = user_name
+    end
+
+    specify { expect(subject.user_name).to eql(user_name) }
+  end
 end