Sha256: c67fb9c6203e9b66f2318dcfdb108c2930e6982ae6710831dcd9f2d18760ebf0

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

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

      attr_reader :resource_class

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

      def error_message(attribute:, resource:, value:)
        if resource_class.is_a?(Array)
          "Attribute #{attribute.name} for #{resource.class.name} should be one of the following: #{resource_class.map(&:name).join(', ')}.  Given #{value.class.name}"
        else
          "Attribute #{attribute.name} for #{resource.class.name} should be a #{resource_class.name}.  Given #{value.class.name}"
        end
      end

      def type
        :reference_one
      end

      def valid_without_casting?(value:)
        return true if value.nil?
        return true if resource_classes.select { |e| value.is_a?(e) }.any?

        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

4 entries across 4 versions & 1 rubygems

Version Path
ledger_sync-1.3.4 lib/ledger_sync/type/reference_one.rb
ledger_sync-1.3.3 lib/ledger_sync/type/reference_one.rb
ledger_sync-1.3.2 lib/ledger_sync/type/reference_one.rb
ledger_sync-1.3.1 lib/ledger_sync/type/reference_one.rb