Module: Redgraph::NodeModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/redgraph/node_model.rb,
lib/redgraph/node_model/persistence.rb,
lib/redgraph/node_model/class_methods.rb,
lib/redgraph/node_model/graph_manipulation.rb

Overview

This mixin allows you to use an interface similar to ActiveRecord

class Actor

include Redgraph::NodeModel

self.graph = Redgraph::Graph.new("movies", url: $REDIS_URL)
self.label = "actor" # optional, if missing it will be extracted from the class name
attribute :name

end

You will then be able to

john = Actor.find(123) total = Actor.count

When you create a record it will automatically set the _type property with the class name. This allows reifying the node into the corresponding NodeModel class.

Defined Under Namespace

Modules: ClassMethods, GraphManipulation, Persistence

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



94
95
96
# File 'lib/redgraph/node_model.rb', line 94

def ==(other)
  attributes == other.attributes && id == other.id
end

#assign_attributes(attrs = {}) ⇒ Object



83
84
85
86
87
# File 'lib/redgraph/node_model.rb', line 83

def assign_attributes(attrs = {})
  attrs.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

#attributesObject

Object attributes as a hash



79
80
81
# File 'lib/redgraph/node_model.rb', line 79

def attributes
  self.class.attribute_names.to_h { |name| [name, public_send(name)] }
end

#graphObject

The current graph



69
70
71
# File 'lib/redgraph/node_model.rb', line 69

def graph
  self.class.graph
end

#initialize(**args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redgraph/node_model.rb', line 55

def initialize(**args)
  absent_attributes = args.keys.map(&:to_sym) - self.class.attribute_names - [:_type]

  if absent_attributes.any?
    raise ArgumentError, "Unknown attribute #{absent_attributes}"
  end

  args.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

#labelObject



73
74
75
# File 'lib/redgraph/node_model.rb', line 73

def label
  self.class.label
end

#to_nodeObject



89
90
91
92
# File 'lib/redgraph/node_model.rb', line 89

def to_node
  props = attributes.except(:id).merge(_type: self.class.name)
  Redgraph::Node.new(id: id, label: label, properties: props)
end