Module: Redgraph::NodeModel::ClassMethods
- Defined in:
- lib/redgraph/node_model/class_methods.rb
Instance Method Summary collapse
-
#all(label: nil, properties: {}, limit: nil, skip: nil, order: nil) ⇒ Object
Returns an array of nodes.
-
#count(label: nil, properties: nil) ⇒ Object
Returns the number of nodes with the current label.
- #create(properties) ⇒ Object
-
#find(id) ⇒ Object
Finds a node by id.
-
#label ⇒ Object
Current label.
-
#label=(x) ⇒ Object
Sets the label for this class of nodes.
-
#query(cmd) ⇒ Object
Runs a query on the graph, but converts the nodes to the corresponding ActiveModel class if available - otherwise they stay NodeObjects.
-
#reify_from_node(node) ⇒ Object
Converts a Node object into NodeModel.
Instance Method Details
#all(label: nil, properties: {}, limit: nil, skip: nil, order: nil) ⇒ Object
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)
12 13 14 15 16 17 |
# 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) ⇒ Object
Returns the number of nodes with the current label. Options:
-
properties: filter by properties
23 24 25 |
# 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) ⇒ Object
68 69 70 |
# File 'lib/redgraph/node_model/class_methods.rb', line 68 def create(properties) new(**properties).add_to_graph end |
#find(id) ⇒ Object
Finds a node by id. Returns nil if not found
29 30 31 32 33 |
# 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 ⇒ Object
Current label
42 43 44 |
# File 'lib/redgraph/node_model/class_methods.rb', line 42 def label @label ||= default_label end |
#label=(x) ⇒ Object
Sets the label for this class of nodes. If missing it will be computed from the class name
36 37 38 |
# File 'lib/redgraph/node_model/class_methods.rb', line 36 def label=(x) @label = x end |
#query(cmd) ⇒ Object
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.
58 59 60 61 62 63 64 65 66 |
# 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) ⇒ Object
Converts a Node object into NodeModel
48 49 50 51 |
# 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 |