Sha256: 334cb95a14284c66e580aa3946ee8961c2d5a4a2d9115f7ef7efc9889ae4fa68

Contents?: true

Size: 880 Bytes

Versions: 4

Compression:

Stored size: 880 Bytes

Contents

# frozen_string_literal: true

require_relative "castable/exceptions"

module ConvenientService
  module Support
    ##
    # TODO: Specs for options.
    #
    module Castable
      include Support::Concern

      instance_methods do
        private

        def cast(...)
          self.class.cast(...)
        end

        def cast!(...)
          self.class.cast!(...)
        end
      end

      class_methods do
        ##
        # @internal
        #   TODO: `include Support::Castable` also extends `Support::AbstractMethod`. Is there a way to avoid such behavior?
        #
        include Support::AbstractMethod

        abstract_method :cast

        def cast!(other, **options)
          casted = cast(other, **options)

          raise Exceptions::FailedToCast.new(other: other, klass: self) unless casted

          casted
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.16.0 lib/convenient_service/support/castable.rb
convenient_service-0.15.0 lib/convenient_service/support/castable.rb
convenient_service-0.14.0 lib/convenient_service/support/castable.rb
convenient_service-0.13.0 lib/convenient_service/support/castable.rb