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



106
107
108
109
110
111
# File 'lib/active_graph/node/persistence.rb', line 106

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.



114
115
116
117
118
119
# File 'lib/active_graph/node/persistence.rb', line 114

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



135
136
137
138
139
140
141
# File 'lib/active_graph/node/persistence.rb', line 135

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



144
145
146
# File 'lib/active_graph/node/persistence.rb', line 144

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.



149
150
151
# File 'lib/active_graph/node/persistence.rb', line 149

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

#find_or_initialize_by(attributes) ⇒ Object



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

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

#load_entity(id) ⇒ Object



157
158
159
160
161
# File 'lib/active_graph/node/persistence.rb', line 157

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



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_graph/node/persistence.rb', line 121

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



163
164
165
# File 'lib/active_graph/node/persistence.rb', line 163

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