lib/scim/kit/v2/attribute.rb in scim-kit-0.2.9 vs lib/scim/kit/v2/attribute.rb in scim-kit-0.2.10

- old
+ new

@@ -7,30 +7,40 @@ class Attribute include ::ActiveModel::Validations include Attributable include Templatable 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 :validate_type - def initialize(type:, value: nil) + def initialize(resource:, type:, value: nil) @type = type - @_value = value - define_attributes_for(type.attributes) + @_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 end def _value=(new_value) _assign(new_value, coerce: true) end + def renderable? + return false if read_only? && _resource.mode?(:client) + return false if write_only? && + (_resource.mode?(:server) || _value.blank?) + + true + end + private def presence_of_value return unless type.required && _value.blank? @@ -45,9 +55,17 @@ def validate_type return if type.valid?(_value) errors.add(type.name, I18n.t('errors.messages.invalid')) + end + + def read_only? + type.mutability == Mutability::READ_ONLY + end + + def write_only? + type.mutability == Mutability::WRITE_ONLY end end end end end