Class: Ree::Contracts::MethodDecorator
- Includes:
- Args
- Defined in:
- lib/ree/contracts/method_decorator.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
[CalledArgsValidator].
-
#contract_definition ⇒ Object
readonly
- ContractDefinition
-
definition of contract.
-
#doc ⇒ Object
readonly
- Nilor
-
rdoc.
-
#errors ⇒ Object
readonly
- Nilor[ArrayOf]
-
list of thrown errors.
-
#is_class_method ⇒ Object
readonly
[Bool].
-
#method_name ⇒ Object
readonly
- Symbol
-
original method name.
-
#method_parameters ⇒ Object
readonly
- Array
-
list of original method parameters.
-
#return_validator ⇒ Object
readonly
- BaseValidator
-
validator for return value.
-
#target ⇒ Object
readonly
- Class
-
class ot superclass of decorated method.
Class Method Summary collapse
- .add_decorator(decorator) ⇒ Object
- .decorator_id(target, method_name, is_class_method) ⇒ Object
- .decorators ⇒ Object
- .get_decorator(id) ⇒ Object
Instance Method Summary collapse
-
#alias_target ⇒ Object
Target class to be used for alias method definition.
- #call ⇒ Object
- #execute_on(target, args, kwargs, &blk) ⇒ Object
-
#id ⇒ Object
Unique ID of this Method Decorator.
-
#initialize(method_name, is_class_method, target) ⇒ MethodDecorator
constructor
A new instance of MethodDecorator.
-
#method_alias ⇒ Object
Alias name for original method.
Methods included from Args
#check_arg, #check_arg_any, #check_arg_array_of, #check_bool, #not_nil
Constructor Details
#initialize(method_name, is_class_method, target) ⇒ MethodDecorator
Returns a new instance of MethodDecorator.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ree/contracts/method_decorator.rb', line 35 def initialize(method_name, is_class_method, target) check_arg(method_name, :method_name, Symbol) check_bool(is_class_method, :is_class_method) check_arg_any(target, :target, [Class, Module]) engine = Engine.fetch_for(target) @method_name = method_name @is_class_method = is_class_method @target = target @contract_definition = engine.fetch_contract @errors = engine.fetch_errors @doc = engine.fetch_doc end |
Instance Attribute Details
#args ⇒ Object (readonly)
- CalledArgsValidator
32 33 34 |
# File 'lib/ree/contracts/method_decorator.rb', line 32 def args @args end |
#contract_definition ⇒ Object (readonly)
- ContractDefinition
-
definition of contract
29 30 31 |
# File 'lib/ree/contracts/method_decorator.rb', line 29 def contract_definition @contract_definition end |
#doc ⇒ Object (readonly)
- Nilor
-
rdoc
31 32 33 |
# File 'lib/ree/contracts/method_decorator.rb', line 31 def doc @doc end |
#errors ⇒ Object (readonly)
- Nilor[ArrayOf]
-
list of thrown errors
30 31 32 |
# File 'lib/ree/contracts/method_decorator.rb', line 30 def errors @errors end |
#is_class_method ⇒ Object (readonly)
- Bool
28 29 30 |
# File 'lib/ree/contracts/method_decorator.rb', line 28 def is_class_method @is_class_method end |
#method_name ⇒ Object (readonly)
- Symbol
-
original method name
26 27 28 |
# File 'lib/ree/contracts/method_decorator.rb', line 26 def method_name @method_name end |
#method_parameters ⇒ Object (readonly)
- Array
-
list of original method parameters
27 28 29 |
# File 'lib/ree/contracts/method_decorator.rb', line 27 def method_parameters @method_parameters end |
#return_validator ⇒ Object (readonly)
- BaseValidator
-
validator for return value
33 34 35 |
# File 'lib/ree/contracts/method_decorator.rb', line 33 def return_validator @return_validator end |
#target ⇒ Object (readonly)
- Class
-
class ot superclass of decorated method
25 26 27 |
# File 'lib/ree/contracts/method_decorator.rb', line 25 def target @target end |
Class Method Details
.add_decorator(decorator) ⇒ Object
10 11 12 |
# File 'lib/ree/contracts/method_decorator.rb', line 10 def add_decorator(decorator) decorators[decorator.id] = decorator end |
.decorator_id(target, method_name, is_class_method) ⇒ Object
6 7 8 |
# File 'lib/ree/contracts/method_decorator.rb', line 6 def decorator_id(target, method_name, is_class_method) "#{target.object_id}#{target.name}#{method_name}#{is_class_method}" end |
.decorators ⇒ Object
18 19 20 |
# File 'lib/ree/contracts/method_decorator.rb', line 18 def decorators @decorators ||= {} end |
.get_decorator(id) ⇒ Object
14 15 16 |
# File 'lib/ree/contracts/method_decorator.rb', line 14 def get_decorator(id) decorators[id] end |
Instance Method Details
#alias_target ⇒ Object
Target class to be used for alias method definition
97 98 99 100 101 102 |
# File 'lib/ree/contracts/method_decorator.rb', line 97 def alias_target @alias_target ||= begin return Utils.eigenclass_of(target) if is_class_method target end end |
#call ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ree/contracts/method_decorator.rb', line 50 def call return if Ree::Contracts.no_contracts? return unless contract_definition self.class.add_decorator(self) @method_parameters = alias_target .instance_method(method_name) .parameters .freeze @args = CalledArgsValidator.new( contract_definition, method_parameters, printed_name ) @return_validator = Validators.fetch_for(contract_definition.return_contract) make_alias make_definition end |
#execute_on(target, args, kwargs, &blk) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ree/contracts/method_decorator.rb', line 73 def execute_on(target, args, kwargs, &blk) @args.call(args, kwargs, blk) result = target.send(method_alias, *args, **kwargs, &blk) if !return_validator.call(result) raise ReturnContractError, "Invalid return value for #{printed_name}\n #{ return_validator.(result, 'returns', 0).strip }" end result end |
#id ⇒ Object
Unique ID of this Method Decorator
87 88 89 |
# File 'lib/ree/contracts/method_decorator.rb', line 87 def id @id ||= self.class.decorator_id(target, method_name, is_class_method) end |
#method_alias ⇒ Object
Alias name for original method
92 93 94 |
# File 'lib/ree/contracts/method_decorator.rb', line 92 def method_alias @method_alias ||= :"__original_#{method_name}_#{SecureRandom.hex}" end |