Module: ActiveGraph::Node::Scope::ClassMethods
- Defined in:
- lib/active_graph/node/scope.rb
Instance Method Summary collapse
- #_call_scope_context(eval_context, query_params, proc) ⇒ Object
- #all(new_var = nil) ⇒ Object
-
#current_scope ⇒ Object
:nodoc:.
-
#current_scope=(scope) ⇒ Object
:nodoc:.
-
#full_scopes ⇒ Hash
Of scopes available to this model.
-
#has_scope?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#scope(name, proc) ⇒ Object
Similar to ActiveRecord scope.
-
#scope?(name) ⇒ Boolean
True if model has access to scope with this name.
-
#scopes ⇒ Hash
Of scopes assigned to this model.
Instance Method Details
#_call_scope_context(eval_context, query_params, proc) ⇒ Object
79 80 81 |
# File 'lib/active_graph/node/scope.rb', line 79 def _call_scope_context(eval_context, query_params, proc) eval_context.instance_exec(*query_params.fill(nil, query_params.length..proc.arity - 1), &proc) end |
#all(new_var = nil) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/active_graph/node/scope.rb', line 91 def all(new_var = nil) var = new_var || (current_scope ? current_scope.node_identity : :n) if current_scope current_scope.new_link(var) else self.as(var) end end |
#current_scope ⇒ Object
:nodoc:
83 84 85 |
# File 'lib/active_graph/node/scope.rb', line 83 def current_scope #:nodoc: ScopeRegistry.value_for(:current_scope, base_class.to_s) end |
#current_scope=(scope) ⇒ Object
:nodoc:
87 88 89 |
# File 'lib/active_graph/node/scope.rb', line 87 def current_scope=(scope) #:nodoc: ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope) end |
#full_scopes ⇒ Hash
Returns of scopes available to this model. Keys are scope name, value is scope callable.
73 74 75 76 77 |
# File 'lib/active_graph/node/scope.rb', line 73 def full_scopes self.ancestors.find_all { |a| a.respond_to?(:scopes) }.reverse.inject({}) do |scopes, a| scopes.merge(a.scopes) end end |
#has_scope?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName
55 56 57 58 59 |
# File 'lib/active_graph/node/scope.rb', line 55 def has_scope?(name) ActiveSupport::Deprecation.warn 'has_scope? is deprecated and may be removed from future releases, use scope? instead.', caller scope?(name) end |
#scope(name, proc) ⇒ Object
Similar to ActiveRecord scope
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_graph/node/scope.rb', line 37 def scope(name, proc) scopes[name.to_sym] = proc klass = class << self; self; end klass.instance_eval do define_method(name) do |*query_params| eval_context = ScopeEvalContext.new(self, current_scope || self.query_proxy) proc = full_scopes[name.to_sym] _call_scope_context(eval_context, query_params, proc) end end define_method(name) do |*query_params| as(:n).public_send(name, *query_params) end end |
#scope?(name) ⇒ Boolean
Returns true if model has access to scope with this name.
63 64 65 |
# File 'lib/active_graph/node/scope.rb', line 63 def scope?(name) full_scopes.key?(name.to_sym) end |
#scopes ⇒ Hash
Returns of scopes assigned to this model. Keys are scope name, value is scope callable.
68 69 70 |
# File 'lib/active_graph/node/scope.rb', line 68 def scopes @scopes ||= {} end |