Class: ForestAdminDatasourceToolkit::Components::Query::Projection
- Inherits:
-
Array
- Object
- Array
- ForestAdminDatasourceToolkit::Components::Query::Projection
- Includes:
- Utils
- Defined in:
- lib/forest_admin_datasource_toolkit/components/query/projection.rb
Instance Method Summary collapse
- #apply(records) ⇒ Object
- #columns ⇒ Object
- #equals(other) ⇒ Object
- #nest(prefix: nil) ⇒ Object
- #re_project(record) ⇒ Object
- #relation_include_paths ⇒ Object
- #relations(only_keys: false) ⇒ Object
- #replace ⇒ Object
- #union(*other_arrays) ⇒ Object
- #unnest ⇒ Object
- #with_pks(collection) ⇒ Object
Instance Method Details
#apply(records) ⇒ Object
78 79 80 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 78 def apply(records) records.map { |record| re_project(record) } end |
#columns ⇒ Object
25 26 27 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 25 def columns reject { |field| field.include?(':') } end |
#equals(other) ⇒ Object
74 75 76 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 74 def equals(other) length == other.length && all? { |field| other.include?(field) } end |
#nest(prefix: nil) ⇒ Object
50 51 52 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 50 def nest(prefix: nil) prefix ? Projection.new(map { |path| "#{prefix}:#{path}" }) : self end |
#re_project(record) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 82 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 |
#relation_include_paths ⇒ Object
44 45 46 47 48 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 44 def relation_include_paths relations.flat_map do |relation, sub_projection| [relation, *sub_projection.relation_include_paths.map { |path| "#{relation}.#{path}" }] end end |
#relations(only_keys: false) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 29 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 |
#replace ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 61 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
95 96 97 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 95 def union(*other_arrays) Projection.new(to_a.union(*other_arrays)) end |
#unnest ⇒ Object
54 55 56 57 58 59 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 54 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
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/forest_admin_datasource_toolkit/components/query/projection.rb', line 7 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 |