Sha256: 38dc7e33fb02a883bd3f8b6d96739640891088556cc6513d3920c1390d6cc72f

Contents?: true

Size: 872 Bytes

Versions: 15

Compression:

Stored size: 872 Bytes

Contents

# frozen_string_literal: true

require_relative "castable/errors"

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 Errors::FailedToCast.new(other: other, klass: self) unless casted

          casted
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/support/castable.rb
convenient_service-0.11.0 lib/convenient_service/support/castable.rb
convenient_service-0.10.1 lib/convenient_service/support/castable.rb
convenient_service-0.10.0 lib/convenient_service/support/castable.rb
convenient_service-0.9.0 lib/convenient_service/support/castable.rb
convenient_service-0.8.0 lib/convenient_service/support/castable.rb
convenient_service-0.7.0 lib/convenient_service/support/castable.rb
convenient_service-0.6.0 lib/convenient_service/support/castable.rb
convenient_service-0.5.0 lib/convenient_service/support/castable.rb
convenient_service-0.4.0 lib/convenient_service/support/castable.rb
convenient_service-0.3.1 lib/convenient_service/support/castable.rb
convenient_service-0.3.0 lib/convenient_service/support/castable.rb
convenient_service-0.2.1 lib/convenient_service/support/castable.rb
convenient_service-0.2.0 lib/convenient_service/support/castable.rb
convenient_service-0.1.0 lib/convenient_service/support/castable.rb