Sha256: dfd0195cd1bf566cb5a8f210abaadc477021dc886ce9f4d9e3ced4fdaf72b31d

Contents?: true

Size: 1.27 KB

Versions: 15

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require_relative 'value'

module LedgerSync
  module Type
    class ReferenceMany < Value # :nodoc:
      include ValueMixin

      attr_reader :resource_class

      def initialize(resource_class:)
        @resource_class = resource_class
        super()
      end

      def cast(args = {})
        value = args.fetch(:value)
        return if value.nil?

        many_array_class = LedgerSync::ResourceAttribute::Reference::Many::ManyArray

        return value if value.is_a?(many_array_class)
        unless value.is_a?(Array)
          raise "Cannot convert #{value.class} to LedgerSync::ResourceAttribute::Reference::Many::ManyArray"
        end

        many_array_class.new(value)
      end

      def type
        :reference_many
      end

      def valid?(value:)
        return false unless value.is_a?(Array)
        return true if (resource_classes & value.map(&:class)).any?
        return true if value.is_a?(Array) && value.empty?

        false
      end

      private

      def resource_classes
        @resource_classes ||= if resource_class.is_a?(Array)
                                resource_class
                              else
                                [resource_class]
                              end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ledger_sync-2.0.2 lib/ledger_sync/type/reference_many.rb
ledger_sync-2.0.1 lib/ledger_sync/type/reference_many.rb
ledger_sync-2.0.0 lib/ledger_sync/type/reference_many.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.8.1 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.8.0 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.7.0 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.6.0 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.5.2 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.5.1 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.5.0 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.4.4 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.4.2 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.4.1 lib/ledger_sync/type/reference_many.rb
ledger_sync-1.4.0 lib/ledger_sync/type/reference_many.rb