Sha256: e96bb98c564b99953c20f18c2e990d9ee2350f094425b34c45ffcc50fe1f7cd1

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "polo/sql_translator"
require "polo/configuration"

module Polo
  class Translator

    # Public: Creates a new Polo::Collector
    #
    # selects - An array of SELECT queries
    #
    def initialize(selects, configuration=Configuration.new)
      @selects = selects
      @configuration = configuration
    end

    # Public: Translates SELECT queries into INSERTS.
    #
    def translate
      SqlTranslator.new(instances, @configuration).to_sql.uniq
    end

    def instances
      active_record_instances = @selects.flat_map do |select|
        select[:klass].find_by_sql(select[:sql]).to_a
      end

      if fields = @configuration.blacklist
        obfuscate!(active_record_instances, fields.map(&:to_s))
      end

      active_record_instances
    end

    private

    def obfuscate!(instances, fields)
      instances.each do |instance|
        next if (instance.attributes.keys & fields).empty?
        fields.each do |field|
          value = instance.attributes[field] || ''
          instance.send("#{field}=", value.split('').shuffle.join)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polo-0.1.0 lib/polo/translator.rb