Module: RedisFixtures

Defined in:
lib/redis_fixtures.rb,
lib/redis_fixtures/config.rb,
lib/redis_fixtures/version.rb,
lib/redis_fixtures/connection.rb,
lib/redis_fixtures/dump_fixtures.rb,
lib/redis_fixtures/load_fixtures.rb

Overview

Add fixtures to your Redis database, to test the parts of your code that need Redis to be more than a cache.

Defined Under Namespace

Classes: Configuration, InvalidConfigSettingError

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.before_fixture_data_generationObject

Cleans up the Redis DB. Call this before starting your sample data generation to start with a clean slate



4
5
6
7
8
# File 'lib/redis_fixtures/dump_fixtures.rb', line 4

def self.before_fixture_data_generation
  with_redis_connection do |redis|
    redis.flushdb
  end
end

.configurationConfiguration

Returns the current configuration

Returns:



42
43
44
# File 'lib/redis_fixtures/config.rb', line 42

def self.configuration
  @configuration ||=  Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields the current configuration, allowing the caller to modify it in a block

Yields:



47
48
49
# File 'lib/redis_fixtures/config.rb', line 47

def self.configure
  yield(configuration) if block_given?
end

.fixture_file_pathString

Path to the file where we’ll store Redis fixtures

Returns:

  • (String)


53
54
55
# File 'lib/redis_fixtures/config.rb', line 53

def self.fixture_file_path
  fixtures_dir(RedisFixtures.configuration.fixture_filename)
end

.load_fixturesObject

Load the Redis fixture YAML file, into Redis, flushing the DB first



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/redis_fixtures/load_fixtures.rb', line 3

def self.load_fixtures
  return unless File.exists?(fixture_file_path)
  commands = YAML.load_file(fixture_file_path)
  with_redis_connection do |redis|
    redis.pipelined do |predis|
      predis.flushdb
      commands.each do |command|
        predis.send(*command)
      end
    end
  end
end

.save_fixturesObject

Dumps the contents of the Redis DB into a fixture file. Call this after generating all your sample data in Redis.



12
13
14
15
16
17
18
# File 'lib/redis_fixtures/dump_fixtures.rb', line 12

def self.save_fixtures
  redis_dump = with_redis_connection do |redis|
    dump_keys(redis)
  end
  FileUtils.mkdir_p(fixtures_dir) unless File.directory?(fixtures_dir)
  File.open(fixture_file_path, 'w') { |file| file.write(redis_dump.to_yaml) }
end

.with_redis_connectionObject

Gets a connection to Redis using whatever method was configured, and yields it to the block passed in

Returns:

  • (Object)

    Whatever your block returned



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/redis_fixtures/connection.rb', line 5

def self.with_redis_connection
  result = nil
  conf = RedisFixtures.configuration
  if conf.connection_pool.present?
    conf.connection_pool.with do |redis|
      result = yield(redis)
    end
  elsif conf.connection.present?
    result = yield(conf.connection)
  elsif conf.connection_proc.present? || conf.connection_settings.present?
    redis = conf.connection_proc.present? ?
              conf.connection_proc.call :
              Redis.new(conf.connection_settings)
    result = yield(redis)
    redis.disconnect! if redis.respond_to?(:disconnect!)
  end
  result
end