<commit_before>def clipboard_scope_char
  '__'
end
def class_scope_char
  '/'
end

class String

  def clipboardify
    self.gsub(class_scope_char, clipboard_scope_char)
  end

  def de_clipboardify
    self.gsub(clipboard_scope_char, class_scope_char)
  end

  def clipboardify!
    self.gsub!(class_scope_char, clipboard_scope_char)
  end
  
  def de_clipboardify!
    self.gsub!(clipboard_scope_char, class_scope_char)
  end
end

module Clipboard
  
  def get_clipboard_object(clipboard_data_type, clipboard_data_id)
    results = nil
    if clipboard_data_type && clipboard_data_type != 'null' && clipboard_data_id
      clipboard_data_type.de_clipboardify!
      begin
        clazz = clipboard_data_type.classify.constantize
        obj_array = clazz.find(:all, :conditions => {:id => clipboard_data_id})
        results = obj_array.empty? ? nil : obj_array.first
      rescue NameError
        error_message = "unkown object in clipboard %s (id:%d)" % [clipboard_data_type,id]
        logger.warn(error_message)
        flash[:warn]=error_message
      end
    end
    return results
  end
  
  def clipboard_object(check_here_first={})
    clipboard_data_type  = check_here_first[:clipboard_data_type] || cookies[:clipboard_data_type]
    clipboard_data_id    = check_here_first[:clipboard_data_id]   || cookies[:clipboard_data_id]
    get_clipboard_object(clipboard_data_type, clipboard_data_id)
  end

  def clipboard_object_name(options={})
    obj = clipboard_object(options)
    if obj
      if obj.respond_to? :name
        return obj.name
      else
        return obj.class.name.humanize
      end
    end
    return "(unknown object)"
  end
  
  
  def paste_link_for(acceptable_types,options={})
    clipboard_data_type  = options[:clipboard_data_type] || cookies[:clipboard_data_type]
    clipboard_data_id    = options[:clipboard_data_id]   || cookies[:clipboard_data_id]
    container_id         = options[:container_id] || params[:container_id]
    
    return "<span class='copy_paste_disabled'>paste (nothing in clipboard)</span>" unless clipboard_data_type
    name = clipboard_object_name
    if acceptable_types.include?(clipboard_data_type) 
      url = url_for(
        :action => 'paste', 
        :method=> 'post', 
        :clipboard_data_type => clipboard_data_type, 
        :clipboard_data_id => clipboard_data_id, 
        :id =>container_id)
      return remote_link_button("paste-out.png", 
        :url => url, 
        :title => "paste #{name}") + link_to_remote("paste #{name}", 
        :url=>url)
    end
    return "<span class='copy_paste_disabled'>cant paste #{clipboard_data_type} #{name} here</span>"
  end
  
end<commit_msg>second attempt: dry up clipboard string conversion<commit_after>module Clipboard
  
  module StringExtensions
      def clipboard_scope_char
        '__'
      end
      def class_scope_char
        '/'
      end
    
      def clipboardify
        self.gsub(class_scope_char, clipboard_scope_char)
      end

      def de_clipboardify
        self.gsub(clipboard_scope_char, class_scope_char)
      end

      def clipboardify!
        self.gsub!(class_scope_char, clipboard_scope_char)
      end

      def de_clipboardify!
        self.gsub!(clipboard_scope_char, class_scope_char)
      end
  end
  
  def get_clipboard_object(clipboard_data_type, clipboard_data_id)
    results = nil
    if clipboard_data_type && clipboard_data_type != 'null' && clipboard_data_id
      clipboard_data_type.de_clipboardify!
      begin
        clazz = clipboard_data_type.classify.constantize
        obj_array = clazz.find(:all, :conditions => {:id => clipboard_data_id})
        results = obj_array.empty? ? nil : obj_array.first
      rescue NameError
        error_message = "unkown object in clipboard %s (id:%d)" % [clipboard_data_type,id]
        logger.warn(error_message)
        flash[:warn]=error_message
      end
    end
    return results
  end
  
  def clipboard_object(check_here_first={})
    clipboard_data_type  = check_here_first[:clipboard_data_type] || cookies[:clipboard_data_type]
    clipboard_data_id    = check_here_first[:clipboard_data_id]   || cookies[:clipboard_data_id]
    get_clipboard_object(clipboard_data_type, clipboard_data_id)
  end

  def clipboard_object_name(options={})
    obj = clipboard_object(options)
    if obj
      if obj.respond_to? :name
        return obj.name
      else
        return obj.class.name.humanize
      end
    end
    return "(unknown object)"
  end
  
  
  def paste_link_for(acceptable_types,options={})
    clipboard_data_type  = options[:clipboard_data_type] || cookies[:clipboard_data_type]
    clipboard_data_id    = options[:clipboard_data_id]   || cookies[:clipboard_data_id]
    container_id         = options[:container_id] || params[:container_id]
    
    return "<span class='copy_paste_disabled'>paste (nothing in clipboard)</span>" unless clipboard_data_type
    name = clipboard_object_name
    if acceptable_types.include?(clipboard_data_type) 
      url = url_for(
        :action => 'paste', 
        :method=> 'post', 
        :clipboard_data_type => clipboard_data_type, 
        :clipboard_data_id => clipboard_data_id, 
        :id =>container_id)
      return remote_link_button("paste-out.png", 
        :url => url, 
        :title => "paste #{name}") + link_to_remote("paste #{name}", 
        :url=>url)
    end
    return "<span class='copy_paste_disabled'>cant paste #{clipboard_data_type} #{name} here</span>"
  end
end

class String
  include Clipboard::StringExtensions
end
{'fraction_non_alphanumeric': 0.09154929577464789, 'fraction_numerical': 0.0, 'mean_word_length': 3.3925455987311657, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, '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': '2798883', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1784, 'n_words': 324}