Class: Lita::Room
- Inherits:
-
Object
- Object
- Lita::Room
- Defined in:
- lib/lita/room.rb
Overview
A room in the chat service. Persisted in Redis.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The room’s unique ID.
-
#metadata ⇒ Hash
readonly
A hash of arbitrary metadata about the room.
-
#name ⇒ String
readonly
The room’s name as displayed in a standard user interface.
Class Method Summary collapse
-
.create_or_update(id, metadata = {}) ⇒ Lita::Room
Creates a new room with the given ID, or merges and saves supplied metadata to a room with the given ID.
-
.find_by_id(id) ⇒ Lita::Room?
Finds a room by ID.
-
.find_by_name(name) ⇒ Lita::Room?
Finds a room by display name.
-
.fuzzy_find(identifier) ⇒ Lita::Room?
Finds a room by ID or name.
-
.redis ⇒ Redis::Namespace
The
Redis::Namespace
for room persistence.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compares the room against another room object to determine equality.
-
#hash ⇒ Fixnum
Generates a
Fixnum
hash value for this user object. -
#initialize(id, metadata = {}) ⇒ Room
constructor
A new instance of Room.
-
#save ⇒ void
Saves the room record to Redis, overwriting any previous data for the current ID.
Constructor Details
#initialize(id, metadata = {}) ⇒ Room
Returns a new instance of Room.
66 67 68 69 70 |
# File 'lib/lita/room.rb', line 66 def initialize(id, = {}) @id = id.to_s @metadata = Util.stringify_keys() @name = @metadata["name"] || @id end |
Instance Attribute Details
#id ⇒ String (readonly)
The room’s unique ID.
53 54 55 |
# File 'lib/lita/room.rb', line 53 def id @id end |
#metadata ⇒ Hash (readonly)
A hash of arbitrary metadata about the room.
57 58 59 |
# File 'lib/lita/room.rb', line 57 def @metadata end |
#name ⇒ String (readonly)
The room’s name as displayed in a standard user interface.
61 62 63 |
# File 'lib/lita/room.rb', line 61 def name @name end |
Class Method Details
.create_or_update(id, metadata = {}) ⇒ Lita::Room
Creates a new room with the given ID, or merges and saves supplied metadata to a room with the given ID.
12 13 14 15 16 17 18 19 |
# File 'lib/lita/room.rb', line 12 def create_or_update(id, = {}) existing_room = find_by_id(id) = Util.stringify_keys() = existing_room..merge() if existing_room room = new(id, ) room.save room end |
.find_by_id(id) ⇒ Lita::Room?
Finds a room by ID.
24 25 26 27 |
# File 'lib/lita/room.rb', line 24 def find_by_id(id) = redis.hgetall("id:#{id}") new(id, ) if .key?("name") end |
.find_by_name(name) ⇒ Lita::Room?
Finds a room by display name.
32 33 34 35 |
# File 'lib/lita/room.rb', line 32 def find_by_name(name) id = redis.get("name:#{name}") find_by_id(id) if id end |
.fuzzy_find(identifier) ⇒ Lita::Room?
Finds a room by ID or name
40 41 42 |
# File 'lib/lita/room.rb', line 40 def fuzzy_find(identifier) find_by_id(identifier) || find_by_name(identifier) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compares the room against another room object to determine equality. Rooms are considered equal if they have the same ID.
76 77 78 |
# File 'lib/lita/room.rb', line 76 def ==(other) other.respond_to?(:id) && id == other.id end |
#hash ⇒ Fixnum
Generates a Fixnum
hash value for this user object. Implemented to support equality.
84 85 86 |
# File 'lib/lita/room.rb', line 84 def hash id.hash end |
#save ⇒ void
This method returns an undefined value.
Saves the room record to Redis, overwriting any previous data for the current ID.
90 91 92 93 94 95 96 97 |
# File 'lib/lita/room.rb', line 90 def save redis.pipelined do redis.hmset("id:#{id}", *.to_a.flatten) redis.set("name:#{name}", id) end end |