Sha256: d532a3e6b4e5afc48bc6c3eeaad6d102cb6d6b729495727288e83e7eee450832

Contents?: true

Size: 900 Bytes

Versions: 4

Compression:

Stored size: 900 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)

          ::ConvenientService.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.19.1 lib/convenient_service/support/castable.rb
convenient_service-0.19.0 lib/convenient_service/support/castable.rb
convenient_service-0.18.0 lib/convenient_service/support/castable.rb
convenient_service-0.17.0 lib/convenient_service/support/castable.rb