Sha256: afaa432cc0f533775dc66f3b4b36ec644dd603a0b607cb1840403108cb8065d3

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 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(args = {})
        attribute      = args.fetch(:attribute)
        resource_class = args.fetch(:resource_class)
        value          = args.fetch(: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

1 entries across 1 versions & 1 rubygems

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