<commit_before># frozen_string_literal: true

require 'active_support'
require 'active_support/inflector'
require 'wisper'

module Ditty
  class Listener
    EVENTS = %i[
      component_list component_create component_read component_update component_delete
      identity_register identity_login identity_logout identity_failed_login identity_update_password identity_update_password_failed
    ]

    def initialize
      @mutex = Mutex.new
    end

    def method_missing(method, *args)
      return super unless args[0].is_a?(Hash) && args[0].has_key?(:target)

      return unless args[0][:target].settings.track_actions
      args[0][:values] ||= {}
      values = {
        user: args[0][:target].current_user,
        action: action_from(args[0][:target], method),
        details: args[0][:details]
      }.merge(args[0][:values])
      log_action values
    end

    def respond_to_missing?(method, _include_private = false)
      EVENTS.include? method
    end

    def action_from(target, method)<commit_msg>refac: Clean up ditty listener<commit_after># frozen_string_literal: true

require 'active_support'
require 'active_support/inflector'
require 'wisper'

module Ditty
  class Listener
    EVENTS = %i[
      component_list component_create component_read component_update component_delete
      identity_register identity_login identity_logout identity_failed_login identity_update_password
      identity_update_password_failed
    ].freeze

    def initialize
      @mutex = Mutex.new
    end

    def method_missing(method, *args)
      return unless args[0].is_a?(Hash) && args[0][:target] && args[0][:target].settings.track_actions

      log_action({
        user: args[0][:target].current_user,
        action: action_from(args[0][:target], method),
        details: args[0][:details]
      }.merge(args[0][:values] || {}))
    end

    def respond_to_missing?(method, _include_private = false)
      EVENTS.include? method
    end

    def action_from(target, method)
{'fraction_non_alphanumeric': 0.09884596086302057, 'fraction_numerical': 0.007526342197691922, 'mean_word_length': 3.8872549019607843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4586839', 'n_tokens_mistral': 602, 'n_tokens_neox': 594, 'n_words': 128}