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


33
34
35
# File 'lib/contracts/engine/base.rb', line 33

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:


18
19
20
# File 'lib/contracts/engine/base.rb', line 18

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

.apply(klass) ⇒ Object

Enable contracts engine for klass

Parameters:

  • klass (Class)
    • target class


10
11
12
# File 'lib/contracts/engine/base.rb', line 10

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:


26
27
28
# File 'lib/contracts/engine/base.rb', line 26

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


84
85
86
87
# File 'lib/contracts/engine/base.rb', line 84

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:


58
59
60
# File 'lib/contracts/engine/base.rb', line 58

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


43
44
45
46
# File 'lib/contracts/engine/base.rb', line 43

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

#decorated_methods?Bool

Returns true if there are any decorated methods

Returns:


74
75
76
77
# File 'lib/contracts/engine/base.rb', line 74

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:


67
68
69
# File 'lib/contracts/engine/base.rb', line 67

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


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

def nearest_decorated_ancestor
  current = klass
  current_engine = self
  ancestors = current.ancestors[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


49
50
51
# File 'lib/contracts/engine/base.rb', line 49

def set_eigenclass_owner
  eigenclass_engine.owner_class = klass
end