lib/eco/api/common/people/default_parsers/csv_parser.rb in eco-helpers-2.0.26 vs lib/eco/api/common/people/default_parsers/csv_parser.rb in eco-helpers-2.0.27

- old
+ new

@@ -68,32 +68,29 @@ def unknown_headers(headers) (headers - known_headers) - all_internal_attrs end def missing_headers(headers) - hint = headers & all_internal_attrs - hext = headers - hint - int_head = hint + hext.map {|e| fields_mapper.to_internal(e)}.compact - known_as_int = known_headers.select do |e| - i = fields_mapper.to_internal(e) - int_head.include?(i) - end + int_head = internal_present_or_active(headers) ext = headers.select do |e| i = fields_mapper.to_internal(e) int_head.include?(i) end - ext_present = known_as_int | ext + ext_present = known_headers_present(int_head) | ext ext_miss = known_headers - ext_present - #int_miss = ext_miss.map {|ext| fields_mapper.to_internal(ext)} + ext_miss.each_with_object({}) do |ext, missing| next unless int = fields_mapper.to_internal(ext) if all_internal_attrs.include?(int) missing[:direct] ||= [] missing[:direct] << ext end related_attrs_requirements = required_attrs.values.select do |req| - req.dependant?(int) && !int_head.include?(req.attr) + dep = req.dependant?(int) + affects = dep && !int_head.include?(int) + in_header = int_head.include?(req.attr) + affects || (dep && !in_header) end next if related_attrs_requirements.empty? missing[:indirect] ||= {} data = missing[:indirect][ext] = {} data[:int] = int @@ -104,10 +101,45 @@ attrs[status] << req.attr end end end + def known_headers_present(headers_internal) + @known_internal ||= known_headers.select do |ext| + int = fields_mapper.to_internal(ext) + headers_internal.include?(int) + end + end + + def internal_present_or_active(headers, inactive_requirements = {}) + hint = headers & all_internal_attrs + hext = headers - hint + int_present = hint + hext.map {|e| fields_mapper.to_internal(e)}.compact + + update_inactive = Proc.new do + inactive_requirements.dup.each do |attr, req| + if req.active?(*int_present) + inactive_requirements.delete(attr) + int_present << attr + update_inactive.call + end + end + end + + required_attrs.values.each do |req| + next if int_present.include?(req) + if req.active?(*int_present) + inactive_requirements.delete(req.attr) + int_present << req.attr + update_inactive.call + else + inactive_requirements[req.attr] = req + end + end + int_present + end + def known_headers @known_headers ||= fields_mapper.list(:external).compact end def fields_mapper @@ -119,10 +151,14 @@ out[ra.attr] = ra end end def all_internal_attrs - person_parser.all_attrs(include_defined_parsers: true) + @all_internal_attrs ||= [].tap do |int_attrs| + known_int_attrs = person_parser.all_attrs(include_defined_parsers: true) + known_int_attrs |= fields_mapper.list(:internal).compact + int_attrs.concat(known_int_attrs) + end end def person_parser session.entry_factory.person_parser end