<commit_before>module WebsocketRails
  class Channel

    include Logging

    delegate :config, :channel_tokens, :channel_manager, :to => WebsocketRails

    attr_reader :name, :subscribers

    def initialize(channel_name)
      @subscribers = []
      @name        = channel_name
      @private     = false
    end

    def subscribe(connection)
      info "#{connection} subscribed to channel #{name}"
      trigger 'subscriber_join', connection.user if config.broadcast_subscriber_events?
      @subscribers << connection
      send_token connection
    end

    def unsubscribe(connection)
      return unless @subscribers.include? connection
      info "#{connection} unsubscribed from channel #{name}"
      @subscribers.delete connection
      trigger 'subscriber_part', connection.user if config.broadcast_subscriber_events?
    end

    def trigger(event_name,data={},options={})
      options.merge! :channel => name, :token => token
      options[:data] = data

      event = Event.new event_name, options

      info "[#{name}] #{event.data.inspect}"
      send_data event
    end

    def trigger_event(event)
      return if event.token != token
      info "[#{name}] #{event.data.inspect}"
      send_data event
    end

    def make_private
      unless config.keep_subscribers_when_private?
        @subscribers.clear
      end
      @private = true
    end

    def is_private?
      @private
    end

    def token
      @token ||= channel_tokens[@name] ||= generate_unique_token
    end

    private

    def generate_unique_token
      begin
        new_token = SecureRandom.uuid
      end while channel_tokens.values.include?(new_token)

      new_token
    end

    def send_token(connection)
      options = {
        :channel => @name,
        :data => {:token => token},
        :connection => connection
      }
      info 'sending token'
      Event.new('websocket_rails.channel_token', options).trigger
    end

    def send_data(event)
      if WebsocketRails.synchronize? && event.server_token.nil?
        Synchronization.publish event
      end

      subscribers.each do |subscriber|
        subscriber.trigger event
      end
    end

  end
end
<commit_msg>Using attributes directly, instead of call `self.attr_name` methods.<commit_after>module WebsocketRails
  class Channel

    include Logging

    delegate :config, :channel_tokens, :channel_manager, :to => WebsocketRails

    attr_reader :name, :subscribers

    def initialize(channel_name)
      @subscribers = []
      @name        = channel_name
      @private     = false
    end

    def subscribe(connection)
      info "#{connection} subscribed to channel #{@name}"
      trigger 'subscriber_join', connection.user if config.broadcast_subscriber_events?
      @subscribers << connection
      send_token connection
    end

    def unsubscribe(connection)
      return unless @subscribers.include? connection
      info "#{connection} unsubscribed from channel #{@name}"
      @subscribers.delete connection
      trigger 'subscriber_part', connection.user if config.broadcast_subscriber_events?
    end

    def trigger(event_name,data={},options={})
      options.merge! :channel => @name, :token => token
      options[:data] = data

      event = Event.new event_name, options

      info "[#{@name}] #{event.data.inspect}"
      send_data event
    end

    def trigger_event(event)
      return if event.token != token
      info "[#{@name}] #{event.data.inspect}"
      send_data event
    end

    def make_private
      unless config.keep_subscribers_when_private?
        @subscribers.clear
      end
      @private = true
    end

    def is_private?
      @private
    end

    def token
      @token ||= channel_tokens[@name] ||= generate_unique_token
    end

    private

    def generate_unique_token
      begin
        new_token = SecureRandom.uuid
      end while channel_tokens.values.include?(new_token)

      new_token
    end

    def send_token(connection)
      options = {
        :channel => @name,
        :data => {:token => token},
        :connection => connection
      }
      info 'sending token'
      Event.new('websocket_rails.channel_token', options).trigger
    end

    def send_data(event)
      if WebsocketRails.synchronize? && event.server_token.nil?
        Synchronization.publish event
      end

      @subscribers.each do |subscriber|
        subscriber.trigger event
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.08833445718138908, 'fraction_numerical': 0.0, 'mean_word_length': 2.6178861788617884, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '5500805', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1254, 'n_words': 317}