lib/redgraph/graph.rb in redgraph-0.1.0 vs lib/redgraph/graph.rb in redgraph-0.1.1
- old
+ new
@@ -71,21 +71,31 @@
Node.new(label: get_label(labels.first), properties: attrs).tap do |node|
node.id = node_id
end
end
- def nodes(label: nil)
- node_or_node_with_label = label ? "node:`#{label}`" : "node"
+ # Returns nodes. Options:
+ #
+ # - label: filter by label
+ # - properties: filter by properties
+ # - limit: number of items
+ # - skip: items offset (useful for pagination)
+ #
+ def nodes(label: nil, properties: nil, limit: nil, skip: nil)
+ _label = ":`#{label}`" if label
+ _props = quote_hash(properties) if properties
+ _limit = "LIMIT #{limit}" if limit
+ _skip = "SKIP #{skip}" if skip
- cmd = "MATCH (#{node_or_node_with_label}) RETURN node"
+ cmd = "MATCH (node#{_label} #{_props}) RETURN node #{_skip} #{_limit}"
result = query(cmd)
result.resultset.map do |item|
- (node_id, labels, properties) = item["node"]
+ (node_id, labels, props) = item["node"]
attrs = {}
- properties.each do |(index, type, value)|
+ props.each do |(index, type, value)|
attrs[get_property(index)] = value
end
Node.new(label: get_label(labels.first), properties: attrs).tap do |node|
node.id = node_id
end
@@ -110,14 +120,12 @@
data = @connection.call("GRAPH.QUERY", graph_name, cmd, "--compact")
QueryResponse.new(data)
end
def quote_hash(hash)
- out = "{"
- hash.each do |k,v|
- out += "#{k}:#{escape_value(v)}"
- end
- out + "}"
+ "{" +
+ hash.map {|k,v| "#{k}:#{escape_value(v)}" }.join(", ") +
+ "}"
end
def escape_value(x)
case x
when Integer then x