Sha256: 84b1b7347b2046c8657e2f979ce9e49076b38562774ac1156e218827d30a5d17

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

require 'redis'

class RedisWidget

  def self.redis
    threaded ||= Redis.new
  end

  def self.redis=(connection)
    threaded = connection
  end

  def self.threaded
    Thread.current[self.class.to_s] ||= {}
  end

  def initialize(options = {})
    options = options.dup
    @name = options[:name]
  end

  def connection
    self.class.redis
  end

  def save
    unless connection.get(self.class.to_s + ':id')
      @id = 0
      connection.set(self.class.to_s + ':id', @id)
    end
    @id = connection.incr(self.class.to_s + ':id')
    connection.set(self.class.to_s + ':%d:name' % @id, @name)
  end

  def self.count
    self.redis.keys(self.to_s + '*name').size
  end

  def self.create!
    new(:name => 'some widget').save

  end
end

class RedisWidgetUsingDatabaseOne < RedisWidget

  def self.redis
    threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_ONE']
  end

  def self.create!
    new(:name => 'a widget using database one').save
  end
end

class RedisWidgetUsingDatabaseTwo < RedisWidget

  def self.redis
    threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_TWO']
  end

  def self.create!
    new(:name => 'a widget using database two').save
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
database_cleaner-1.6.2 examples/lib/redis_models.rb
database_cleaner-1.6.1 examples/lib/redis_models.rb
database_cleaner-1.6.0 examples/lib/redis_models.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/examples/lib/redis_models.rb
database_cleaner-1.5.3 examples/lib/redis_models.rb
database_cleaner-1.5.2 examples/lib/redis_models.rb