lib/eco/api/common/people/entry_factory.rb in eco-helpers-1.5.1 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-1.5.2

- old
+ new

@@ -10,25 +10,23 @@ # @param e [Eco::API::Common::Session::Environment] requires a session environment, as any child of `Eco::API::Common::Session::BaseSession` # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon. # @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers. # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper to translate external names into internal ones and _vice versa_. - # @param logger [Common::Session::Logger, ::Logger] object to manage logs. def initialize(e, schema:, person_parser: nil, attr_map: nil) fatal "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema) fatal "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser) fatal "Expecting Mapper object. Given: #{fields_mapper}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper) super(e) @schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json)) - @source_person_parser = person_parser - # load default parsers - @person_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema) - # merge the custom parsers - @person_parser = @person_parser.merge(@source_person_parser) + # load default parser + custom parsers + base_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema).merge(@source_person_parser) + # new parser with linked schema + @person_parser = @source_person_parser.new(schema: @schema).merge(base_parser) @person_parser_patch_version = @source_person_parser.patch_version @attr_map = attr_map end @@ -77,12 +75,16 @@ if file arr_hash = [] if Eco::API::Common::Session::FileManager.file_exists?(file) encoding ||= Eco::API::Common::Session::FileManager.encoding(file) + encoding = (encoding != "utf-8")? "#{encoding}|utf-8": encoding file_content = File.read(file, encoding: encoding) - arr_hash = person_parser.parse(format, file_content) + arr_hash = person_parser.parse(format, file_content).map.each_with_index do |entry_hash, i| + j = (format == :csv)? i + 2 : i + 1 + entry_hash.tap {|hash| hash["idx"] = j} + end else logger.warn("File does not exist: #{file}") end entries(data: arr_hash) @@ -117,10 +119,10 @@ if run deps = {"supervisor_id" => {people: data}} data_entries = data.map do |person| - self.new(person, dependencies: deps).to_hash + self.new(person, dependencies: deps).external_entry end File.open(file, "w", enconding: encoding) do |fd| fd.write(person_parser.serialize(format, data_entries)) end