Module: ActiveGraph::Node::Persistence::ClassMethods

Defined in:
lib/active_graph/node/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(props = {}) ⇒ Object

Creates and saves a new node

Parameters:

  • props (Hash) (defaults to: {})

    the properties the new node should have



95
96
97
98
99
100
# File 'lib/active_graph/node/persistence.rb', line 95

def create(props = {})
  new(props).tap do |obj|
    yield obj if block_given?
    obj.save
  end
end

#create!(props = {}) ⇒ Object

Same as #create, but raises an error if there is a problem during save.



103
104
105
106
107
108
# File 'lib/active_graph/node/persistence.rb', line 103

def create!(props = {})
  new(props).tap do |o|
    yield o if block_given?
    o.save!
  end
end

#find_or_create(find_attributes, set_attributes = {}) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/active_graph/node/persistence.rb', line 124

def find_or_create(find_attributes, set_attributes = {})
  on_create_attributes = set_attributes.reverse_merge(find_attributes.merge(self.new(find_attributes).props_for_create))

  new_query.merge(n: {self.mapped_label_names => find_attributes})
           .on_create_set(n: on_create_attributes)
           .pluck(:n).first
end

#find_or_create_by(attributes, &block) ⇒ Object

Finds the first node with the given attributes, or calls create if none found



133
134
135
# File 'lib/active_graph/node/persistence.rb', line 133

def find_or_create_by(attributes, &block)
  find_by(attributes) || create(attributes, &block)
end

#find_or_create_by!(attributes, &block) ⇒ Object

Same as #find_or_create_by, but calls #create! so it raises an error if there is a problem during save.



138
139
140
# File 'lib/active_graph/node/persistence.rb', line 138

def find_or_create_by!(attributes, &block)
  find_by(attributes) || create!(attributes, &block)
end

#find_or_initialize_by(attributes) ⇒ Object



142
143
144
# File 'lib/active_graph/node/persistence.rb', line 142

def find_or_initialize_by(attributes)
  find_by(attributes) || new(attributes).tap { |o| yield(o) if block_given? }
end

#load_entity(id) ⇒ Object



146
147
148
149
150
# File 'lib/active_graph/node/persistence.rb', line 146

def load_entity(id)
  query = query_base_for(id, :n).return(:n)
  result = neo4j_query(query).first
  result && result[:n]
end

#merge(match_attributes, optional_attrs = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_graph/node/persistence.rb', line 110

def merge(match_attributes, optional_attrs = {})
  options = [:on_create, :on_match, :set]
  optional_attrs.assert_valid_keys(*options)

  optional_attrs.default = {}
  on_create_attrs, on_match_attrs, set_attrs = optional_attrs.values_at(*options)

  new_query.merge(n: {self.mapped_label_names => match_attributes})
           .on_create_set(on_create_clause(on_create_attrs))
           .on_match_set(on_match_clause(on_match_attrs))
           .break.set(n: set_attrs)
           .pluck(:n).first
end

#query_base_for(neo_id, var = :n) ⇒ Object



152
153
154
# File 'lib/active_graph/node/persistence.rb', line 152

def query_base_for(neo_id, var = :n)
  ActiveGraph::Base.new_query.match(var).where(var => {neo_id: neo_id})
end