lib/eco/api/common/people/entry_factory.rb in eco-helpers-1.5.0 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-1.5.1
- old
+ new
@@ -2,25 +2,25 @@
module API
module Common
module People
# Helper factory class to generate entries (input entries).
# @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory
- # @attr_reader person_parser [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
- class EntryFactory
+ class EntryFactory < Eco::API::Common::Session::BaseSession
attr_reader :schema, :person_parser
+ # @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(schema:, person_parser: nil, attr_map: nil, logger: ::Logger.new(IO::NULL))
+ 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)
- @logger = logger
@schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
@source_person_parser = person_parser
# load default parsers
@@ -32,10 +32,11 @@
@attr_map = attr_map
end
# provides with a Eco::API::Common::People::PersonParser object (collection of attribute parsers)
# @note if the custom person parser has changed, it updates the copy of this EntryFactory instance
+ # @return [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
def person_parser
if @person_parser_patch_version < @source_person_parser.patch_version
@person_parser.merge(@source_person_parser)
@person_parser_patch_version = @source_person_parser.patch_version
end
@@ -50,11 +51,11 @@
PersonEntry.new(
data,
person_parser: person_parser,
attr_map: @attr_map,
dependencies: dependencies,
- logger: @logger
+ logger: logger
)
end
# Helper that provides a collection of `Entries`, which in turn provides with further helpers to find and exclude entries.
# It accepts a `file:` and `format:` or `data:` but not both options together.
@@ -79,11 +80,11 @@
if Eco::API::Common::Session::FileManager.file_exists?(file)
encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
file_content = File.read(file, encoding: encoding)
arr_hash = person_parser.parse(format, file_content)
else
- @logger.warn("File does not exist: #{file}")
+ logger.warn("File does not exist: #{file}")
end
entries(data: arr_hash)
else
Entries.new(data, klass: PersonEntry, factory: self)
@@ -129,10 +130,10 @@
end
private
def fatal(msg)
- @logger.fatal(msg)
+ logger.fatal(msg)
raise msg
end
end