Class: Izanami::Mappers::Command
- Inherits:
-
Izanami::Mapper
- Object
- Izanami::Mapper
- Izanami::Mappers::Command
- Defined in:
- lib/izanami/mappers/command.rb
Overview
Mapper that handles the storage of ‘commands`.
Instance Attribute Summary
Attributes inherited from Izanami::Mapper
Instance Method Summary collapse
-
#count ⇒ Fixnum
Count all commands.
-
#delete(ids) ⇒ Object
Delete the specified commands.
-
#delete_all ⇒ Object
Delete all commands.
-
#delete_commands(ids) ⇒ Object
Delete the commands’ attributes.
-
#delete_ids(ids) ⇒ Object
Delete the commands’ ids from the global IDs set.
-
#exists?(id) ⇒ true, false
The command exists?.
-
#find(id) ⇒ Hash?
Find a command.
-
#get(id, attribute) ⇒ String?
Get the attribute of a command.
-
#publish(id, payload) ⇒ Object
Publish to a Redis channel.
-
#save(hash) ⇒ Hash
Save the hash as a command.
-
#stored_ids ⇒ Array
Retrieve all stored IDs.
-
#subscribe(id, &block) ⇒ Object
Subscribe to a Redis channel.
-
#take(number) ⇒ Array
Retrieve only the specified number of commands, sorted by id.
-
#ttl ⇒ String, Fixnum
Default expiration time (defaults to 604800s == 7 days).
-
#unsubscribe(id) ⇒ Object
Unsubscribe from a Redis channel.
-
#update(id, attribute, value) ⇒ Hash
Update one field.
Methods inherited from Izanami::Mapper
#client, #initialize, #redis, #to_s
Constructor Details
This class inherits a constructor from Izanami::Mapper
Instance Method Details
#count ⇒ Fixnum
Count all commands.
42 43 44 |
# File 'lib/izanami/mappers/command.rb', line 42 def count client.scard('ids') end |
#delete(ids) ⇒ Object
Delete the specified commands
59 60 61 62 63 64 |
# File 'lib/izanami/mappers/command.rb', line 59 def delete(ids) client.multi do delete_commands ids delete_ids ids end end |
#delete_all ⇒ Object
Delete all commands
83 84 85 |
# File 'lib/izanami/mappers/command.rb', line 83 def delete_all delete stored_ids end |
#delete_commands(ids) ⇒ Object
Delete the commands’ attributes.
69 70 71 |
# File 'lib/izanami/mappers/command.rb', line 69 def delete_commands(ids) client.del(ids) end |
#delete_ids(ids) ⇒ Object
Delete the commands’ ids from the global IDs set.
76 77 78 |
# File 'lib/izanami/mappers/command.rb', line 76 def delete_ids(ids) client.srem('ids', ids) end |
#exists?(id) ⇒ true, false
The command exists?
35 36 37 |
# File 'lib/izanami/mappers/command.rb', line 35 def exists?(id) client.exists(id) end |
#find(id) ⇒ Hash?
Find a command.
14 15 16 17 18 |
# File 'lib/izanami/mappers/command.rb', line 14 def find(id) if exists?(id) client.hgetall(id) end end |
#get(id, attribute) ⇒ String?
Get the attribute of a command.
26 27 28 |
# File 'lib/izanami/mappers/command.rb', line 26 def get(id, attribute) client.hget(id, attribute) end |
#publish(id, payload) ⇒ Object
Publish to a Redis channel.
145 146 147 |
# File 'lib/izanami/mappers/command.rb', line 145 def publish(id, payload) client.publish(channel(id), payload) end |
#save(hash) ⇒ Hash
all the commands has an expiration time of #ttl time.
Save the hash as a command.
If the hash has an ‘id’, the command exists. If not, a new id is generated.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/izanami/mappers/command.rb', line 102 def save(hash) attributes = hash.dup attributes['id'] ||= generate_id client.multi do client.hmset(attributes['id'], *attributes.to_a) client.sadd('ids', attributes['id']) # just keep the command enough time client.expire(attributes['id'], ttl) end attributes end |
#stored_ids ⇒ Array
Retrieve all stored IDs
90 91 92 |
# File 'lib/izanami/mappers/command.rb', line 90 def stored_ids client.smembers('ids') end |
#subscribe(id, &block) ⇒ Object
Subscribe to a Redis channel.
154 155 156 |
# File 'lib/izanami/mappers/command.rb', line 154 def subscribe(id, &block) client.subscribe(channel(id), &block) end |
#take(number) ⇒ Array
Retrieve only the specified number of commands, sorted by id.
51 52 53 54 |
# File 'lib/izanami/mappers/command.rb', line 51 def take(number) = { order: 'DESC', limit: [0, number] } client.sort('ids', ).map { |id| find(id) }.compact end |
#ttl ⇒ String, Fixnum
Default expiration time (defaults to 604800s == 7 days)
137 138 139 |
# File 'lib/izanami/mappers/command.rb', line 137 def ttl @options[:ttl] || 604800 # 7 days end |
#unsubscribe(id) ⇒ Object
Unsubscribe from a Redis channel.
161 162 163 |
# File 'lib/izanami/mappers/command.rb', line 161 def unsubscribe(id) client.unsubscribe(channel(id)) end |
#update(id, attribute, value) ⇒ Hash
Update one field.
124 125 126 |
# File 'lib/izanami/mappers/command.rb', line 124 def update(id, attribute, value) save('id' => id, attribute => value) end |