Module: ActiveGraph::Node::Scope::ClassMethods

Defined in:
lib/active_graph/node/scope.rb

Instance Method Summary collapse

Instance Method Details

#_call_scope_context(eval_context, *query_params, **kwargs, &proc) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/active_graph/node/scope.rb', line 81

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

#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

#full_scopesHash

Returns of scopes available to this model. Keys are scope name, value is scope callable.

Returns:

  • (Hash)

    of scopes available to this model. Keys are scope name, value is scope callable.



75
76
77
78
79
# File 'lib/active_graph/node/scope.rb', line 75

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

Returns:



57
58
59
60
61
# File 'lib/active_graph/node/scope.rb', line 57

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

Examples:

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

Argument for scopes

Person.scope :level, ->(num) { where(level_num: num)}

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 Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_graph/node/scope.rb', line 39

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

#scope?(name) ⇒ Boolean

Returns true if model has access to scope with this name.

Returns:

  • (Boolean)

    true if model has access to scope with this name



65
66
67
# File 'lib/active_graph/node/scope.rb', line 65

def scope?(name)
  full_scopes.key?(name.to_sym)
end

#scopesHash

Returns of scopes assigned to this model. Keys are scope name, value is scope callable.

Returns:

  • (Hash)

    of scopes assigned to this model. Keys are scope name, value is scope callable.



70
71
72
# File 'lib/active_graph/node/scope.rb', line 70

def scopes
  @scopes ||= {}
end