Sha256: 5d612838f7be1809b195d1d02373d2290333b3d1031ca4301e4341de5ad0db61

Contents?: true

Size: 1.79 KB

Versions: 15

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Contracts
  module Engine
    # Represents class in question
    class Target
      # Creates new instance of Target
      #
      # @param [Class] target - class in question
      def initialize(target)
        @target = target
      end

      # Enable contracts engine for target
      # - it is no-op if contracts engine is already enabled
      # - it automatically enables contracts engine for its eigenclass
      # - it sets owner class to target for its eigenclass
      #
      # @param [Engine::Base:Class] engine_class - type of engine to
      #   enable (Base or Eigenclass)
      def apply(engine_class = Base)
        return if applied?

        apply_to_eigenclass

        eigenclass.class_eval do
          define_method(:__contracts_engine) do
            @__contracts_engine ||= engine_class.new(self)
          end
        end

        engine.set_eigenclass_owner
      end

      # Returns true if target has contracts engine already
      #
      # @return [Bool]
      def applied?
        target.respond_to?(:__contracts_engine)
      end

      # Returns contracts engine of target
      #
      # @return [Engine::Base or Engine::Eigenclass]
      def engine
        applied? && target.__contracts_engine
      end

      private

      attr_reader :target

      def apply_to_eigenclass
        return unless meaningless_eigenclass?

        self.class.new(eigenclass).apply(Eigenclass)
        eigenclass.extend(MethodDecorators)
        # FIXME; this should detect what user uses `include Contracts` or
        # `include Contracts;;Core`
        eigenclass.send(:include, Contracts)
      end

      def eigenclass
        Support.eigenclass_of(target)
      end

      def meaningless_eigenclass?
        !Support.eigenclass?(target)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
contracts-0.17.2 lib/contracts/engine/target.rb
contracts-0.17.1 lib/contracts/engine/target.rb
entitlements-app-1.1.0 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-app-1.0.0 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-app-0.3.4 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-app-0.3.1 lib/contracts-ruby3/lib/contracts/engine/target.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/engine/target.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/engine/target.rb
entitlements-app-0.3.0 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-0.2.1 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-app-0.2.1 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-0.2.0 lib/contracts-ruby3/lib/contracts/engine/target.rb
entitlements-app-0.2.0 lib/contracts-ruby3/lib/contracts/engine/target.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/contracts-0.17/lib/contracts/engine/target.rb
contracts-0.17 lib/contracts/engine/target.rb