Module: Neoid::Relationship::InstanceMethods

Defined in:
lib/neoid/relationship.rb

Instance Method Summary collapse

Instance Method Details

#_neo_saveObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/neoid/relationship.rb', line 93

def _neo_save
  return unless Neoid.enabled?

  options = self.class.neoid_config.relationship_options

  start_item = send(options[:start_node])
  end_item = send(options[:end_node])

  return unless start_item && end_item

  # initialize nodes
  start_item.neo_node
  end_item.neo_node

  data = to_neo.merge(ar_type: self.class.name, ar_id: id, Neoid::UNIQUE_ID_KEY => neo_unique_id)
  data.reject! { |k, v| v.nil? }

  rel_type = options[:type].is_a?(Proc) ? options[:type].call(self) : options[:type]

  gremlin_query = <<-GREMLIN
    idx = g.idx('relationship_auto_index');
    q = null;
    if (idx) q = idx.get(unique_id_key, unique_id);

    relationship = null;
    if (q && q.hasNext()) {
      relationship = q.next();
      relationship_data.each {
        if (relationship.getProperty(it.key) != it.value) {
          relationship.setProperty(it.key, it.value);
        }
      }
    } else {
      node_index = g.idx('node_auto_index');
      start_node = node_index.get(unique_id_key, start_node_unique_id).next();
      end_node = node_index.get(unique_id_key, end_node_unique_id).next();

      relationship = g.addEdge(start_node, end_node, rel_type, relationship_data);
    }

    relationship
  GREMLIN

  script_vars = {
    unique_id_key: Neoid::UNIQUE_ID_KEY,
    relationship_data: data,
    unique_id: neo_unique_id,
    start_node_unique_id: start_item.neo_unique_id,
    end_node_unique_id: end_item.neo_unique_id,
    rel_type: rel_type
  }

  Neoid::logger.info "Relationship#neo_save #{self.class.name} #{id}"

  relationship = Neoid.execute_script_or_add_to_batch gremlin_query, script_vars do |value|
    Neoid::Relationship.from_hash(value)
  end

  relationship
end

#neo_find_by_idObject



87
88
89
90
91
# File 'lib/neoid/relationship.rb', line 87

def neo_find_by_id
  results = Neoid.db.get_relationship_auto_index(Neoid::UNIQUE_ID_KEY, neo_unique_id)
  relationship = results.present? ? Neoid::Relationship.from_hash(results[0]) : nil
  relationship
end

#neo_load(hash) ⇒ Object



154
155
156
# File 'lib/neoid/relationship.rb', line 154

def neo_load(hash)
  Neoid::Relationship.from_hash(hash)
end

#neo_relationshipObject



158
159
160
# File 'lib/neoid/relationship.rb', line 158

def neo_relationship
  _neo_representation
end