Sha256: 9986abcf73098abfbaae6d5dbdefdc9b8e73d642ed0540d755bbe93e91179696

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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.first.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

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.1.3 lib/ledger_sync/type/reference_one.rb