app/lib/cds_importer/entity_mapper/base_mapper.rb
def parse
        expanded = [@values]
        # iterating through all the mapping keys to expand Arrays
        entity_mapping_keys_to_parse.each do |path|
          current_path = []
          path.each do |key|
            current_path << key
            new_expanded = nil
            expanded.each do |values|
              value = values.dig(*current_path)
              if value.is_a?(Array)
                # iterating through all items in Array and creating @values copy
                value.each do |v|
                  # [1,2,3] => {1=>{2=>{3=>value}}
                  tmp = current_path.lazy.reverse_each.inject(v) { |memo, i| memo = { i => memo }; memo }
                  new_expanded ||= []
                  new_expanded << values.deep_merge(tmp)
                end
              end
            end
            expanded = new_expanded if new_expanded.present?
          end
        end
        # creating instances for all expanded values
        if mapping_path.present?
          expanded.select! { |values| values.dig(*mapping_path.split(PATH_SEPARATOR)).present? }
        end
        expanded.map { |values| create_instance(values) }
      end
# Sometimes we have array as a mapping path value,
# so need to iterate through it and import each item separately
Sometimes we have array as a mapping path value,
so need to iterate through it and import each item separately
Sometimes we have array as a mapping path value so need to iterate through it and import each item separately
def parse
        expanded = [@values]
        entity_mapping_keys_to_parse.each do |path|
          current_path = []
          path.each do |key|
            current_path << key
            new_expanded = nil
            expanded.each do |values|
              value = values.dig(*current_path)
              if value.is_a?(Array)
                value.each do |v|
                  tmp = current_path.lazy.reverse_each.inject(v) { |memo, i| memo = { i => memo }; memo }
                  new_expanded ||= []
                  new_expanded << values.deep_merge(tmp)
                end
              end
            end
            expanded = new_expanded if new_expanded.present?
          end
        end
        if mapping_path.present?
          expanded.select! { |values| values.dig(*mapping_path.split(PATH_SEPARATOR)).present? }
        end
        expanded.map { |values| create_instance(values) }
      end
def parse expanded = [ @values ] entity_mapping_keys_to_parse . each do | path | current_path = [ ] path . each do | key | current_path << key new_expanded = nil expanded . each do | values | value = values . dig ( * current_path ) if value . is_a? ( Array ) value . each do | v | tmp = current_path . lazy . reverse_each . inject ( v ) { | memo , i | memo = { i => memo } ; memo } new_expanded ||= [ ] new_expanded << values . deep_merge ( tmp ) end end end expanded = new_expanded if new_expanded . present? end end if mapping_path . present? expanded . select! { | values | values . dig ( * mapping_path . split ( PATH_SEPARATOR ) ) . present? } end expanded . map { | values | create_instance ( values ) } end
Sometimes we have array as a mapping path value,
so need to iterate through it and import each item separately
Sometimes we have array as a mapping path value so need to iterate through it and import each item separately
# iterating through all the mapping keys to expand Arrays # iterating through all items in Array and creating @values copy # [1,2,3] => {1=>{2=>{3=>value}} # creating instances for all expanded values