Module: N4j::Entity
Defined Under Namespace
Modules: ClassMethods, Representation
Instance Method Summary
collapse
Instance Method Details
#connected_unsaved ⇒ Object
96
|
# File 'lib/n4j/entity.rb', line 96
def connected_unsaved ; [] ; end
|
#create_bundle(accumulated_bundle = []) ⇒ Object
Self and related nodes/relationships
100
101
102
103
104
105
106
107
108
|
# File 'lib/n4j/entity.rb', line 100
def create_bundle(accumulated_bundle = [])
unless accumulated_bundle.detect {|entity| entity == self || entity.path == path }
accumulated_bundle = prerequisites.inject(accumulated_bundle) {|bundle, entity| entity.create_bundle(bundle) }
self.place_in_batch = accumulated_bundle.length
accumulated_bundle << self
connected_unsaved.inject(accumulated_bundle) {|bundle, entity| entity.create_bundle(bundle) }
end
accumulated_bundle
end
|
#dependent ⇒ Object
95
|
# File 'lib/n4j/entity.rb', line 95
def dependent ; [] ; end
|
#destroy ⇒ Object
71
72
73
74
75
76
|
# File 'lib/n4j/entity.rb', line 71
def destroy
destroyable = destroy_bundle.select(&:persisted?)
commands = destroyable.collect(&:destroy_hash)
results = N4j.batch(commands)
destroy_bundle.each {|entity| entity.destroyed = true }
end
|
#destroy_bundle(accumulated_bundle = []) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/n4j/entity.rb', line 110
def destroy_bundle(accumulated_bundle = [])
unless accumulated_bundle.detect {|entity| entity == self || entity.path == path }
accumulated_bundle = dependent.inject(accumulated_bundle) {|bundle, entity| entity.destroy_bundle(bundle) }
accumulated_bundle << self
end
accumulated_bundle
end
|
#destroyed? ⇒ Boolean
67
68
69
|
# File 'lib/n4j/entity.rb', line 67
def destroyed?
@destroyed
end
|
#from_neo4j ⇒ Object
36
37
38
|
# File 'lib/n4j/entity.rb', line 36
def from_neo4j
@from_neo4j ||= HashWithIndifferentAccess.new
end
|
#from_neo4j_relative ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/n4j/entity.rb', line 51
def from_neo4j_relative
@from_neo4j_relative ||= Hash.new do |hash,key|
from_neo4j &&
from_neo4j[key].try(:sub, /http.+\/db\/data/, '')
end
end
|
#initialize(hsh) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/n4j/entity.rb', line 27
def initialize(hsh)
run_callbacks :initialize do
if N4j.neo4j_hash?(hsh)
load_neo4j_data(hsh)
load_attributes(from_neo4j)
end
end
end
|
#load_neo4j_data(hsh) ⇒ Object
44
45
46
47
48
49
|
# File 'lib/n4j/entity.rb', line 44
def load_neo4j_data(hsh)
changed_attributes.clear
if hsh self.from_neo4j = HashWithIndifferentAccess.new.merge(hsh)
end
end
|
#needs_persist? ⇒ Boolean
63
64
65
|
# File 'lib/n4j/entity.rb', line 63
def needs_persist?
!persisted? || changed?
end
|
#note_update! ⇒ Object
40
41
42
|
# File 'lib/n4j/entity.rb', line 40
def note_update!
changed_attributes.clear
end
|
#persisted? ⇒ Boolean
59
60
61
|
# File 'lib/n4j/entity.rb', line 59
def persisted?
N4j.neo4j_hash?(from_neo4j) && !destroyed?
end
|
#place_in_batch ⇒ Object
122
123
124
|
# File 'lib/n4j/entity.rb', line 122
def place_in_batch
@place_in_batch
end
|
#place_in_batch=(num) ⇒ Object
118
119
120
|
# File 'lib/n4j/entity.rb', line 118
def place_in_batch=(num)
@place_in_batch = num
end
|
#post_save ⇒ Object
97
|
# File 'lib/n4j/entity.rb', line 97
def post_save ; [] ; end
|
#prerequisites ⇒ Object
94
|
# File 'lib/n4j/entity.rb', line 94
def prerequisites ; [] ; end
|
#save ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/n4j/entity.rb', line 78
def save
run_callbacks :save do
updateable_bundle = create_bundle.select(&:persist_hash)
commands = updateable_bundle.collect {|entity| entity.persist_hash(:id => entity.place_in_batch) }
index_commands = create_bundle.select do |entity|
entity.class.ancestors.
include?(N4j::Node)
end.collect(&:index_hashes).compact.flatten
results = N4j.batch(commands + index_commands)
updateable_bundle.zip(results).each do |entity,result|
entity.load_neo4j_data(result['body'])
entity.post_save
end
end
end
|