diff options
| -rw-r--r-- | lib/scim/kit/v2/attributable.rb | 4 | ||||
| -rw-r--r-- | lib/scim/kit/v2/attribute.rb | 28 | ||||
| -rw-r--r-- | lib/scim/kit/v2/templates/attribute.json.jbuilder | 6 | ||||
| -rw-r--r-- | lib/scim/kit/version.rb | 2 | ||||
| -rw-r--r-- | spec/scim/kit/v2/resource_spec.rb | 27 |
5 files changed, 46 insertions, 21 deletions
diff --git a/lib/scim/kit/v2/attributable.rb b/lib/scim/kit/v2/attributable.rb index d9767db..b111332 100644 --- a/lib/scim/kit/v2/attributable.rb +++ b/lib/scim/kit/v2/attributable.rb @@ -20,9 +20,9 @@ module Scim def read_attribute(name) attribute = attribute_for(name) - return attribute._value if attribute.type.multi_valued + return attribute._value if attribute._type.multi_valued - attribute.type.complex? ? attribute : attribute._value + attribute._type.complex? ? attribute : attribute._value end def write_attribute(name, value) diff --git a/lib/scim/kit/v2/attribute.rb b/lib/scim/kit/v2/attribute.rb index 989c2b0..c947e57 100644 --- a/lib/scim/kit/v2/attribute.rb +++ b/lib/scim/kit/v2/attribute.rb @@ -8,23 +8,23 @@ module Scim include ::ActiveModel::Validations include Attributable include Templatable - attr_reader :type + attr_reader :_type attr_reader :_resource attr_reader :_value - validate :presence_of_value, if: proc { |x| x.type.required } - validate :inclusion_of_value, if: proc { |x| x.type.canonical_values } + validate :presence_of_value, if: proc { |x| x._type.required } + validate :inclusion_of_value, if: proc { |x| x._type.canonical_values } validate :validate_type def initialize(resource:, type:, value: nil) - @type = type + @_type = type @_value = value || type.multi_valued ? [] : nil @_resource = resource define_attributes_for(resource, type.attributes) end def _assign(new_value, coerce: true) - @_value = coerce ? type.coerce(new_value) : new_value + @_value = coerce ? _type.coerce(new_value) : new_value end def _value=(new_value) @@ -50,33 +50,33 @@ module Scim end def restricted? - _resource.mode?(:server) && type.returned == Returned::NEVER + _resource.mode?(:server) && _type.returned == Returned::NEVER end def presence_of_value - return unless type.required && _value.blank? + return unless _type.required && _value.blank? - errors.add(type.name, I18n.t('errors.messages.blank')) + errors.add(_type.name, I18n.t('errors.messages.blank')) end def inclusion_of_value - return if type.canonical_values.include?(_value) + return if _type.canonical_values.include?(_value) - errors.add(type.name, I18n.t('errors.messages.inclusion')) + errors.add(_type.name, I18n.t('errors.messages.inclusion')) end def validate_type - return if type.valid?(_value) + return if _type.valid?(_value) - errors.add(type.name, I18n.t('errors.messages.invalid')) + errors.add(_type.name, I18n.t('errors.messages.invalid')) end def read_only? - type.mutability == Mutability::READ_ONLY + _type.mutability == Mutability::READ_ONLY end def write_only? - type.mutability == Mutability::WRITE_ONLY + _type.mutability == Mutability::WRITE_ONLY end end end diff --git a/lib/scim/kit/v2/templates/attribute.json.jbuilder b/lib/scim/kit/v2/templates/attribute.json.jbuilder index 574f601..d6c87c2 100644 --- a/lib/scim/kit/v2/templates/attribute.json.jbuilder +++ b/lib/scim/kit/v2/templates/attribute.json.jbuilder @@ -1,12 +1,12 @@ # frozen_string_literal: true json.key_format! camelize: :lower -if type.complex? && !type.multi_valued - json.set! type.name do +if _type.complex? && !_type.multi_valued + json.set! _type.name do dynamic_attributes.values.each do |attribute| render attribute, json: json end end elsif renderable? - json.set! type.name, _value + json.set! _type.name, _value end diff --git a/lib/scim/kit/version.rb b/lib/scim/kit/version.rb index fef65ae..93082d3 100644 --- a/lib/scim/kit/version.rb +++ b/lib/scim/kit/version.rb @@ -2,6 +2,6 @@ module Scim module Kit - VERSION = '0.2.11' + VERSION = '0.2.12' end end diff --git a/spec/scim/kit/v2/resource_spec.rb b/spec/scim/kit/v2/resource_spec.rb index cdfd6b8..96fa3a6 100644 --- a/spec/scim/kit/v2/resource_spec.rb +++ b/spec/scim/kit/v2/resource_spec.rb @@ -42,6 +42,31 @@ RSpec.describe Scim::Kit::V2::Resource do end end + context 'with attribute named "type"' do + before do + schema.add_attribute(name: 'members') do |attribute| + attribute.mutability = :read_only + attribute.multi_valued = true + attribute.add_attribute(name: 'value') do |z| + z.mutability = :immutable + end + attribute.add_attribute(name: '$ref') do |z| + z.reference_types = %w[User Group] + z.mutability = :immutable + end + attribute.add_attribute(name: 'type') do |z| + z.canonical_values = %w[User Group] + z.mutability = :immutable + end + end + subject.members << { value: SecureRandom.uuid, '$ref' => FFaker::Internet.uri('https'), type: 'User' } + end + + specify { expect(subject.members[0][:type]).to eql('User') } + specify { expect(subject.as_json[:members][0][:type]).to eql('User') } + specify { expect(subject.to_h[:members][0][:type]).to eql('User') } + end + context 'with custom string attribute' do let(:user_name) { FFaker::Internet.user_name } @@ -61,7 +86,7 @@ RSpec.describe Scim::Kit::V2::Resource do specify { expect(subject.type).to eql('User') } specify { expect(subject.as_json[:type]).to eql('User') } - specify { expect(subject.send(:attribute_for, :type).type).to be_instance_of(Scim::Kit::V2::AttributeType) } + specify { expect(subject.send(:attribute_for, :type)._type).to be_instance_of(Scim::Kit::V2::AttributeType) } end context 'with a complex attribute' do |
