class Godredis::Base

Subclass Godredis base class to collect Redis-related objects and use simple DSL to set mapping for the common methods such as quit or reconnect if they are different from the defaults.

class CacheStoreGodredis < Godredis::Base
  redis ->{ Rails.cache.instance_variable_get('@data') }
end

class ObjectsGodredis < Godredis::Base
  redis ->{ Redis::Objects.redis }
end

class SidekiqGodredis < Godredis::Base
  # define mappings with blocks or lambdas
  redis  ->(&block){ Sidekiq.redis &block }
  client { redis &:client }
  quit   { redis &:quit }
end

Default mapping:

redis      { Redis.current }
client     { redis.client }
connected? { client.connected? }
reconnect  { client.reconnect.connected? }
quit       { redis.quit }

You may also add custom commands:

class SomeGodredis < Godredis::Base
  del_some_key { redis.del('some_key') }
end

Godredis.redises(&:del_some_key!)
# etc...

Every commands (except question-marked) also has a banged wrapper command!, which calls an itself command and puts a short message about its execution result

Public Class Methods

tag() click to toggle source
# File lib/godredis.rb, line 91
def tag
  @tag ||= name.demodulize[/^(.+?)((?:god)?redis)?$/i, 1].underscore
end

Protected Class Methods

method_missing(method, proc = nil, *args, &block) click to toggle source
Calls superclass method
# File lib/godredis.rb, line 96
def method_missing(method, proc = nil, *args, &block)
  if block_given? || proc.respond_to?(:to_proc)
    define_method method, &(block || proc)
  else
    super
  end
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/godredis.rb, line 112
def method_missing(method, *args, &block)
  if /^(?<action>.+[^\?])!$/ =~ method.to_s && respond_to?(action)
    say(action) { send action }
  else
    super
  end
end

Private Instance Methods

get_short_status_calling(&block) click to toggle source
# File lib/godredis.rb, line 128
def get_short_status_calling(&block)
  result = begin
    block.call
  rescue => e
    "[FAIL] #{e.message}"
  end
  result == true ? '[OK]' : result || "[FAIL]"
end
say(action, &block) click to toggle source
# File lib/godredis.rb, line 123
def say(action, &block)
  result = get_short_status_calling(&block)
  puts "Redis [#{tag}]: #{action}... #{result}"
end