Class: ActiveGraph::Node::Query::QueryProxyEagerLoading::AssociationTree

Inherits:
Hash
  • Object
show all
Defined in:
lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name = nil, rel_length = nil) ⇒ AssociationTree

Returns a new instance of AssociationTree.



8
9
10
11
12
13
14
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 8

def initialize(model, name = nil, rel_length = nil)
  super()
  self.model = name ? target_class(model, name) : model
  self.name = name
  self.association = name ? model.associations[name] : nil
  self.rel_length = rel_length
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



6
7
8
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 6

def association
  @association
end

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 6

def model
  @model
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 6

def name
  @name
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 6

def path
  @path
end

#rel_lengthObject

Returns the value of attribute rel_length.



6
7
8
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 6

def rel_length
  @rel_length
end

Instance Method Details

#add_key(key, length = nil) ⇒ Object



50
51
52
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 50

def add_key(key, length = nil)
  self[key] ||= self.class.new(model, key, length)
end

#add_nested(key, value, length = nil) ⇒ Object



54
55
56
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 54

def add_nested(key, value, length = nil)
  add_key(key, length).add_spec(value)
end

#add_spec(spec) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 20

def add_spec(spec)
  fail_spec(spec) unless model

  case spec
  when nil
    nil
  when Array
    spec.each(&method(:add_spec))
  when Hash
    process_hash(spec)
  when String
    process_string(spec)
  else
    add_key(spec)
  end
end

#cloneObject



16
17
18
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 16

def clone
  super.tap { |copy| copy.each { |key, value| copy[key] = value.clone } }
end

#fail_spec(spec) ⇒ Object



37
38
39
40
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 37

def fail_spec(spec)
  fail "Cannot eager load \"past\" a polymorphic association. \
    (Since the association can return multiple models, we don't how to handle the \"#{spec}\" association.)"
end

#paths(*prefix) ⇒ Object



42
43
44
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 42

def paths(*prefix)
  values.flat_map { |v| [[*prefix, v]] + v.paths(*prefix, v) }
end

#process_hash(spec) ⇒ Object



46
47
48
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 46

def process_hash(spec)
  spec.each { |key, value| add_nested(key, value) }
end

#process_string(str) ⇒ Object



58
59
60
61
62
63
# File 'lib/active_graph/node/query/query_proxy_eager_loading/association_tree.rb', line 58

def process_string(str)
  head, rest = str.split('.', 2)
  k, length = head.split('*', -2)
  length = {max: length} if length
  add_nested(k.to_sym, rest, length)
end