-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathscope.rb
137 lines (120 loc) · 4.34 KB
/
scope.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module ActiveGraph::Node
module Scope
extend ActiveSupport::Concern
included do
thread_mattr_accessor :current_scope
end
module ClassMethods
# Similar to ActiveRecord scope
#
# @example without argument
# class Person
# include ActiveGraph::Node
# property :name
# property :score
# has_many :out, :friends, type: :has_friend, model_class: self
# scope :top_students, -> { where(score: 42)}") }
# end
# Person.top_students.to_a
# a_person.friends.top_students.to_a
# a_person.friends.friends.top_students.to_a
# a_person.friends.top_students.friends.to_a
#
# @example Argument for scopes
# Person.scope :level, ->(num) { where(level_num: num)}
#
# @example Argument as a cypher identifier
# class Person
# include ActiveGraph::Node
# property :name
# property :score
# has_many :out, :friends, type: :has_friend, model_class: self
# scope :great_students, ->(identifier) { where("#{identifier}.score > 41") }
# end
# Person.as(:all_people).great_students(:all_people).to_a
#
# @see http://guides.rubyonrails.org/active_record_querying.html#scopes
def scope(name, proc)
scopes[name.to_sym] = proc
klass = class << self; self; end
klass.instance_eval do
define_method(name) do |*query_params, **kwargs|
eval_context = ScopeEvalContext.new(self, current_scope || self.query_proxy)
proc = full_scopes[name.to_sym]
_call_scope_context(eval_context, *query_params, **kwargs, &proc)
end
end
define_method(name) do |*query_params, **kwargs|
as(:n).public_send(name, *query_params, **kwargs)
end
end
# rubocop:disable Naming/PredicateName
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
# rubocop:enable Naming/PredicateName
# @return [Boolean] true if model has access to scope with this name
def scope?(name)
full_scopes.key?(name.to_sym)
end
# @return [Hash] of scopes assigned to this model. Keys are scope name, value is scope callable.
def scopes
@scopes ||= {}
end
# @return [Hash] of scopes available to this model. Keys are scope name, value is scope callable.
def full_scopes
self.ancestors.find_all { |a| a.respond_to?(:scopes) }.reverse.inject({}) do |scopes, a|
scopes.merge(a.scopes)
end
end
def _call_scope_context(eval_context, *query_params, **kwargs, &proc)
last_vararg_index = proc.arity - (kwargs.empty? ? 1 : 2)
query_params.fill(nil, query_params.length..last_vararg_index)
if RUBY_VERSION < '3' && kwargs.empty?
eval_context.instance_exec(*query_params, &proc)
else
eval_context.instance_exec(*query_params, **kwargs, &proc)
end
end
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
end
class ScopeEvalContext
def initialize(target, query_proxy)
@query_proxy = query_proxy
@target = target
end
def identity
query_proxy_or_target.identity
end
ActiveGraph::Node::Query::QueryProxy::METHODS.each do |method|
define_method(method) do |*args|
@target.all.scoping do
query_proxy_or_target.public_send(method, *args)
end
end
end
# method_missing is not delegated to super class but to aggregated class
# rubocop:disable Style/MethodMissingSuper
def method_missing(name, *params, **kwargs, &block)
if RUBY_VERSION < '3' && kwargs.empty?
query_proxy_or_target.public_send(name, *params, &block)
else
query_proxy_or_target.public_send(name, *params, **kwargs, &block)
end
end
# rubocop:enable Style/MethodMissingSuper
private
def query_proxy_or_target
@query_proxy_or_target ||= @query_proxy || @target
end
end
end
end