Sha256: b26ac43c79f062429562c223958c2193a41e8806594b993f33a71edaed6bf8ec

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

class Eco::API::UseCases::DefaultCases::CleanUnknownTags < Eco::API::Common::Loaders::UseCase
  name "clean-unknown-tags"
  type :transform

  UNKNOWN_LOCS_FILE = "unknown_locations.txt"

  REGISTER_TAGS = [
    "EVENT", "INJURY", "RISK", "CONTRACTOR", "PERMIT",
    "AUDIT", "JSEA",
    "TRAINING", "INDUCTION",
    "MEETING",  "PPE",  "CHEMICAL",
    "PLANT", "ASSET",
    "POLICY", "IDEA", "REPORTS"
  ]

  def main(people, session, options, usecase)
    update   = session.new_job("main", "update", :update, usecase)
    people.each do |person|
      unknown_tags = person.filter_tags.select {|tag| !tag?(tag)}
      person.filter_tags -= unknown_tags
      unknown_tag!(*unknown_tags)
      if (account = person.account) && tag = account.default_tag
        unless tag?(tag)
          micro.refresh_default_tag(person.entry, person, options)
          unknown_tag!(tag)
        end
      end
      update.add(person)
    end
  end

  private

  def unknown_tag!(*tags)
    tags.each do |value|
      unless unknown.include?(value)
        unknown << value
        File.open(unknown_tags_file, 'a') do |fd|
          fd.write("#{value}\n")
        end
        puts "Custom detected: '#{value}'"
      end
    end
  end

  def register_tags
    REGISTER_TAGS
  end

  def unknown
    @unknown ||= []
  end

  def tag?(value)
    tagtree.tag?(value).yield_self do |is_loc|
      next true if is_loc
      if clean_register_tags?
        register_tags.any? {|reg| value == reg}
      end
    end
  end

  def tagtree
    @tagtree ||= ASSETS.config.tagtree
  end

  def unknown_tags_file
    File.join(".","#{session.enviro_name}_#{UNKNOWN_LOCS_FILE}")
  end

  def clean_register_tags?
    !options.dig(:usecase, :include_register_tags)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
eco-helpers-2.5.10 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.9 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.8 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.7 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.6 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.5 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.4 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb
eco-helpers-2.5.3 lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb