Class: Neo4j::ActiveNode::Query::QueryProxy::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/active_node/query/query_proxy_link.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clause, arg, args = []) ⇒ Link

Returns a new instance of Link.



8
9
10
11
12
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 8

def initialize(clause, arg, args = [])
  @clause = clause
  @arg = arg
  @args = args
end

Instance Attribute Details

#clauseObject (readonly)

Returns the value of attribute clause.



6
7
8
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 6

def clause
  @clause
end

Class Method Details

.converted_key(model, key) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 123

def converted_key(model, key)
  if key.to_sym == :id
    model ? model.id_property_name : :uuid
  else
    key
  end
end

.converted_keys(model, arg) ⇒ Object



119
120
121
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 119

def converted_keys(model, arg)
  arg.is_a?(Hash) ? Hash[arg.map { |key, value| [converted_key(model, key), value] }] : arg
end

.converted_value(model, key, value) ⇒ Object



131
132
133
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 131

def converted_value(model, key, value)
  model.declared_properties.value_for_where(key, value)
end

.for_arg(model, clause, arg, *args) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 111

def for_arg(model, clause, arg, *args)
  default = [Link.new(clause, arg, *args)]

  Link.for_clause(clause, arg, model, *args) || default
rescue NoMethodError
  default
end

.for_args(model, clause, args, association = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 101

def for_args(model, clause, args, association = nil)
  if [:where, :where_not].include?(clause) && args[0].is_a?(String) # Better way?
    [for_arg(model, clause, args[0], *args[1..-1])]
  elsif [:rel_where, :rel_where_not].include?(clause)
    args.map { |arg| for_arg(model, clause, arg, association) }
  else
    args.map { |arg| for_arg(model, clause, arg) }
  end
end

.for_association(name, value, n_string, model) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 68

def for_association(name, value, n_string, model)
  neo_id = value.try(:neo_id) || value
  fail ArgumentError, "Invalid value for '#{name}' condition" if not neo_id.is_a?(Integer)

  [
    new(:match, ->(v, _) { "(#{v})#{model.associations[name].arrow_cypher}(#{n_string})" }),
    new(:where, ->(_, _) { {"ID(#{n_string})" => neo_id.to_i} })
  ]
end

.for_clause(clause, arg, model, *args) ⇒ Object



23
24
25
26
27
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 23

def for_clause(clause, arg, model, *args)
  method_to_call = "for_#{clause}_clause"

  send(method_to_call, arg, model, *args)
end

.for_order_clause(arg, model) ⇒ Object



97
98
99
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 97

def for_order_clause(arg, model)
  [new(:order, ->(v, _) { arg.is_a?(String) ? arg : {v => converted_keys(model, arg)} })]
end

.for_rel_order_clause(arg, _) ⇒ Object



93
94
95
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 93

def for_rel_order_clause(arg, _)
  [new(:order, ->(_, v) { arg.is_a?(String) ? arg : {v => arg} })]
end

.for_rel_where_clause(arg, _, association) ⇒ Object

We don’t accept strings here. If you want to use a string, just use where.



79
80
81
82
83
84
85
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 79

def for_rel_where_clause(arg, _, association)
  arg.each_with_object([]) do |(key, value), result|
    rel_class = association.relationship_class if association.relationship_class
    val =  rel_class ? converted_value(rel_class, key, value) : value
    result << new(:where, ->(_, rel_var) { {rel_var => {key => val}} })
  end
end

.for_rel_where_not_clause(*args) ⇒ Object



87
88
89
90
91
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 87

def for_rel_where_not_clause(*args)
  for_rel_where_clause(*args).each do |link|
    link.instance_variable_set('@clause', :where_not)
  end
end

.for_where_clause(arg, model, *args) ⇒ Object Also known as: for_node_where_clause



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 29

def for_where_clause(arg, model, *args)
  node_num = 1
  result = []
  if arg.is_a?(Hash)
    arg.each do |key, value|
      if model && model.association?(key)
        result += for_association(key, value, "n#{node_num}", model)
        node_num += 1
      else
        result << new_for_key_and_value(model, key, value)
      end
    end
  elsif arg.is_a?(String)
    result << new(:where, arg, args)
  end
  result
end

.for_where_not_clause(*args) ⇒ Object



48
49
50
51
52
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 48

def for_where_not_clause(*args)
  for_where_clause(*args).each do |link|
    link.instance_variable_set('@clause', :where_not)
  end
end

.new_for_key_and_value(model, key, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 54

def new_for_key_and_value(model, key, value)
  key = converted_key(model, key)

  val = if !model
          value
        elsif key == model.id_property_name && value.is_a?(Neo4j::ActiveNode)
          value.id
        else
          converted_value(model, key, value)
        end

  new(:where, ->(v, _) { {v => {key => val}} })
end

Instance Method Details

#args(var, rel_var) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/neo4j/active_node/query/query_proxy_link.rb', line 14

def args(var, rel_var)
  if @arg.respond_to?(:call)
    @arg.call(var, rel_var)
  else
    [@arg] + @args
  end
end