Class: Contracts::Engine::Base
- Inherits:
-
Object
- Object
- Contracts::Engine::Base
- Defined in:
- lib/contracts/engine/base.rb
Overview
Contracts engine
Direct Known Subclasses
Class Method Summary collapse
-
.applied?(klass) ⇒ Bool
Returns true if klass has contracts engine.
-
.apply(klass) ⇒ Object
Enable contracts engine for klass.
-
.fetch_from(klass) ⇒ Engine::Base or Engine::Eigenclass
Fetches contracts engine out of klass.
Instance Method Summary collapse
-
#add_method_decorator(type, name, decorator) ⇒ Object
Adds method decorator.
-
#all_decorators ⇒ ArrayOf[Decorator]
Fetches all accumulated decorators (both this engine and corresponding eigenclass’ engine) It clears all accumulated decorators.
-
#decorate(decorator_class, *args) ⇒ Object
Adds provided decorator to the engine It validates that decorator can be added to this engine at the moment.
-
#decorated_methods? ⇒ Bool
Returns true if there are any decorated methods.
-
#decorated_methods_for(type, name) ⇒ ArrayOf[Decorator]
Fetches decorators of specified type for method with name.
-
#initialize(klass) ⇒ Base
constructor
Creates new instance of contracts engine.
-
#nearest_decorated_ancestor ⇒ Engine::Base or Engine::Eigenclass
Returns nearest ancestor’s engine that has decorated methods.
-
#set_eigenclass_owner ⇒ Object
Sets eigenclass’ owner to klass.
Constructor Details
#initialize(klass) ⇒ Base
Creates new instance of contracts 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
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
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
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
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_decorators ⇒ ArrayOf[Decorator]
Fetches all accumulated decorators (both this engine and corresponding eigenclass’ engine) It clears all accumulated decorators
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
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
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
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_ancestor ⇒ Engine::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_owner ⇒ Object
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 |