Class: Lita::Alias::AliasStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/alias/alias_store.rb

Overview

Repository of aliases. Stores the aliases in a hash in redis keyed by the alias name

Defined Under Namespace

Classes: AliasAddException, AliasNotFoundException

Constant Summary collapse

STORE_KEY =
'aliases'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ AliasStore

Returns a new instance of AliasStore.



9
10
11
# File 'lib/lita/alias/alias_store.rb', line 9

def initialize(redis)
  @redis = redis
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



7
8
9
# File 'lib/lita/alias/alias_store.rb', line 7

def redis
  @redis
end

Instance Method Details

#add(aliased_command) ⇒ Object

Store the alias.

Returns:

  • AliasedCommand The saved alias



25
26
27
28
29
# File 'lib/lita/alias/alias_store.rb', line 25

def add(aliased_command)
  fail AliasAddException, "The alias is not valid" unless aliased_command.valid?

  @redis.hset(STORE_KEY, aliased_command.name, aliased_command.command)
end

#delete(name) ⇒ Object

Delete the alias with name



14
15
16
17
18
19
20
# File 'lib/lita/alias/alias_store.rb', line 14

def delete(name)
  fail AliasNotFoundException, "No alias with name #{name} exists" unless @redis.hexists(STORE_KEY, name)

  command = @redis.hget(STORE_KEY, name)
  @redis.hdel(STORE_KEY, name)
  AliasedCommand.new(name, command).freeze
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/lita/alias/alias_store.rb', line 44

def exists?(name)
  @redis.hexists(STORE_KEY, name)
end

#listObject

Return a list of all aliases



33
34
35
36
37
# File 'lib/lita/alias/alias_store.rb', line 33

def list
  @redis.hgetall(STORE_KEY).sort.map do |name, command|
    AliasedCommand.new(name, command)
  end
end

#lookup(name) ⇒ Object



39
40
41
42
# File 'lib/lita/alias/alias_store.rb', line 39

def lookup(name)
  command = @redis.hget(STORE_KEY, name)
  AliasedCommand.new(name, command)
end