Class: CanCanCan::Neo4j::CypherConstructor

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

Overview

Constructs cypher query from rule cypher options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule_cyphers) ⇒ CypherConstructor

Returns a new instance of CypherConstructor.



9
10
11
12
13
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 9

def initialize(rule_cyphers)
  @model_class = rule_cyphers.first.options[:model_class]
  reset_variables
  construct_cypher(rule_cyphers)
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 7

def query
  @query
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 7

def scope
  @scope
end

Instance Method Details

#construct_can_cypher(rule_cypher) ⇒ Object



52
53
54
55
56
57
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 52

def construct_can_cypher(rule_cypher)
  with_clause = with_clause_for_rule(rule_cypher, true)
  @query = @query.optional_match(rule_cypher.path)
                 .where(rule_cypher.rule_conditions)
                 .with(with_clause)
end

#construct_cannot_cypher(rule_cypher) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 69

def construct_cannot_cypher(rule_cypher)
  match_cls = match_clause(rule_cypher)
  unwind_for_cannot(rule_cypher)
  @query = @query.break
                 .match(match_cls)
                 .where_not(rule_cypher.rule_conditions)
  with_claus = with_clause_for_rule(rule_cypher, false)
  @query = @query.with(with_claus)
end

#construct_cypher(rule_cyphers) ⇒ Object



20
21
22
23
24
25
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 20

def construct_cypher(rule_cyphers)
  rule_cyphers.each do |rule_cypher|
    construct_cypher_for_rule(rule_cypher)
  end
  unwind_query_with_distinct
end

#construct_cypher_for_rule(rule_cypher) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 27

def construct_cypher_for_rule(rule_cypher)
  rule = rule_cypher.options[:rule]
  return if update_scope(rule_cypher)
  reset_variables if rule.conditions.blank?
  if rule.base_behavior
    construct_can_cypher(rule_cypher)
  else
    construct_cannot_cypher(rule_cypher)
  end
end

#match_clause(rule_cypher) ⇒ Object



86
87
88
89
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 86

def match_clause(rule_cypher)
  var = rule_cypher.options[:var_label]
  @current_collection.present? ? "(#{var})" : rule_cypher.path
end

#reset_variablesObject



15
16
17
18
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 15

def reset_variables
  @query = @model_class.new_query
  @current_collection = nil
end

#unwind_for_cannot(rule_cypher) ⇒ Object



79
80
81
82
83
84
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 79

def unwind_for_cannot(rule_cypher)
  return unless @current_collection.present?
  var = rule_cypher.options[:var_label]
  @query = unwind_qeury(var)
           .with("DISTINCT #{var} as #{var}")
end

#unwind_qeury(var_name) ⇒ Object



48
49
50
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 48

def unwind_qeury(var_name)
  @query = @query.unwind("#{@current_collection} as #{var_name}")
end

#unwind_query_with_distinctObject



42
43
44
45
46
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 42

def unwind_query_with_distinct
  var = CanCanCan::Neo4j::CypherConstructorHelper.var_name(@model_class)
  @query = unwind_qeury("#{var}_can")
           .with("DISTINCT #{var}_can as #{var}")
end

#update_scope(rule_cypher) ⇒ Object



38
39
40
# File 'lib/cancancan/neo4j/cypher_constructor.rb', line 38

def update_scope(rule_cypher)
  @scope = rule_cypher.options[:scope]
end

#with_clause_for_rule(rule_cypher, can_rule) ⇒ Object



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

def with_clause_for_rule(rule_cypher, can_rule)
  var = rule_cypher.options[:var_label]
  with = "collect(DISTINCT #{var}) as #{var}_col"
  if can_rule && @current_collection
    with = "#{@current_collection} + #{with}"
  end
  @current_collection = "#{var}_col"
  with
end