class Slnky::Brain::Base

Public Class Methods

connect(config) click to toggle source
# File lib/slnky/brain.rb, line 17
def connect(config)
  @brain = self.new(config)
end
instance() click to toggle source
# File lib/slnky/brain.rb, line 13
def instance
  @brain ||= self.new(Slnky.config.redis.to_h)
end
new(redis={}) click to toggle source
# File lib/slnky/brain.rb, line 22
def initialize(redis={})
  @options = {
      host: '127.0.0.1',
      port: 6379,
      user: nil,
      pass: nil,
      db: '15'
  }
  @options.merge!(redis) if redis
  userpass = @options[:user] ? "#{@options[:user]}:#{@options[:pass]}" : ''
  @redis = Redis.new(url: "redis://#{userpass}#{@options[:host]}:#{@options[:port]}/#{@options[:db]}")
end

Public Instance Methods

hget(key, field) click to toggle source
# File lib/slnky/brain.rb, line 56
def hget(key, field)
  val = @redis.hget("slnky.#{key}", field)
  begin
    JSON.parse(val)
  rescue
    val
  end
end
hgetall(key) click to toggle source
# File lib/slnky/brain.rb, line 65
def hgetall(key)
  keys = @redis.hkeys("slnky.#{key}")
  keys.inject({}) {|h, e| h[e] = hget(key, e); h}
end
hset(key, field, value) click to toggle source

def keys(pattern=‘*’)

@redis.keys(pattern)

end

def set(key, value)

@redis.set key, value.is_a?(String) ? value : value.to_json

end

def get(key)

val = @redis.get key
begin
  JSON.parse(val)
rescue
  val
end

end

# File lib/slnky/brain.rb, line 52
def hset(key, field, value)
  @redis.hset("slnky.#{key}", field, value.is_a?(String) ? value : value.to_json)
end