Class: Rasti::DB::Relations::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/db/relations/graph.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment, collection_class, relations = [], selected_attributes = {}, excluded_attributes = {}, queries = {}) ⇒ Graph

Returns a new instance of Graph.



6
7
8
9
10
11
12
13
# File 'lib/rasti/db/relations/graph.rb', line 6

def initialize(environment, collection_class, relations=[], selected_attributes={}, excluded_attributes={}, queries={})
  @environment = environment
  @collection_class = collection_class
  @graph = build_graph relations,
                       Hash::Indifferent.new(selected_attributes),
                       Hash::Indifferent.new(excluded_attributes),
                       Hash::Indifferent.new(queries)
end

Instance Method Details

#add_joins(dataset, prefix = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rasti/db/relations/graph.rb', line 50

def add_joins(dataset, prefix=nil)
  graph.roots.inject(dataset) do |ds, node|
    relation = relation_of node
    dataset_with_relation = relation.add_join environment, ds, prefix
    subgraph_of(node).add_joins dataset_with_relation, relation.join_relation_name(prefix)
  end
end

#apply_to(query) ⇒ Object



31
32
33
34
35
36
# File 'lib/rasti/db/relations/graph.rb', line 31

def apply_to(query)
  query.graph(*flat_relations)
       .select_graph_attributes(flat_selected_attributes)
       .exclude_graph_attributes(flat_excluded_attributes)
       .graph_queries(flat_queries)
end

#fetch_graph(rows) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rasti/db/relations/graph.rb', line 38

def fetch_graph(rows)
  return if rows.empty?
  graph.roots.each do |node|
    relation_of(node).fetch_graph environment,
                                  rows,
                                  node[:selected_attributes],
                                  node[:excluded_attributes],
                                  node[:queries] ,
                                  subgraph_of(node)
  end
end

#merge(relations: [], selected_attributes: {}, excluded_attributes: {}, queries: {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/rasti/db/relations/graph.rb', line 15

def merge(relations:[], selected_attributes:{}, excluded_attributes:{}, queries: {})
  Graph.new environment,
            collection_class,
            (flat_relations | relations),
            flat_selected_attributes.merge(selected_attributes),
            flat_excluded_attributes.merge(excluded_attributes),
            flat_queries.merge(queries)
end

#with_all_attributes_for(relations) ⇒ Object



24
25
26
27
28
29
# File 'lib/rasti/db/relations/graph.rb', line 24

def with_all_attributes_for(relations)
  relations_with_all_attributes = relations.map { |r| [r, nil] }.to_h

  merge selected_attributes: relations_with_all_attributes,
        excluded_attributes: relations_with_all_attributes
end