Class: CanCanCan::Neo4j::CypherConstructorHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cancancan/neo4j/cypher_constructor_helper.rb

Overview

Cypher query constructs

Class Method Summary collapse

Class Method Details

.append_and_or_to_conditions(cypher_options, rule_cypher) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 58

def append_and_or_to_conditions(cypher_options, rule_cypher)
  conditions_string = cypher_options[:conditions]
  connector = if conditions_string.blank?
                ''
              else
                rule_cypher.conditions_connector
              end
  connector += 'NOT' if rule_cypher.append_not_to_conditions?
  cypher_options[:conditions] = conditions_string + connector
end

.bifurcate_conditions(conditions) ⇒ Object



54
55
56
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 54

def bifurcate_conditions(conditions)
  conditions.partition { |_, value| value.is_a?(Hash) }.map(&:to_h)
end

.condition_for_attribute(value, variable_name, attribute) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 28

def condition_for_attribute(value, variable_name, attribute)
  lhs = variable_name + '.' + attribute.to_s
  return lhs + ' IS NULL ' if value.nil?
  rhs = value.to_s
  rhs = "'" + rhs + "'" unless [true, false].include?(value)
  lhs + '=' + rhs
end

.condition_for_id(base_class, variable_name, value) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 41

def condition_for_id(base_class, variable_name, value)
  id_property = base_class.id_property_name
  if id_property == :neo_id
    "ID(#{variable_name})=#{value}"
  else
    variable_name + '.' + id_property.to_s + '=' + "'#{value}'"
  end
end

.condtion_for_path(path:, variable_name:, base_class:, key:) ⇒ Object



36
37
38
39
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 36

def condtion_for_path(path:, variable_name:, base_class:, key:)
  path = "(#{variable_name})" if path.blank?
  path + base_class.associations[key].arrow_cypher + '()'
end

.construct_conditions_string(conditions_hash, base_class, path = '') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 10

def construct_conditions_string(conditions_hash, base_class, path = '')
  variable_name = var_name(base_class)
  conditions_hash.collect do |key, value|
    condition = if base_class.associations_keys.include?(key)
                  con = condtion_for_path(path: path,
                                          variable_name: variable_name,
                                          base_class: base_class,
                                          key: key)
                  value ? con : ' NOT ' + con
                elsif key == :id
                  condition_for_id(base_class, variable_name, value)
                else
                  condition_for_attribute(value, variable_name, key)
                end
    '(' + condition + ')'
  end.join(' AND ')
end

.match_node_cypher(node_class) ⇒ Object



6
7
8
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 6

def match_node_cypher(node_class)
  "(#{var_name(node_class)}:`#{node_class.mapped_label_name}`)"
end

.var_name(class_constant) ⇒ Object



50
51
52
# File 'lib/cancancan/neo4j/cypher_constructor_helper.rb', line 50

def var_name(class_constant)
  class_constant.name.downcase.split('::').join('_')
end