Module: Redgraph::NodeModel::Persistence

Defined in:
lib/redgraph/node_model/persistence.rb

Instance Method Summary collapse

Instance Method Details

#add_to_graph(allow_duplicates: true) ⇒ Object

Adds the node to the graph

  • allow_duplicates: if false it will create a node with the same type and properties only if

    not present
    

Raises:



9
10
11
12
13
14
# File 'lib/redgraph/node_model/persistence.rb', line 9

def add_to_graph(allow_duplicates: true)
  raise MissingGraphError unless graph
  item = allow_duplicates ? graph.add_node(to_node) : graph.merge_node(to_node)
  self.id = item.id
  self
end

#destroyObject

Deletes the record from the graph



40
41
42
43
44
45
46
47
# File 'lib/redgraph/node_model/persistence.rb', line 40

def destroy
  @destroyed = true
  if graph.destroy_node(self)
    self
  else
    false
  end
end

#destroyed?Boolean

Returns true if this object has been destroyed, otherwise returns false.

Returns:

  • (Boolean)


51
52
53
# File 'lib/redgraph/node_model/persistence.rb', line 51

def destroyed?
  !!@destroyed
end

#persisted?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/redgraph/node_model/persistence.rb', line 27

def persisted?
  id.present?
end

#reloadObject



31
32
33
34
35
36
# File 'lib/redgraph/node_model/persistence.rb', line 31

def reload
  item = self.class.find(id)
  @label = item.label
  assign_attributes(item.attributes)
  self
end

#saveObject

Creates a new record or updates the existing



18
19
20
21
22
23
24
25
# File 'lib/redgraph/node_model/persistence.rb', line 18

def save
  if persisted?
    item = graph.update_node(to_node)
    self.class.reify_from_node(item)
  else
    add_to_graph
  end
end