active_node 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_node/graph/delegation.rb +35 -0
- data/lib/active_node/graph.rb +41 -3
- data/lib/active_node/persistence.rb +1 -1
- data/lib/active_node/reflection.rb +3 -7
- data/lib/active_node/version.rb +1 -1
- data/lib/active_node.rb +1 -0
- data/spec/functional/graph_spec.rb +12 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3c0d2fe8c063ac29c20b91a568919d9723d5b3e
|
4
|
+
data.tar.gz: 11522535a767b8709c00420e83e9e2f2340f9a65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81912f84e1e63e9fa220b0b67c07b15e58a30af7f8780e34cf72d68552e0a3347e7f6d6a2bcf59cd34d244f43afdd4b43ef2b2339b7fcefccdb1603bc77ebe33
|
7
|
+
data.tar.gz: d38f3caad3486c0504d1aa51b40ec1433dd11a5b135ad0c3fca5c7c787b8219e06e3aa0585d345bbaa42bfe192d5d742f3e728c995cb4154e9b328ca0ff73eee
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module ActiveNode
|
5
|
+
module Delegation # :nodoc:
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
# This module creates compiled delegation methods dynamically at runtime, which makes
|
9
|
+
# subsequent calls to that method faster by avoiding method_missing. The delegations
|
10
|
+
# may vary depending on the klass of a relation, so we create a subclass of Relation
|
11
|
+
# for each different klass, and the delegations are compiled into that subclass only.
|
12
|
+
|
13
|
+
BLACKLISTED_ARRAY_METHODS = [
|
14
|
+
:compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
|
15
|
+
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
|
16
|
+
:keep_if, :pop, :shift, :delete_at, :compact
|
17
|
+
].to_set # :nodoc:
|
18
|
+
|
19
|
+
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, to: :to_a
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def array_delegable?(method)
|
24
|
+
Array.method_defined?(method) && BLACKLISTED_ARRAY_METHODS.exclude?(method)
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(method, *args, &block)
|
28
|
+
if array_delegable?(method)
|
29
|
+
to_a.public_send(method, *args, &block)
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/active_node/graph.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
module ActiveNode
|
2
2
|
class Graph
|
3
3
|
include Neography::Rest::Helpers
|
4
|
-
include FinderMethods, QueryMethods
|
4
|
+
include FinderMethods, QueryMethods, Delegation
|
5
5
|
|
6
6
|
attr_reader :reflections, :matches, :klass, :loaded
|
7
7
|
alias :loaded? :loaded
|
8
8
|
|
9
|
-
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, to: :to_a
|
10
|
-
|
11
9
|
def initialize klass, *includes
|
12
10
|
@klass = klass if klass < ActiveNode::Base
|
13
11
|
@matches = []
|
@@ -57,6 +55,46 @@ module ActiveNode
|
|
57
55
|
@records
|
58
56
|
end
|
59
57
|
|
58
|
+
def as_json(options = nil) #:nodoc:
|
59
|
+
to_a.as_json(options)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns size of the records.
|
63
|
+
def size
|
64
|
+
loaded? ? @records.length : count
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns true if there are no records.
|
68
|
+
def empty?
|
69
|
+
return @records.empty? if loaded?
|
70
|
+
|
71
|
+
if limit_value == 0
|
72
|
+
true
|
73
|
+
else
|
74
|
+
# FIXME: This count is not compatible with #select('authors.*') or other select narrows
|
75
|
+
c = count
|
76
|
+
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns true if there are any records.
|
81
|
+
def any?
|
82
|
+
if block_given?
|
83
|
+
to_a.any? { |*block_args| yield(*block_args) }
|
84
|
+
else
|
85
|
+
!empty?
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns true if there is more than one record.
|
90
|
+
def many?
|
91
|
+
if block_given?
|
92
|
+
to_a.many? { |*block_args| yield(*block_args) }
|
93
|
+
else
|
94
|
+
limit_value ? to_a.many? : size > 1
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
60
98
|
# Compares two relations for equality.
|
61
99
|
def ==(other)
|
62
100
|
case other
|
@@ -222,13 +222,9 @@ module ActiveNode
|
|
222
222
|
|
223
223
|
private
|
224
224
|
def derive_class_name
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
class_name = name.to_s
|
229
|
-
class_name = class_name.singularize if collection?
|
230
|
-
end
|
231
|
-
model.name.sub(/[^:]*$/, class_name.camelize)
|
225
|
+
class_name = name.to_s.camelize
|
226
|
+
class_name = class_name.singularize if collection?
|
227
|
+
model.name.sub(/[^:]*$/, class_name)
|
232
228
|
end
|
233
229
|
end
|
234
230
|
|
data/lib/active_node/version.rb
CHANGED
data/lib/active_node.rb
CHANGED
@@ -32,4 +32,16 @@ describe ActiveNode::Graph do
|
|
32
32
|
person.children
|
33
33
|
end
|
34
34
|
end
|
35
|
+
|
36
|
+
describe '#empty?' do
|
37
|
+
it 'should return true' do
|
38
|
+
Person.all.should be_empty
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#detect' do
|
43
|
+
it 'should return nil' do
|
44
|
+
Person.all.detect{|p| true}.should be_nil
|
45
|
+
end
|
46
|
+
end
|
35
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_node
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Heinrich Klobuczek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_attr
|
@@ -168,6 +168,7 @@ files:
|
|
168
168
|
- lib/active_node/dirty.rb
|
169
169
|
- lib/active_node/errors.rb
|
170
170
|
- lib/active_node/graph.rb
|
171
|
+
- lib/active_node/graph/delegation.rb
|
171
172
|
- lib/active_node/graph/finder_methods.rb
|
172
173
|
- lib/active_node/graph/query_methods.rb
|
173
174
|
- lib/active_node/neo.rb
|