Class: Izanami::Mappers::Command

Inherits:
Izanami::Mapper show all
Defined in:
lib/izanami/mappers/command.rb

Overview

Mapper that handles the storage of ‘commands`.

Instance Attribute Summary

Attributes inherited from Izanami::Mapper

#namespace, #options

Instance Method Summary collapse

Methods inherited from Izanami::Mapper

#client, #initialize, #redis, #to_s

Constructor Details

This class inherits a constructor from Izanami::Mapper

Instance Method Details

#countFixnum

Count all commands.

Returns:

  • (Fixnum)


42
43
44
# File 'lib/izanami/mappers/command.rb', line 42

def count
  client.scard('ids')
end

#delete(ids) ⇒ Object

Delete the specified commands

Parameters:

  • ids (Array)

    the commands’ ids.



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_allObject

Delete all commands

See Also:

  • Izanami::Mappers::Command.{{#delete}


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.

Parameters:

  • ids (Array)

    the commands’ ids.



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.

Parameters:

  • ids (Array)

    the commands’ ids.



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?

Parameters:

  • id (String)

    the command’s id.

Returns:

  • (true, false)


35
36
37
# File 'lib/izanami/mappers/command.rb', line 35

def exists?(id)
  client.exists(id)
end

#find(id) ⇒ Hash?

Find a command.

Parameters:

  • id (String)

    the command’s id.

Returns:

  • (Hash, nil)

    the command or nil if is not found.



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.

Parameters:

  • id (String)

    the command’s id.

  • attribute (String)

    the command’s attribute.

Returns:

  • (String, nil)

    the attribute or nil if is empty.



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.

Parameters:

  • id (String)

    the command’s id.

  • payload (String)

    what to publish.



145
146
147
# File 'lib/izanami/mappers/command.rb', line 145

def publish(id, payload)
  client.publish(channel(id), payload)
end

#save(hash) ⇒ Hash

Note:

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.

Returns:

  • (Hash)

    the stored attributes.



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_idsArray

Retrieve all stored IDs

Returns:

  • (Array)

    all the ids as Strings.



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.

Parameters:

  • id (String)

    the command’s id.

See Also:

  • Izanami::Mappers::Command.{Redis{Redis::Client{Redis::Client#subscribe}


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.

Parameters:

  • number (Fixnum, String)

    the quantity of commands to retrieve.

Returns:

  • (Array)

    all the commands found.



51
52
53
54
# File 'lib/izanami/mappers/command.rb', line 51

def take(number)
  options = { order: 'DESC', limit: [0, number] }
  client.sort('ids', options).map { |id| find(id) }.compact
end

#ttlString, Fixnum

Default expiration time (defaults to 604800s == 7 days)

Returns:

  • (String, Fixnum)


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.

See Also:

  • Izanami::Mappers::Command.{Redis{Redis::Client{Redis::Client#subscribe}


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.

Parameters:

  • id (String)

    the command’s id.

  • attribute (String)

    the command’s attribute name.

  • value (String)

    the new value for the attribute.

Returns:

  • (Hash)

    the attributes stored.



124
125
126
# File 'lib/izanami/mappers/command.rb', line 124

def update(id, attribute, value)
  save('id' => id, attribute => value)
end