<commit_before>require 'delegate'
require 'singleton'

#TODO: Use logger and log throughout
#TODO: Counters and timers

module Squealer
  class Target

    def self.current
      Queue.instance.current
    end

    def initialize(database_connection, table_name, row_id=nil, &block)
      raise BlockRequired, "Block must be given to target (otherwise, there's no work to do)" unless block_given?
      raise ArgumentError, "Table name must be supplied" if table_name.to_s.strip.empty?

      @table_name = table_name.to_s
      @row_id = obtain_row_id(row_id, &block)
      @column_names = []
      @column_values = []
      @sql = ''

      target(&block)
    end

    def sql
      @sql
    end

    def assign(column_name, &block)
      raise BlockRequired, "At least specify an empty block, like this:\n  assign(:#{column_name}) {}" unless block_given?
      @column_names << column_name
      @column_values << (yield || infer_value(column_name, &block))
    end


    private

    def obtain_row_id(row_id, &block)
      #TODO: Remove in version 1.1
      if row_id != nil
        puts "\033[33mWARNING - squealer:\033[0m the 'target' row_id parameter is deprecated and will be invalid in version 1.1.x. Remove it, and ensure the table_name matches a variable containing a hashmap with an _id key"
        row_id
      else
        infer_row_id(&block)
      end
    end

    def infer_row_id(&block)
      block.binding.eval "#{@table_name}._id"<commit_msg>Fix deprecation warning to version 1.3 - give them some time ;-)<commit_after>require 'delegate'
require 'singleton'

#TODO: Use logger and log throughout
#TODO: Counters and timers

module Squealer
  class Target

    def self.current
      Queue.instance.current
    end

    def initialize(database_connection, table_name, row_id=nil, &block)
      raise BlockRequired, "Block must be given to target (otherwise, there's no work to do)" unless block_given?
      raise ArgumentError, "Table name must be supplied" if table_name.to_s.strip.empty?

      @table_name = table_name.to_s
      @row_id = obtain_row_id(row_id, &block)
      @column_names = []
      @column_values = []
      @sql = ''

      target(&block)
    end

    def sql
      @sql
    end

    def assign(column_name, &block)
      raise BlockRequired, "At least specify an empty block, like this:\n  assign(:#{column_name}) {}" unless block_given?
      @column_names << column_name
      @column_values << (yield || infer_value(column_name, &block))
    end


    private

    def obtain_row_id(row_id, &block)
      #TODO: Remove in version 1.3 - just call infer_row_id in initialize
      if row_id != nil
        puts "\033[33mWARNING - squealer:\033[0m the 'target' row_id parameter is deprecated and will be invalid in version 1.3 and above. Remove it, and ensure the table_name matches a variable containing a hashmap with an _id key"
        row_id
      else
        infer_row_id(&block)
      end
    end

    def infer_row_id(&block)
      block.binding.eval "#{@table_name}._id"
{'fraction_non_alphanumeric': 0.08883751651254954, 'fraction_numerical': 0.009247027741083224, 'mean_word_length': 2.9907773386034258, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4474754', 'n_tokens_mistral': 972, 'n_tokens_neox': 925, 'n_words': 300}