<commit_before>require 'monitor'
require 'securerandom'

module OmfCommon
  class Comm
    class Topic

      @@name2inst = {}
      @@lock = Monitor.new

      def self.create(name, opts = {}, &block)
        name = name.to_sym
        @@lock.synchronize do
          unless t = @@name2inst[name]
            #opts[:address] ||= address_for(name)
            t = @@name2inst[name] = self.new(name, opts, &block)
          end
          t
        end
      end

      def self.[](name)
        @@name2inst[name]
      end

      attr_reader :id

      # Request the creation of a new resource. Returns itself
      #
      def create(res_type, config_props = {}, &block)
        # new_res = nil
        #res_name = res_name.to_sym
        #config_props[:name] ||= res_name
        config_props[:type] ||= res_type
        debug "Create resource of type '#{res_type}'"
        create_message_and_publish(:create, config_props, block)
        self
      end

      def configure(props = {}, &block)
        create_message_and_publish(:configure, props, block)
        self
      end
<commit_msg>Execute callback with topic instance in Topic.create if topic existing<commit_after>require 'monitor'
require 'securerandom'

module OmfCommon
  class Comm
    class Topic

      @@name2inst = {}
      @@lock = Monitor.new

      def self.create(name, opts = {}, &block)
        name = name.to_sym
        @@lock.synchronize do
          unless @@name2inst[name]
            debug "New topic: #{name}"
            #opts[:address] ||= address_for(name)
            @@name2inst[name] = self.new(name, opts, &block)
          else
            debug "Existing topic: #{name}"
            block.call(@@name2inst[name]) if block
          end
          @@name2inst[name]
        end
      end

      def self.[](name)
        @@name2inst[name]
      end

      attr_reader :id

      # Request the creation of a new resource. Returns itself
      #
      def create(res_type, config_props = {}, &block)
        # new_res = nil
        #res_name = res_name.to_sym
        #config_props[:name] ||= res_name
        config_props[:type] ||= res_type
        debug "Create resource of type '#{res_type}'"
        create_message_and_publish(:create, config_props, block)
        self
      end

      def configure(props = {}, &block)
        create_message_and_publish(:configure, props, block)
        self
      end

{'fraction_non_alphanumeric': 0.10833333333333334, 'fraction_numerical': 0.004166666666666667, 'mean_word_length': 2.0625, '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': '3418901', 'n_tokens_mistral': 737, 'n_tokens_neox': 707, 'n_words': 181}