Module: N4j::Entity

Extended by:
ActiveSupport::Concern
Included in:
GenericNode, GenericRelationship
Defined in:
lib/n4j/entity.rb

Defined Under Namespace

Modules: ClassMethods, Representation

Instance Method Summary collapse

Instance Method Details

#connected_unsavedObject



86
# File 'lib/n4j/entity.rb', line 86

def connected_unsaved ; [] ; end

#create_bundle(accumulated_bundle = []) ⇒ Object

Self and related nodes/relationships



90
91
92
93
94
95
96
97
98
# File 'lib/n4j/entity.rb', line 90

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

#dependentObject



85
# File 'lib/n4j/entity.rb', line 85

def dependent         ; [] ; end

#destroyObject



67
68
69
70
71
72
# File 'lib/n4j/entity.rb', line 67

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



100
101
102
103
104
105
106
# File 'lib/n4j/entity.rb', line 100

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

Returns:

  • (Boolean)


63
64
65
# File 'lib/n4j/entity.rb', line 63

def destroyed?
  @destroyed
end

#from_neo4j_relativeObject



47
48
49
50
51
52
53
# File 'lib/n4j/entity.rb', line 47

def from_neo4j_relative
  # Use try here?
  @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



40
41
42
43
44
45
# File 'lib/n4j/entity.rb', line 40

def load_neo4j_data(hsh)
  changed_attributes.clear
  if hsh # update requests do not return data
    self.from_neo4j = HashWithIndifferentAccess.new.merge(hsh)
  end
end

#needs_persist?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/n4j/entity.rb', line 59

def needs_persist?
  !persisted? || changed?
end

#note_update!Object



36
37
38
# File 'lib/n4j/entity.rb', line 36

def note_update!
  changed_attributes.clear
end

#persisted?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/n4j/entity.rb', line 55

def persisted?
  N4j.neo4j_hash?(from_neo4j) && !destroyed?
end

#place_in_batchObject



112
113
114
# File 'lib/n4j/entity.rb', line 112

def place_in_batch
  @place_in_batch
end

#place_in_batch=(num) ⇒ Object



108
109
110
# File 'lib/n4j/entity.rb', line 108

def place_in_batch=(num)
  @place_in_batch = num
end

#post_saveObject



87
# File 'lib/n4j/entity.rb', line 87

def post_save         ; [] ; end

#prerequisitesObject



84
# File 'lib/n4j/entity.rb', line 84

def prerequisites     ; [] ; end

#saveObject



74
75
76
77
78
79
80
81
82
# File 'lib/n4j/entity.rb', line 74

def save
  updateable_bundle = create_bundle.select(&:persist_hash)
  commands = updateable_bundle.collect {|entity| entity.persist_hash(:id => entity.place_in_batch) }
  results = N4j.batch(commands)
  updateable_bundle.zip(results).each do |entity,result|
    entity.load_neo4j_data(result['body'])
    entity.post_save
  end
end