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 = {}) ⇒ Graph

Returns a new instance of Graph.



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

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

Instance Method Details

#add_joins(dataset, prefix = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rasti/db/relations/graph.rb', line 47

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



29
30
31
32
33
# File 'lib/rasti/db/relations/graph.rb', line 29

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

#fetch_graph(rows) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rasti/db/relations/graph.rb', line 35

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] ,
                                  subgraph_of(node)
  end
end

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



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

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

#with_all_attributes_for(relations) ⇒ Object



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

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