module Neo4j::ActiveNode::Scope::ClassMethods
Public Instance Methods
_call_scope_context(eval_context, query_params, proc)
click to toggle source
# File lib/neo4j/active_node/scope.rb 79 def _call_scope_context(eval_context, query_params, proc) 80 eval_context.instance_exec(*query_params.fill(nil, query_params.length..proc.arity - 1), &proc) 81 end
all(new_var = nil)
click to toggle source
# File lib/neo4j/active_node/scope.rb 91 def all(new_var = nil) 92 var = new_var || (current_scope ? current_scope.node_identity : :n) 93 if current_scope 94 current_scope.new_link(var) 95 else 96 self.as(var) 97 end 98 end
full_scopes()
click to toggle source
@return [Hash] of scopes available to this model. Keys are scope name, value is scope callable.
# File lib/neo4j/active_node/scope.rb 73 def full_scopes 74 self.ancestors.find_all { |a| a.respond_to?(:scopes) }.reverse.inject({}) do |scopes, a| 75 scopes.merge(a.scopes) 76 end 77 end
has_scope?(name)
click to toggle source
rubocop:disable Naming/PredicateName
# File lib/neo4j/active_node/scope.rb 55 def has_scope?(name) 56 ActiveSupport::Deprecation.warn 'has_scope? is deprecated and may be removed from future releases, use scope? instead.', caller 57 58 scope?(name) 59 end
scope(name, proc)
click to toggle source
Similar to ActiveRecord scope
@example without argument
class Person include Neo4j::ActiveNode 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 Neo4j::ActiveNode 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 guides.rubyonrails.org/active_record_querying.html#scopes
# File lib/neo4j/active_node/scope.rb 37 def scope(name, proc) 38 scopes[name.to_sym] = proc 39 40 klass = class << self; self; end 41 klass.instance_eval do 42 define_method(name) do |*query_params| 43 eval_context = ScopeEvalContext.new(self, current_scope || self.query_proxy) 44 proc = full_scopes[name.to_sym] 45 _call_scope_context(eval_context, query_params, proc) 46 end 47 end 48 49 define_method(name) do |*query_params| 50 as(:n).public_send(name, *query_params) 51 end 52 end
scope?(name)
click to toggle source
@return [Boolean] true if model has access to scope with this name
# File lib/neo4j/active_node/scope.rb 63 def scope?(name) 64 full_scopes.key?(name.to_sym) 65 end
scopes()
click to toggle source
@return [Hash] of scopes assigned to this model. Keys are scope name, value is scope callable.
# File lib/neo4j/active_node/scope.rb 68 def scopes 69 @scopes ||= {} 70 end