lib/redgraph/node_model.rb in redgraph-0.2.0 vs lib/redgraph/node_model.rb in redgraph-0.2.1
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
require_relative 'node_model/class_methods'
+require_relative 'node_model/graph_manipulation'
+require_relative 'node_model/persistence'
module Redgraph
# This mixin allows you to use an interface similar to ActiveRecord
#
# class Actor
@@ -22,22 +24,24 @@
#
module NodeModel
extend ActiveSupport::Concern
included do |base|
+ include Persistence
+ include GraphManipulation
+
@attribute_names = [:id]
- attr_accessor :id
- attr_accessor :_type
+ attr_accessor :id, :_type
class << self
attr_reader :attribute_names
attr_accessor :graph
def attribute(name)
@attribute_names << name
- attr_reader(name)
+ attr_accessor(name)
end
private
def inherited(subclass)
@@ -74,46 +78,18 @@
#
def attributes
self.class.attribute_names.to_h { |name| [name, public_send(name)] }
end
- def persisted?
- id.present?
+ def assign_attributes(attrs = {})
+ attrs.each do |name, value|
+ instance_variable_set("@#{name}", value)
+ end
end
- # 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
- #
- def add_to_graph(allow_duplicates: true)
- item = allow_duplicates ? graph.add_node(to_node) : graph.merge_node(to_node)
- self.id = item.id
- self
- end
-
- # Adds a relation between the node and another node.
- #
- # - type: type of relation
- # - node: the destination node
- # - properties: optional properties hash
- # - allow_duplicates: if false it will create a relation between two nodes with the same type
- # and properties only if not present
- #
- def add_relation(type:, node:, properties: nil, allow_duplicates: true)
- edge = Edge.new(type: type, src: to_node, dest: node.to_node, properties: properties)
- allow_duplicates ? graph.add_edge(edge) : graph.merge_edge(edge)
- end
-
def to_node
props = attributes.except(:id).merge(_type: self.class.name)
Redgraph::Node.new(id: id, label: label, properties: props)
- end
-
- # Converts a Node object into NodeModel
- #
- def reify_from_node(node)
- self.class.reify_from_node(node)
end
def ==(other)
attributes == other.attributes && id == other.id
end