Module: Redstream::Model

Defined in:
lib/redstream/model.rb

Overview

Include Redstream::Model in your model to stream the model’s updates via redis streams.

Examples:

class User < ActiveRecord::Base
  include Redstream::Model

  # ...

  redstream_callbacks
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/redstream/model.rb', line 15

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#redstream_nameObject

Override to customize the stream name. By default, the stream name is determined by the class name. If you override the instance method, please also override the class method.

Examples:

Sharding

class Product
  include Redstream::Model

  NUM_SHARDS = 4

  def redstream_name
    self.class.redstream_name(Digest::SHA1.hexdigest(id.to_s)[0, 4].to_i(16) % NUM_SHARDS)
  end

  def self.redstream_name(shard)
    "products-#{shard}
  end
end


81
82
83
# File 'lib/redstream/model.rb', line 81

def redstream_name
  self.class.redstream_name
end

#redstream_payloadObject

Override to customize the message payload. By default, the payload consists of the record id only (see example 1).

Examples:

Default

def redstream_payload
  { id: id }
end


58
59
60
# File 'lib/redstream/model.rb', line 58

def redstream_payload
  { id: id }
end