Class: Contracts::Engine::Base
- Inherits:
-
Object
- Object
- Contracts::Engine::Base
- Defined in:
- lib/contracts-ruby2/lib/contracts/engine/base.rb,
lib/contracts-ruby3/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
31 32 33 |
# File 'lib/contracts-ruby2/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
16 17 18 |
# File 'lib/contracts-ruby2/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
8 9 10 |
# File 'lib/contracts-ruby2/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
24 25 26 |
# File 'lib/contracts-ruby2/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
82 83 84 85 |
# File 'lib/contracts-ruby2/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_decorators ⇒ ArrayOf[Decorator]
Fetches all accumulated decorators (both this engine and corresponding eigenclass’ engine) It clears all accumulated decorators
56 57 58 |
# File 'lib/contracts-ruby2/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
41 42 43 44 |
# File 'lib/contracts-ruby2/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
72 73 74 75 |
# File 'lib/contracts-ruby2/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
65 66 67 |
# File 'lib/contracts-ruby2/lib/contracts/engine/base.rb', line 65 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
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/contracts-ruby2/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_owner ⇒ Object
Sets eigenclass’ owner to klass
47 48 49 |
# File 'lib/contracts-ruby2/lib/contracts/engine/base.rb', line 47 def set_eigenclass_owner eigenclass_engine.owner_class = klass end |