-
Notifications
You must be signed in to change notification settings - Fork 82
Refactor to contracts engine #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
egonSchiele
merged 13 commits into
egonSchiele:master
from
waterlink:non_intrusive_contracts_v2
May 10, 2015
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eed57a9
Moved #decorate, #fetch_decorators, #pop_decorators and #decorated_me…
waterlink f8266d3
Move out #common_method_added to MethodHandler object
waterlink df923a1
Remove: Eigenclass, Modules
waterlink fbdb85a
Engine :: small cleanup
waterlink ba9e520
Remove Eigenclass reference
waterlink 030f57c
split engine module into multiple files and provide a facade
waterlink ea8fd8b
Provide useful documentation for engine module
waterlink 2ff320e
Update tutorial to match the fact that inclusion of Contracts::Module…
waterlink 0b2c2fd
Refactor MethodHandler
waterlink aaabb0f
Return Contract method definition for global scope
waterlink c8d12f3
Satisfy rubocop
waterlink 054aff9
Add documentation for Engine::Base#decorated_methods_for
waterlink ea19e3b
Support for different rubies
waterlink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| module Contracts | ||
| class EngineTarget | ||
| def initialize(target) | ||
| @target = target | ||
| end | ||
|
|
||
| def apply(engine_class = Engine) | ||
| return if applied? | ||
|
|
||
| apply_to_eigenclass | ||
|
|
||
| target.class_eval do | ||
| define_singleton_method(:__contracts_engine) do | ||
| @__contracts_engine ||= engine_class.new(self) | ||
| end | ||
| end | ||
|
|
||
| engine.set_eigenclass_owner | ||
| end | ||
|
|
||
| def applied? | ||
| target.respond_to?(:__contracts_engine) | ||
| end | ||
|
|
||
| def engine | ||
| applied? && target.__contracts_engine | ||
| end | ||
|
|
||
| private | ||
| attr_reader :target | ||
|
|
||
| def apply_to_eigenclass | ||
| return unless has_meaningless_eigenclass? | ||
|
|
||
| EngineTarget.new(eigenclass).apply(EigenclassEngine) | ||
| eigenclass.extend(MethodDecorators) | ||
| eigenclass.send(:include, Contracts) | ||
| end | ||
|
|
||
| def eigenclass | ||
| Support.eigenclass_of(target) | ||
| end | ||
|
|
||
| def has_meaningless_eigenclass? | ||
| return true if target.class == Module | ||
| return false if target < Module | ||
| !Support.eigenclass?(target) | ||
| end | ||
| end | ||
|
|
||
| class Engine | ||
| def self.apply(klass) | ||
| EngineTarget.new(klass).apply | ||
| end | ||
|
|
||
| def self.applied?(klass) | ||
| EngineTarget.new(klass).applied? | ||
| end | ||
|
|
||
| def self.fetch_from(klass) | ||
| EngineTarget.new(klass).engine | ||
| end | ||
|
|
||
| def initialize(target) | ||
| @target = target | ||
| end | ||
|
|
||
| def decorate(klass, *args) | ||
| validate! | ||
| decorators << [klass, args] | ||
| end | ||
|
|
||
| def validate! | ||
| end | ||
|
|
||
| def set_eigenclass_owner | ||
| Engine.fetch_from(eigenclass).owner_class = target | ||
| end | ||
|
|
||
| def all_decorators | ||
| pop_decorators + eigenclass_engine.all_decorators | ||
| end | ||
|
|
||
| def pop_decorators | ||
| decorators.tap { clear_decorators } | ||
| end | ||
|
|
||
| def decorated_methods | ||
| @_decorated_methods ||= { :class_methods => {}, :instance_methods => {} } | ||
| end | ||
|
|
||
| def has_decorated_methods? | ||
| !decorated_methods[:class_methods].empty? || | ||
| !decorated_methods[:instance_methods].empty? | ||
| end | ||
|
|
||
| def add_method_decorator(type, name, decorator) | ||
| decorated_methods[type][name] ||= [] | ||
| decorated_methods[type][name] << decorator | ||
| end | ||
|
|
||
| private | ||
| attr_reader :target | ||
|
|
||
| def eigenclass | ||
| Support.eigenclass_of(target) | ||
| end | ||
|
|
||
| def eigenclass_engine | ||
| Engine.fetch_from(eigenclass) | ||
| end | ||
|
|
||
| def decorators | ||
| @_decorators ||= [] | ||
| end | ||
|
|
||
| def clear_decorators | ||
| @_decorators = [] | ||
| end | ||
| end | ||
|
|
||
| class EigenclassEngine < Engine | ||
| attr_accessor :owner_class | ||
|
|
||
| def validate! | ||
| fail Contracts::ContractsNotIncluded unless has_owner? | ||
| end | ||
|
|
||
| def set_eigenclass_owner | ||
| end | ||
|
|
||
| def all_decorators | ||
| pop_decorators | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def has_owner? | ||
| !!owner_class | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just
eigenclass_enginehere