Sha256: 7955b74f93eea87770fe4d91652811843ff45588d8b29e2b01459627c65d3a8b

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

module RedisImporter
  class RedisImporter
    include GemConfigurator

    attr_reader :files, :commands, :errors
    attr_accessor :collection

    def initialize
      configure
      self.collection = Object::const_get("#{@settings[:storage_method].camelcase}Collection").new()
      
      self.files = self.collection.files
      self.commands = []
    end
    
    def import
      files.each do |file|
        begin
          convert_to_redis_commands(file) if class_exists?(file.to_class_name)
        rescue NameError
          @errors ||= []
          @errors << "#{file.name} is not matched by a class #{file.to_class_name} in the system."
        end
      end
      pipeline
    end
    
    private

    attr_writer :files, :commands

    def class_exists?(c)
      Module.const_get(c)
    end

    def convert_to_redis_commands(file)
      local_path = local_storage_path(file)
      file.save_to(local_path)
      convert_objects_to_redis_commands(get_objects(local_path))
    end

    def default_settings
      {:storage_method => 's3', :local_storage_directory => 'tmp'}
    end

    def get_objects(local_path)
      CsvToObject::CsvToObject.new(local_path).to_objects
    end
    
    def convert_objects_to_redis_commands(objects)
      objects.each do |obj|
        self.commands << obj.to_redis
      end
    end
    
    def local_storage_path(file)
      "#{@settings[:local_storage_directory]}/#{file.name}"
    end

    def pipeline
      if !self.commands.empty?
        pipeline = RedisPipeline::RedisPipeline.new
        pipeline.add_commands(self.commands.flatten)
        pipeline.execute_commands
      else
        self.commands
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis_importer-0.0.7 lib/redis_importer/redis_importer.rb
redis_importer-0.0.6 lib/redis_importer/redis_importer.rb