class CabezaDeTermo::Lunfardo::Scope
Public Class Methods
after(*params, &block)
click to toggle source
# File lib/lunfardo/scope.rb, line 56 def after(*params, &block) define(:after, &block) end
before(*params, &block)
click to toggle source
# File lib/lunfardo/scope.rb, line 42 def before(*params, &block) define(:before, &block) end
block_as_parameter?()
click to toggle source
# File lib/lunfardo/scope.rb, line 28 def block_as_parameter?() @block_as_parameter end
define(method_name, &block)
click to toggle source
Defining the DSL
# File lib/lunfardo/scope.rb, line 38 def define(method_name, &block) define_method(method_name, &block) end
infere_block_as_parameter_from(block)
click to toggle source
# 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(context, outer_scope)
click to toggle source
Initializing
# File lib/lunfardo/scope.rb, line 73 def initialize(context, outer_scope) @context = context @outer_scope = outer_scope end
new_scope_class(name:, &block)
click to toggle source
# 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)
click to toggle source
# 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
perform(*params, &block)
click to toggle source
# File lib/lunfardo/scope.rb, line 46 def perform(*params, &block) define(:perform, &block) end
raise_error(message)
click to toggle source
Raising errors
# File lib/lunfardo/scope.rb, line 66 def raise_error(message) raise Lunfardo::Error.new(message) end
recursive(scope_name, scope:)
click to toggle source
# File lib/lunfardo/scope.rb, line 60 def recursive(scope_name, scope:) scopes[scope_name] = scope end
scope_name()
click to toggle source
# File lib/lunfardo/scope.rb, line 18 def scope_name() @scope_name end
scopes()
click to toggle source
# File lib/lunfardo/scope.rb, line 32 def scopes() @scopes ||= Hash[] end
set_scope_name(name)
click to toggle source
Accessing
# File lib/lunfardo/scope.rb, line 14 def set_scope_name(name) @scope_name = name.to_sym end
Public Instance Methods
_block_as_parameter?()
click to toggle source
# File lib/lunfardo/scope.rb, line 88 def _block_as_parameter?() self.class.block_as_parameter? end
_evaluate(*params, &block)
click to toggle source
# 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)
click to toggle source
Asking
# 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)
click to toggle source
Raising errors
# 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)
click to toggle source
Accessing
# File lib/lunfardo/scope.rb, line 80 def _scope_at(scope_name) _scopes.fetch(scope_name.to_sym) end
_scope_name()
click to toggle source
# File lib/lunfardo/scope.rb, line 84 def _scope_name() self.class.scope_name end
_scope_path()
click to toggle source
# File lib/lunfardo/scope.rb, line 139 def _scope_path outer.nil? ? [_scope_name] : outer._scope_path << _scope_name end
_scopes()
click to toggle source
# File lib/lunfardo/scope.rb, line 92 def _scopes() self.class.scopes end
context()
click to toggle source
Evaluating
# File lib/lunfardo/scope.rb, line 104 def context() @context end
method_missing(method_name, *params, &block)
click to toggle source
# 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
outer(n = 1)
click to toggle source
# 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