Class: Contracts::Engine::Base

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

Overview

Contracts engine

Direct Known Subclasses

Eigenclass

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Base

Creates new instance of contracts engine

Parameters:

  • klass (Class)
    • class that owns this engine



31
32
33
# File 'lib/contracts/engine/base.rb', line 31

def initialize(klass)
  @klass = klass
end

Class Method Details

.applied?(klass) ⇒ Bool

Returns true if klass has contracts engine

Parameters:

  • klass (Class)
    • target class

Returns:



16
17
18
# File 'lib/contracts/engine/base.rb', line 16

def self.applied?(klass)
  Engine::Target.new(klass).applied?
end

.apply(klass) ⇒ Object

Enable contracts engine for klass

Parameters:

  • klass (Class)
    • target class



8
9
10
# File 'lib/contracts/engine/base.rb', line 8

def self.apply(klass)
  Engine::Target.new(klass).apply
end

.fetch_from(klass) ⇒ Engine::Base or Engine::Eigenclass

Fetches contracts engine out of klass

Parameters:

  • klass (Class)
    • target class

Returns:



24
25
26
# File 'lib/contracts/engine/base.rb', line 24

def self.fetch_from(klass)
  Engine::Target.new(klass).engine
end

Instance Method Details

#add_method_decorator(type, name, decorator) ⇒ Object

Adds method decorator

Parameters:

  • type (Or[:class_methods, :instance_methods])
    • method type

  • name (Symbol)
    • method name

  • decorator (Decorator)
    • method decorator



82
83
84
85
# File 'lib/contracts/engine/base.rb', line 82

def add_method_decorator(type, name, decorator)
  decorated_methods[type][name] ||= []
  decorated_methods[type][name] << decorator
end

#all_decoratorsArrayOf[Decorator]

Fetches all accumulated decorators (both this engine and corresponding eigenclass’ engine) It clears all accumulated decorators

Returns:



56
57
58
# File 'lib/contracts/engine/base.rb', line 56

def all_decorators
  pop_decorators + eigenclass_engine.all_decorators
end

#decorate(decorator_class, *args) ⇒ Object

Adds provided decorator to the engine It validates that decorator can be added to this engine at the moment

Parameters:

  • decorator_class (Decorator:Class)
  • args
    • arguments for decorator



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

def decorate(decorator_class, *args)
  validate!
  decorators << [decorator_class, args]
end

#decorated_methods?Bool

Returns true if there are any decorated methods

Returns:



72
73
74
75
# File 'lib/contracts/engine/base.rb', line 72

def decorated_methods?
  !decorated_methods[:class_methods].empty? ||
    !decorated_methods[:instance_methods].empty?
end

#decorated_methods_for(type, name) ⇒ ArrayOf[Decorator]

Fetches decorators of specified type for method with name

Parameters:

  • type (Or[:class_methods, :instance_methods])
    • method type

  • name (Symbol)
    • method name

Returns:



65
66
67
# File 'lib/contracts/engine/base.rb', line 65

def decorated_methods_for(type, name)
  Array(decorated_methods[type][name])
end

#nearest_decorated_ancestorEngine::Base or Engine::Eigenclass

Returns nearest ancestor’s engine that has decorated methods



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/contracts/engine/base.rb', line 90

def nearest_decorated_ancestor
  current        = klass
  current_engine = self
  ancestors      = current.ancestors[1..-1]

  while current && current_engine && !current_engine.decorated_methods?
    current        = ancestors.shift
    current_engine = Engine.fetch_from(current)
  end

  current_engine
end

#set_eigenclass_ownerObject

Sets eigenclass’ owner to klass



47
48
49
# File 'lib/contracts/engine/base.rb', line 47

def set_eigenclass_owner
  eigenclass_engine.owner_class = klass
end