module Minuteman::Analyzable

Public Instance Methods

&(event) click to toggle source
# File lib/minuteman/analyzable.rb, line 8
def &(event)
  operation("AND", [self, event])
end
+(event)
Alias for: |
-(event) click to toggle source
# File lib/minuteman/analyzable.rb, line 26
def -(event)
  operation("MINUS", [self, event])
end
-@() click to toggle source
# File lib/minuteman/analyzable.rb, line 21
def -@()
  operation("NOT", [self])
end
Also aliased as: ~@
^(event) click to toggle source
# File lib/minuteman/analyzable.rb, line 17
def ^(event)
  operation("XOR", [self, event])
end
count() click to toggle source
# File lib/minuteman/analyzable.rb, line 30
def count
  Minuteman.config.redis.call("BITCOUNT", key)
end
include?(user) click to toggle source
# File lib/minuteman/analyzable.rb, line 34
def include?(user)
  Minuteman.config.redis.call("GETBIT", key, user.id) == 1
end
|(event) click to toggle source
# File lib/minuteman/analyzable.rb, line 12
def |(event)
  operation("OR", [self, event])
end
Also aliased as: +
~@()
Alias for: -@

Private Instance Methods

key_exists?(key) click to toggle source
# File lib/minuteman/analyzable.rb, line 40
def key_exists?(key)
  Minuteman.config.redis.call("EXISTS", key) == 1
end
operation(action, events = []) click to toggle source
# File lib/minuteman/analyzable.rb, line 44
def operation(action, events = [])
  base_key = Minuteman.config.operations_prefix

  destination_key = if action == "NOT"
                      "#{base_key}#{events[0].id}:#{action}"
                    else
                      src, dst = events[0].id, events[1].id
                      "#{base_key}#{src}:#{action}:#{dst}"
                    end

  if key_exists?(destination_key)
    return Minuteman::Result.new(destination_key)
  end

  script(Minuteman::LUA_OPERATIONS, 0, action.upcase.to_json,
         events.map(&:key).to_json, destination_key.to_json)

  Minuteman::Result.new(destination_key)
end
script(file, *args) click to toggle source

Code taken from Ohm. Just small changes to adapt to the current Minuteman stuff. elcuervo <3 (soveran + djanowski)

# File lib/minuteman/analyzable.rb, line 66
def script(file, *args)
  begin
    cache = Minuteman::LUA_CACHE[Minuteman.config.redis.url]

    if cache.key?(file)
      sha = cache[file]
    else
      src = File.read(file)
      sha = Minuteman.config.redis.call("SCRIPT", "LOAD", src)

      cache[file] = sha
    end

    Minuteman.config.redis.call!("EVALSHA", sha, *args)

  rescue RuntimeError
    case $!.message
    when ErrorPatterns::NOSCRIPT
      Minuteman::LUA_CACHE[Minuteman.config.redis.url].clear
      retry
    when ErrorPatterns::DUPLICATE
      raise UniqueIndexViolation, $1
    else
      raise $!
    end
  end
end