Class: Contracts::Engine::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts/engine/target.rb

Overview

Represents class in question

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Target

Returns a new instance of Target.

Parameters:

  • target (Class)
    • class in question



6
7
8
# File 'lib/contracts/engine/target.rb', line 6

def initialize(target)
  @target = target
end

Instance Method Details

#applied?Bool

Returns true if target has contracts engine already

Returns:



34
35
36
# File 'lib/contracts/engine/target.rb', line 34

def applied?
  target.respond_to?(:__contracts_engine)
end

#apply(engine_class = Base) ⇒ Object

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

Parameters:

  • engine_class (Engine::Base:Class) (defaults to: Base)
    • type of engine to

    enable (Base or Eigenclass)



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/contracts/engine/target.rb', line 17

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

#engineEngine::Base or Engine::Eigenclass

Returns contracts engine of target



41
42
43
# File 'lib/contracts/engine/target.rb', line 41

def engine
  applied? && target.__contracts_engine
end