Sha256: 2b769cb7708cc5e3e6834807a82f0681876caed33ddf915a8234516458390d6d

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 KB

Contents

require 'redis'

class RedisWidget

  def self.redis
    threaded ||= Redis.connect
  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.connect :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.connect :url => ENV['REDIS_URL_TWO']
  end

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

Version data entries

18 entries across 13 versions & 3 rubygems

Version Path
database_cleaner-1.5.1 examples/lib/redis_models.rb
database_cleaner-1.5.0 examples/lib/redis_models.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/database_cleaner-1.4.1/examples/lib/redis_models.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/database_cleaner-1.4.1/examples/lib/redis_models.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/database_cleaner-1.4.1/examples/lib/redis_models.rb
database_cleaner-1.4.1 examples/lib/redis_models.rb
database_cleaner-1.4.0 examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/database_cleaner-1.2.0/examples/lib/redis_models.rb
database_cleaner-1.3.0 examples/lib/redis_models.rb
database_cleaner-1.2.0 examples/lib/redis_models.rb
database_cleaner-1.1.1 examples/lib/redis_models.rb
database_cleaner-1.1.0 examples/lib/redis_models.rb
database_cleaner-1.0.1 examples/lib/redis_models.rb