Sha256: e39dd8ab9ef52cc229e037c07eabd87c431fc801d695e1867e8d314793e898d4

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

ActiveRecord::Base.class_eval do
  def stream
    Nest.new(self.class.name, $redis)[to_param]
  rescue
    nil
  end

  def self.stream
    Nest.new(name, $redis)
  rescue
    nil
  end

  def events(size = -1)
    stream[:event].lrange(0, size).map do |event_id|
      {
        action: $redis.hget("events:#{event_id}", "action"),
        timestamp: $redis.hget("events:#{event_id}", "timestamp")
      }
    end
  rescue
    []
  end

  def profile_events(size = -1)
    stream[:event][:profile].lrange(0, size).map do |event_id|
      {
        action: $redis.hget("events:#{event_id}", "action"),
        timestamp: $redis.hget("events:#{event_id}", "timestamp")
      }
    end
  rescue
    []
  end

  def create_event(action, timestamp)
    event_id = $redis.incr("events:latest_id")
    $redis.hmset("events:#{event_id}", "action", action, "timestamp", timestamp)
    event_id
  rescue
    nil
  end

  def log_event(event_id)
    stream[:event].lpush(event_id)
  rescue
    nil
  end

  def log_profile_event(event_id)
    stream[:event][:profile].lpush(event_id)
  rescue
    nil
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sufia-models-6.7.0 lib/sufia/models/active_record/redis.rb
sufia-models-6.6.1 lib/sufia/models/active_record/redis.rb
sufia-models-6.6.0 lib/sufia/models/active_record/redis.rb
sufia-models-6.5.0 lib/sufia/models/active_record/redis.rb
sufia-models-6.4.0 lib/sufia/models/active_record/redis.rb
sufia-models-6.3.0 lib/sufia/models/active_record/redis.rb