module Redgraph::NodeModel::ClassMethods

Public Instance Methods

all(label: nil, properties: {}, limit: nil, skip: nil, order: nil) click to toggle source

Returns an array of nodes. Options:

  • label: filter by label

  • properties: filter by properties

  • order: node.name ASC, node.year DESC

  • limit: number of items

  • skip: items offset (useful for pagination)

# File lib/redgraph/node_model/class_methods.rb, line 12
def all(label: nil, properties: {}, limit: nil, skip: nil, order: nil)
  graph.nodes(label: label, properties: properties_plus_type(properties),
              limit: limit, skip: skip, order: nil).map do |node|
    reify_from_node(node)
  end
end
count(label: nil, properties: nil) click to toggle source

Returns the number of nodes with the current label. Options:

  • properties: filter by properties

# File lib/redgraph/node_model/class_methods.rb, line 23
def count(label: nil, properties: nil)
  graph.count_nodes(label: label, properties: properties_plus_type(properties))
end
create(properties) click to toggle source
# File lib/redgraph/node_model/class_methods.rb, line 68
def create(properties)
  new(**properties).add_to_graph
end
find(id) click to toggle source

Finds a node by id. Returns nil if not found

# File lib/redgraph/node_model/class_methods.rb, line 29
def find(id)
  node = graph.find_node_by_id(id)
  return unless node
  reify_from_node(node)
end
label() click to toggle source

Current label

# File lib/redgraph/node_model/class_methods.rb, line 42
def label
  @label ||= default_label
end
label=(x) click to toggle source

Sets the label for this class of nodes. If missing it will be computed from the class name

# File lib/redgraph/node_model/class_methods.rb, line 36
def label=(x)
  @label = x
end
query(cmd) click to toggle source

Runs a query on the graph, but converts the nodes to the corresponding ActiveModel class if available - otherwise they stay NodeObjects.

Returns an array of rows.

# File lib/redgraph/node_model/class_methods.rb, line 58
def query(cmd)
  raise MissingGraphError unless graph

  graph.query(cmd).map do |row|
    row.map do |item|
      item.is_a?(Node) ? reify_from_node(item) : item
    end
  end
end
reify_from_node(node) click to toggle source

Converts a Node object into NodeModel

# File lib/redgraph/node_model/class_methods.rb, line 48
def reify_from_node(node)
  klass = node.properties[:_type].to_s.safe_constantize || self
  klass.new(id: node.id, **node.properties)
end

Private Instance Methods

default_label() click to toggle source
# File lib/redgraph/node_model/class_methods.rb, line 74
def default_label
  name.demodulize.underscore
end
properties_plus_type(properties = {}) click to toggle source
# File lib/redgraph/node_model/class_methods.rb, line 78
def properties_plus_type(properties = {})
  {_type: name}.merge(properties || {})
end