lib/minuteman/bit_operations.rb in minuteman-0.1.1 vs lib/minuteman/bit_operations.rb in minuteman-0.2.0.pre
- old
+ new
@@ -1,15 +1,15 @@
+require "minuteman/bit_operations/operation"
+
class Minuteman
module BitOperations
- BIT_OPERATION_PREFIX = "bitop"
-
# Public: Checks for the existance of ids on a given set
#
# ids - Array of ids
#
def include?(*ids)
- result = ids.map { |id| redis.getbit(key, id) == 1 }
+ result = ids.map { |id| getbit(id) }
result.size == 1 ? result.first : result
end
# Public: Resets the current key
#
@@ -24,76 +24,62 @@
end
# Public: Calculates the NOT of the current key
#
def -@
- bit_operation("NOT", key)
+ operation("NOT", key)
end
alias :~@ :-@
# Public: Calculates the substract of one set to another
#
+ # timespan: Another BitOperations enabled class
+ #
def -(timespan)
- self ^ (self & timespan)
+ operation("MINUS", timespan)
end
# Public: Calculates the XOR against another timespan
#
# timespan: Another BitOperations enabled class
#
def ^(timespan)
- bit_operation("XOR", [key, timespan.key])
+ operation("XOR", timespan)
end
# Public: Calculates the OR against another timespan
#
- # timespan: Another BitOperations enabled class
+ # timespan: Another BitOperations enabled class or an Array
#
def |(timespan)
- bit_operation("OR", [key, timespan.key])
+ operation("OR", timespan)
end
alias :+ :|
# Public: Calculates the AND against another timespan
#
- # timespan: Another BitOperations enabled class
+ # timespan: Another BitOperations enabled class or an Array
#
def &(timespan)
- bit_operation("AND", [key, timespan.key])
+ operation("AND", timespan)
end
private
- # Private: The destination key for the operation
+ # Private: Helper to access the value a given bit
#
- # type - The bitwise operation
- # events - The events to permuted
+ # id: The bit
#
- def destination_key(type, events)
- [
- Minuteman::PREFIX,
- BIT_OPERATION_PREFIX,
- type,
- events.join("-")
- ].join("_")
+ def getbit(id)
+ redis.getbit(key, id) == 1
end
- # Private: Executes a bit operation
+ # Private: executes an operation between the current timespan and another
#
- # type - The bitwise operation
- # events - The events to permuted
+ # type: The operation type
+ # timespan: The given timespan
#
- def bit_operation(type, events)
- key = destination_key(type, Array(events))
- redis.bitop(type, key, events)
- BitOperation.new(redis, key)
+ def operation(type, timespan)
+ Operation.new(redis, type, self, timespan).call
end
- end
-
- # Public: The result of intersecting results
- # redis - The Redis connection
- # key - The key where the result it's stored
- #
- class BitOperation < Struct.new(:redis, :key)
- include BitOperations
end
end