2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
module Import
|
|
|
|
module Helper
|
|
|
|
class AttributesExamples
|
|
|
|
attr_reader :examples, :enough, :max_unkown
|
|
|
|
|
|
|
|
def initialize(&block)
|
|
|
|
@max_unkown = 50
|
|
|
|
@no_new_counter = 1
|
|
|
|
@examples = {}
|
|
|
|
@known = []
|
|
|
|
|
|
|
|
# Support both builder styles:
|
|
|
|
#
|
|
|
|
# Import::Helper::AttributesExamples.new do
|
|
|
|
# extract(attributes)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# and
|
|
|
|
#
|
|
|
|
# Import::Helper::AttributesExamples.new do |extractor|
|
|
|
|
# extractor.extract(attributes)
|
|
|
|
# end
|
2020-11-24 16:20:57 +00:00
|
|
|
return if !block
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
if block.arity.zero?
|
|
|
|
instance_eval(&block)
|
|
|
|
else
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract(attributes)
|
|
|
|
unknown = attributes.keys - @known
|
|
|
|
|
|
|
|
return if !unknown?(unknown)
|
|
|
|
|
|
|
|
store(attributes, unknown)
|
|
|
|
|
|
|
|
@known.concat(unknown)
|
|
|
|
@no_new_counter = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def unknown?(unknown)
|
|
|
|
return true if unknown.present?
|
|
|
|
|
|
|
|
@no_new_counter += 1
|
|
|
|
|
|
|
|
# check max 50 entries with no or no new attributes in a row
|
2021-03-01 14:23:03 +00:00
|
|
|
@enough = @no_new_counter != 50
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def store(attributes, unknown)
|
|
|
|
unknown.each do |attribute|
|
|
|
|
value = attributes[attribute]
|
|
|
|
|
|
|
|
next if value.nil?
|
|
|
|
|
2018-06-01 11:32:59 +00:00
|
|
|
example = value.to_utf8(fallback: :read_as_sanitized_binary)
|
2021-05-12 11:37:44 +00:00
|
|
|
example.gsub!(%r{^(.{20,}?).*$}m, '\1...')
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
@examples[attribute] = "#{attribute} (e. g. #{example})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|