lib/neoid/node.rb in neoid-0.1.2 vs lib/neoid/node.rb in neoid-0.2.0

- old
+ new

@@ -4,19 +4,26 @@ node = Neography::Node.new(hash) node.neo_server = Neoid.db node end + def self.included(receiver) + receiver.send :include, Neoid::ModelAdditions + receiver.extend ClassMethods + receiver.send :include, InstanceMethods + Neoid.node_models << receiver + end + module ClassMethods attr_accessor :neo_subref_node def neo_subref_rel_type - @_neo_subref_rel_type ||= "#{self.name.tableize}_subref" + @_neo_subref_rel_type ||= "#{name.tableize}_subref" end def neo_subref_node_rel_type - @_neo_subref_node_rel_type ||= self.name.tableize + @_neo_subref_node_rel_type ||= name.tableize end def delete_command :delete_node end @@ -58,11 +65,11 @@ # Neoid.logger.info "subref query:\n#{gremlin_query}" script_vars = { neo_subref_rel_type: neo_subref_rel_type, - name: self.name + name: name } Neoid.execute_script_or_add_to_batch gremlin_query, script_vars do |value| Neoid::Node.from_hash(value) end @@ -75,22 +82,22 @@ def neo_search(term, options = {}) Neoid.search(self, term, options) end end - + module InstanceMethods def neo_find_by_id # Neoid::logger.info "Node#neo_find_by_id #{self.class.neo_index_name} #{self.id}" - node = Neoid.db.get_node_auto_index(Neoid::UNIQUE_ID_KEY, self.neo_unique_id) + node = Neoid.db.get_node_auto_index(Neoid::UNIQUE_ID_KEY, neo_unique_id) node.present? ? Neoid::Node.from_hash(node[0]) : nil end - + def _neo_save return unless Neoid.enabled? - data = self.to_neo.merge(ar_type: self.class.name, ar_id: self.id, Neoid::UNIQUE_ID_KEY => self.neo_unique_id) + 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? } gremlin_query = <<-GREMLIN idx = g.idx('node_auto_index'); q = null; @@ -112,14 +119,14 @@ } node GREMLIN - script_vars = { + script_vars = { unique_id_key: Neoid::UNIQUE_ID_KEY, node_data: data, - unique_id: self.neo_unique_id, + unique_id: neo_unique_id, enable_subrefs: Neoid.config.enable_subrefs, enable_model_index: Neoid.config.enable_per_model_indexes && self.class.neoid_config.enable_model_index } if Neoid.config.enable_subrefs @@ -133,11 +140,11 @@ script_vars.update( neo_model_index_name: self.class.neo_model_index_name ) end - Neoid::logger.info "Node#neo_save #{self.class.name} #{self.id}" + Neoid::logger.info "Node#neo_save #{self.class.name} #{id}" node = Neoid.execute_script_or_add_to_batch(gremlin_query, script_vars) do |value| @_neo_representation = Neoid::Node.from_hash(value) end.then do |result| neo_search_index @@ -169,18 +176,18 @@ def neo_helper_get_field_value(field, options = {}) if options[:block] options[:block].call else - self.send(field) rescue (raise "No field #{field} for #{self.class.name}") + send(field) rescue (raise "No field #{field} for #{self.class.name}") end end - + def neo_load(hash) Neoid::Node.from_hash(hash) end - + def neo_node _neo_representation end def neo_after_relationship_remove(relationship) @@ -189,22 +196,15 @@ def neo_before_relationship_through_remove(record) rel_model, foreign_key_of_owner, foreign_key_of_record = Neoid::Relationship.meta_data[self.class.name.to_s][record.class.name.to_s] rel_model = rel_model.to_s.constantize @__neo_temp_rels ||= {} - @__neo_temp_rels[record] = rel_model.where(foreign_key_of_owner => self.id, foreign_key_of_record => record.id).first + @__neo_temp_rels[record] = rel_model.where(foreign_key_of_owner => id, foreign_key_of_record => record.id).first end def neo_after_relationship_through_remove(record) @__neo_temp_rels.each { |record, relationship| relationship.neo_destroy } @__neo_temp_rels.delete(record) end - end - - def self.included(receiver) - receiver.send :include, Neoid::ModelAdditions - receiver.extend ClassMethods - receiver.send :include, InstanceMethods - Neoid.node_models << receiver end end end