Module: Neo4j::DelayedSave

Included in:
Model
Defined in:
lib/neo4j/delayed_save.rb

Overview

Extension to Neo4j::NodeMixin to delay creating/updating properties until #save is called.

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Delegate property access to temporary properties first



25
26
27
28
29
30
31
# File 'lib/neo4j/delayed_save.rb', line 25

def [](key)
  if @_unsaved_props.has_key?(key)
    @_unsaved_props[key]
  elsif @_java_node
    @_java_node[key]
  end
end

#[]=(key, value) ⇒ Object

Delegate property write to temporary properties



34
35
36
# File 'lib/neo4j/delayed_save.rb', line 34

def []=(key, value)
  @_unsaved_props[key] = value
end

#idObject



4
5
6
# File 'lib/neo4j/delayed_save.rb', line 4

def id
  persisted? ? neo_id : nil
end

#init_with_node(node) ⇒ Object

Ensure nodes loaded from the database are marked as persisted.



18
19
20
21
22
# File 'lib/neo4j/delayed_save.rb', line 18

def init_with_node(node)
  super
  @persisted = true
  @_unsaved_props = {}
end

#init_without_node(props) ⇒ Object

Override NodeMixin#init_without_node to save the properties for when #save is called.



10
11
12
13
14
15
# File 'lib/neo4j/delayed_save.rb', line 10

def init_without_node(props) # :nodoc:
  raise "Can't use Neo4j::Model with anonymous classes" if self.class.name == ""
  props[:_classname] = self.class.name
  @_unsaved_props = props || {}
  @persisted = false
end

#reloadObject Also known as: reset



38
39
40
41
# File 'lib/neo4j/delayed_save.rb', line 38

def reload(*)
  @_unsaved_props.clear
  self
end

#saveObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/neo4j/delayed_save.rb', line 44

def save
  if valid?
    _run_save_callbacks do
      if @persisted
        _run_update_callbacks do
          @_unsaved_props.each {|k,v| @_java_node[k] = v }
          @_unsaved_props.clear
        end
      else
        _run_create_callbacks do
          @_java_node = Neo4j.create_node
          @_java_node._wrapper = self
          @_unsaved_props.each {|k,v| @_java_node[k] = v }
          @_unsaved_props.clear
          update_index
          Neo4j.event_handler.node_created(self)
          @persisted = true
        end
      end
    end
    true
  end
end