module ActiveGraph::Node::HasN::AssociationCypherMethods

Constants

VALID_REL_LENGTH_SYMBOLS

Public Instance Methods

arrow_cypher(var = nil, properties = {}, create = false, reverse = false, length = nil) click to toggle source

Return cypher partial query string for the relationship part of a MATCH (arrow / relationship definition)

   # File lib/active_graph/node/has_n/association_cypher_methods.rb
 6 def arrow_cypher(var = nil, properties = {}, create = false, reverse = false, length = nil)
 7   validate_origin!
 8 
 9   if create && length.present?
10     fail(ArgumentError, 'rel_length option cannot be specified when creating a relationship')
11   end
12 
13   direction_cypher(get_relationship_cypher(var, properties, create, length), create, reverse)
14 end

Private Instance Methods

cypher_for_hash_rel_length(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
68 def cypher_for_hash_rel_length(length)
69   range_end = length[:max]
70   range_end = nil if range_end == Float::INFINITY
71   "*#{length[:min]}..#{range_end}"
72 end
cypher_for_range_rel_length(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
62 def cypher_for_range_rel_length(length)
63   range_end = length.end
64   range_end = nil if range_end == Float::INFINITY
65   "*#{length.begin}..#{range_end}"
66 end
cypher_for_rel_length(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
49 def cypher_for_rel_length(length)
50   return nil if length.blank?
51 
52   validate_rel_length!(length)
53 
54   case length
55   when Symbol then VALID_REL_LENGTH_SYMBOLS[length]
56   when Integer then "*#{length}"
57   when Range then cypher_for_range_rel_length(length)
58   when Hash then cypher_for_hash_rel_length(length)
59   end
60 end
direction_cypher(relationship_cypher, create, reverse = false) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
18 def direction_cypher(relationship_cypher, create, reverse = false)
19   case get_direction(create, reverse)
20   when :out
21     "-#{relationship_cypher}->"
22   when :in
23     "<-#{relationship_cypher}-"
24   when :both
25     "-#{relationship_cypher}-"
26   end
27 end
get_properties_string(properties) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
38 def get_properties_string(properties)
39   p = properties.map do |key, value|
40     "#{key}: #{value.inspect}"
41   end.join(', ')
42   p.empty? ? '' : " {#{p}}"
43 end
get_relationship_cypher(var, properties, create, length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
29 def get_relationship_cypher(var, properties, create, length)
30   relationship_type = relationship_type(create)
31   relationship_name_cypher = ":`#{relationship_type}`" if relationship_type
32   rel_length_cypher = cypher_for_rel_length(length)
33   properties_string = get_properties_string(properties)
34 
35   "[#{var}#{relationship_name_cypher}#{rel_length_cypher}#{properties_string}]"
36 end
rel_length_error_message(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
80 def rel_length_error_message(length)
81   case length
82   when Integer then 'cannot be negative' if length < 0
83   when Symbol then rel_length_symbol_error_message(length)
84   when Range then rel_length_range_error_message(length)
85   when Hash then rel_length_hash_error_message(length)
86   else 'should be a Symbol, Integer, Range or Hash'
87   end
88 end
rel_length_hash_error_message(length) click to toggle source
    # File lib/active_graph/node/has_n/association_cypher_methods.rb
102 def rel_length_hash_error_message(length)
103   'Hash keys should be a subset of [:min, :max]' if (length.keys & [:min, :max]) != length.keys
104 end
rel_length_range_error_message(length) click to toggle source
    # File lib/active_graph/node/has_n/association_cypher_methods.rb
 94 def rel_length_range_error_message(length)
 95   if length.begin > length.end
 96     'cannot be a decreasing Range'
 97   elsif length.begin < 0
 98     'cannot include negative values'
 99   end
100 end
rel_length_symbol_error_message(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
90 def rel_length_symbol_error_message(length)
91   "expecting one of #{VALID_REL_LENGTH_SYMBOLS.keys.inspect}" if !VALID_REL_LENGTH_SYMBOLS.key?(length)
92 end
validate_rel_length!(length) click to toggle source
   # File lib/active_graph/node/has_n/association_cypher_methods.rb
74 def validate_rel_length!(length)
75   message = rel_length_error_message(length)
76   fail(ArgumentError, "Invalid value for rel_length (#{length.inspect}): #{message}") if message
77   true
78 end