This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
forked from neo4jrb/activegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neo4jrb#445 from andreasronge/query_proxy_refactor
Query proxy refactor. Fixes multithreading issue and adds scope.
- Loading branch information
Showing
9 changed files
with
247 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module Neo4j::ActiveNode | ||
module Scope | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
|
||
# Similar to ActiveRecord scope | ||
# | ||
# @example without argument | ||
# class Person | ||
# include Neo4j::ActiveNode | ||
# property :name | ||
# property :score | ||
# has_many :out, :friends, 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, 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) | ||
_scope[name.to_sym] = proc | ||
|
||
module_eval(%Q{ | ||
def #{name}(query_params=nil, _=nil, query_proxy=nil) | ||
eval_context = ScopeEvalContext.new(self, query_proxy || self.class.query_proxy) | ||
proc = self.class._scope[:"#{name}"] | ||
self.class._call_scope_context(eval_context, query_params, proc) | ||
end | ||
}, __FILE__, __LINE__) | ||
|
||
instance_eval(%Q{ | ||
def #{name}(query_params=nil, _=nil, query_proxy=nil) | ||
eval_context = ScopeEvalContext.new(self, query_proxy || self.query_proxy) | ||
proc = _scope[:"#{name}"] | ||
_call_scope_context(eval_context, query_params, proc) | ||
end | ||
}, __FILE__, __LINE__) | ||
end | ||
|
||
|
||
|
||
def _scope | ||
@_scope ||= {} | ||
end | ||
|
||
def _call_scope_context(eval_context, query_params, proc) | ||
if proc.arity == 1 | ||
eval_context.instance_exec(query_params,&proc) | ||
else | ||
eval_context.instance_exec(&proc) | ||
end | ||
end | ||
|
||
|
||
end | ||
|
||
class ScopeEvalContext | ||
def initialize(target, query_proxy) | ||
@query_proxy = query_proxy | ||
@target = target | ||
end | ||
|
||
Neo4j::ActiveNode::Query::QueryProxy::METHODS.each do |method| | ||
module_eval(%Q{ | ||
def #{method}(params={}) | ||
(@query_proxy || @target).#{method}(params) | ||
end}, __FILE__, __LINE__) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require 'spec_helper' | ||
|
||
#module Neo4j::ActiveNode::Scope | ||
|
||
describe 'Neo4j::NodeMixin::Scope' do | ||
class Person | ||
include Neo4j::ActiveNode | ||
property :name | ||
property :score | ||
property :level_num | ||
has_many :out, :friends, model_class: self | ||
end | ||
|
||
before(:all) do | ||
@a = Person.create name: 'a', score: 42, level_num: 1 | ||
@b = Person.create name: 'b', score: 42, level_num: 2 | ||
@b1 = Person.create name: 'b1', score: 42, level_num: 3 | ||
@b2 = Person.create name: 'b2', score: 42, level_num: 4 | ||
|
||
@a.friends << @b | ||
@b.friends << @b1 << @b2 | ||
end | ||
|
||
|
||
describe 'Person.scope :level, -> (num) { where(level: num)}' do | ||
before(:all) do | ||
Person.scope :level, ->(num) { where(level_num: num)} | ||
end | ||
|
||
describe 'Person.level(3)' do | ||
it 'returns person with level 3' do | ||
expect(Person.level(3).to_a).to eq([@b1]) | ||
end | ||
end | ||
end | ||
|
||
describe 'Person.scope :in_order, -> { order(level: num)}' do | ||
before(:all) do | ||
Person.scope :in_order, ->(identifier){ order("#{identifier}.level_num DESC")} | ||
end | ||
|
||
describe 'Person.in_order' do | ||
it 'returns person in order' do | ||
expect(Person.as(:people).in_order(:people).to_a).to eq([@b2,@b1,@b,@a]) | ||
end | ||
end | ||
end | ||
|
||
describe 'Person.scope :great_students, -> (identifier) { where("#{identifier}.score > 41")' do | ||
before(:all) do | ||
Person.scope :great_students, ->(identifier) { where("#{identifier}.score > 41") } | ||
end | ||
|
||
|
||
describe 'Person.top_students.to_a' do | ||
subject do | ||
Person.as(:foo).great_students(:foo).to_a | ||
end | ||
it { is_expected.to match_array([@a, @b, @b1, @b2])} | ||
end | ||
|
||
end | ||
|
||
describe 'Person.scope :top_students, -> { where(score: 42)}' do | ||
before(:all) do | ||
Person.scope :top_students, -> { where(score: 42)} | ||
end | ||
|
||
|
||
describe 'Person.top_students.to_a' do | ||
subject do | ||
Person.top_students.to_a | ||
end | ||
it { is_expected.to match_array([@a, @b, @b1, @b2])} | ||
end | ||
|
||
describe 'person.friends.top_students.to_a' do | ||
subject do | ||
@a.friends.top_students.to_a | ||
end | ||
it { is_expected.to match_array([@b])} | ||
end | ||
|
||
describe 'person.friends.friend.top_students.to_a' do | ||
subject do | ||
@a.friends.friends.top_students.to_a | ||
end | ||
it { is_expected.to match_array([@b1, @b2])} | ||
end | ||
|
||
describe 'person.top_students.friends.to_a' do | ||
subject do | ||
@a.friends.top_students.friends.to_a | ||
end | ||
it { is_expected.to match_array([@b1, @b2])} | ||
end | ||
|
||
describe 'person.top_students.top_students.to_a' do | ||
subject do | ||
Person.top_students.friends.top_students.to_a | ||
end | ||
it { is_expected.to match_array([@b, @b1, @b2])} | ||
end | ||
|
||
end | ||
#end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
describe Neo4j::ActiveNode::Scope do | ||
|
||
let(:clazz) do | ||
UniqueClass.create do | ||
include Neo4j::ActiveNode | ||
scope :active, -> do | ||
where state: 'active' | ||
end | ||
end | ||
end | ||
|
||
it 'wraps the where method with a query_proxy' do | ||
query_proxy = double(:query_proxy) | ||
expect(query_proxy).to receive(:where).with({state: 'active'}) | ||
clazz.stub(:query_proxy).and_return(query_proxy) | ||
clazz.active | ||
end | ||
end |