Class: ForestAdminDatasourceToolkit::Components::Query::Projection

Inherits:
Array
  • Object
show all
Includes:
Utils
Defined in:
lib/forest_admin_datasource_toolkit/components/query/projection.rb

Instance Method Summary collapse

Instance Method Details

#apply(records) ⇒ Object



71
72
73
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 71

def apply(records)
  records.map { |record| re_project(record) }
end

#columnsObject



24
25
26
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 24

def columns
  reject { |field| field.include?(':') }
end

#equals(other) ⇒ Object



67
68
69
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 67

def equals(other)
  length == other.length && all? { |field| other.include?(field) }
end

#nest(prefix: nil) ⇒ Object



43
44
45
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 43

def nest(prefix: nil)
  prefix ? Projection.new(map { |path| "#{prefix}:#{path}" }) : self
end

#re_project(record) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 75

def re_project(record)
  result = nil

  if record
    record = HashHelper.convert_keys(record, :to_s)
    result = {}
    columns.each { |column| result[column.to_s] = record[column.to_s] }
    relations.each { |relation, projection| result[relation] = projection.re_project(record[relation]) }
  end

  result
end

#relations(only_keys: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 28

def relations(only_keys: false)
  relations = each_with_object({}) do |path, memo|
    next unless path.include?(':')

    original_path = path.split(':')
    next if original_path.size == 1 && !only_keys

    relation = original_path.shift

    memo[relation] = Projection.new([original_path.join(':')].union(memo[relation] || []))
  end

  only_keys ? relations.keys : relations
end

#replaceObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 54

def replace(...)
  Projection.new(
    map(...)
      .reduce(Projection.new) do |memo, path|
      if path.is_a?(String)
        memo.union([path])
      else
        memo.union(path)
      end
    end
  )
end

#union(*other_arrays) ⇒ Object



88
89
90
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 88

def union(*other_arrays)
  Projection.new(to_a.union(*other_arrays))
end

#unnestObject



47
48
49
50
51
52
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 47

def unnest
  prefix = first.split(':')[0]
  raise 'Cannot unnest projection.' unless all? { |path| path.start_with?(prefix) }

  Projection.new(map { |path| path[prefix.length + 1, path.length - prefix.length - 1] })
end

#with_pks(collection) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 6

def with_pks(collection)
  ForestAdminDatasourceToolkit::Utils::Schema.primary_keys(collection).each do |key|
    push(key) unless include?(key)
  end

  relations.each do |relation, projection|
    schema = collection.schema[:fields][relation]
    next unless schema.type != 'PolymorphicManyToOne'

    association = collection.datasource.get_collection(schema.foreign_collection)
    projection_with_pks = projection.with_pks(association).nest(prefix: relation)

    projection_with_pks.each { |field| push(field) unless include?(field) }
  end

  self
end