Class: CabezaDeTermo::Lunfardo::Scope
- Inherits:
-
Object
- Object
- CabezaDeTermo::Lunfardo::Scope
show all
- Defined in:
- lib/lunfardo/scope.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context, outer_scope) ⇒ Scope
73
74
75
76
|
# File 'lib/lunfardo/scope.rb', line 73
def initialize(context, outer_scope)
@context = context
@outer_scope = outer_scope
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *params, &block) ⇒ Object
126
127
128
129
130
|
# File 'lib/lunfardo/scope.rb', line 126
def method_missing(method_name, *params, &block)
return _raise_missing_scope_error(method_name) unless _includes_scope?(method_name)
_scope_at(method_name).new(@context, self)._evaluate(*params, &block)
end
|
Class Method Details
.after(*params, &block) ⇒ Object
56
57
58
|
# File 'lib/lunfardo/scope.rb', line 56
def after(*params, &block)
define(:after, &block)
end
|
.before(*params, &block) ⇒ Object
42
43
44
|
# File 'lib/lunfardo/scope.rb', line 42
def before(*params, &block)
define(:before, &block)
end
|
.block_as_parameter? ⇒ Boolean
28
29
30
|
# File 'lib/lunfardo/scope.rb', line 28
def block_as_parameter?()
@block_as_parameter
end
|
.define(method_name, &block) ⇒ Object
38
39
40
|
# File 'lib/lunfardo/scope.rb', line 38
def define(method_name, &block)
define_method(method_name, &block)
end
|
.infere_block_as_parameter_from(block) ⇒ Object
22
23
24
25
26
|
# File 'lib/lunfardo/scope.rb', line 22
def infere_block_as_parameter_from(block)
@block_as_parameter =
!block.nil? && !block.parameters.empty? &&
block.parameters.last[0] == :block
end
|
.new_scope_class(name:, &block) ⇒ Object
5
6
7
8
9
10
|
# File 'lib/lunfardo/scope.rb', line 5
def new_scope_class(name:, &block)
Class.new(self) do
set_scope_name name
infere_block_as_parameter_from(block)
end
end
|
.on(method_name, &block) ⇒ Object
50
51
52
53
54
|
# File 'lib/lunfardo/scope.rb', line 50
def on(method_name, &block)
scopes[method_name] = Scope.new_scope_class(name: method_name, &block)
scopes[method_name].instance_exec(&block)
end
|
46
47
48
|
# File 'lib/lunfardo/scope.rb', line 46
def perform(*params, &block)
define(:perform, &block)
end
|
.raise_error(message) ⇒ Object
66
67
68
|
# File 'lib/lunfardo/scope.rb', line 66
def raise_error(message)
raise Lunfardo::Error.new(message)
end
|
.recursive(scope_name, scope:) ⇒ Object
60
61
62
|
# File 'lib/lunfardo/scope.rb', line 60
def recursive(scope_name, scope:)
scopes[scope_name] = scope
end
|
.scope_name ⇒ Object
18
19
20
|
# File 'lib/lunfardo/scope.rb', line 18
def scope_name()
@scope_name
end
|
.scopes ⇒ Object
32
33
34
|
# File 'lib/lunfardo/scope.rb', line 32
def scopes()
@scopes ||= Hash[]
end
|
.set_scope_name(name) ⇒ Object
14
15
16
|
# File 'lib/lunfardo/scope.rb', line 14
def set_scope_name(name)
@scope_name = name.to_sym
end
|
Instance Method Details
#_block_as_parameter? ⇒ Boolean
88
89
90
|
# File 'lib/lunfardo/scope.rb', line 88
def _block_as_parameter?()
self.class.block_as_parameter?
end
|
#_evaluate(*params, &block) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/lunfardo/scope.rb', line 114
def _evaluate(*params, &block)
result = send(:before, *params, &block) if respond_to?(:before)
result = send(:perform, *params, &block) if respond_to?(:perform)
result = instance_exec(*params, &block) unless block.nil? || _block_as_parameter?
result = send(:after, *params, &block) if respond_to?(:after)
result
end
|
#_includes_scope?(scope_name) ⇒ Boolean
98
99
100
|
# File 'lib/lunfardo/scope.rb', line 98
def _includes_scope?(scope_name)
_scopes.key?(scope_name.to_sym)
end
|
#_raise_missing_scope_error(method_name) ⇒ Object
134
135
136
137
|
# File 'lib/lunfardo/scope.rb', line 134
def _raise_missing_scope_error(method_name)
path = (_scope_path << method_name).join('.')
self.class.raise_error("The scope '#{path}' is not defined")
end
|
#_scope_at(scope_name) ⇒ Object
80
81
82
|
# File 'lib/lunfardo/scope.rb', line 80
def _scope_at(scope_name)
_scopes.fetch(scope_name.to_sym)
end
|
#_scope_name ⇒ Object
84
85
86
|
# File 'lib/lunfardo/scope.rb', line 84
def _scope_name()
self.class.scope_name
end
|
#_scope_path ⇒ Object
139
140
141
|
# File 'lib/lunfardo/scope.rb', line 139
def _scope_path
outer.nil? ? [_scope_name] : outer._scope_path << _scope_name
end
|
#_scopes ⇒ Object
92
93
94
|
# File 'lib/lunfardo/scope.rb', line 92
def _scopes()
self.class.scopes
end
|
#context ⇒ Object
104
105
106
|
# File 'lib/lunfardo/scope.rb', line 104
def context()
@context
end
|
#outer(n = 1) ⇒ Object
108
109
110
111
112
|
# File 'lib/lunfardo/scope.rb', line 108
def outer(n = 1)
(1..n-1).inject(@outer_scope) do |outer_scope, i|
outer_scope.outer
end
end
|