lib/ohm.rb in ohm-0.0.24 vs lib/ohm.rb in ohm-0.0.25
- old
+ new
@@ -178,10 +178,14 @@
# @return [Integer] Returns the number of elements in the list.
def size
db.llen(key)
end
+
+ def inspect
+ "#<List: #{raw.inspect}>"
+ end
end
# Represents a Redis set.
#
# @example Use a set attribute.
@@ -241,10 +245,11 @@
# search_results.filter(public: true) do |filter_results|
# @events = filter_results.all
# end
# end
def filter(hash, &block)
+ raise ArgumentError, "filter expects a block" unless block_given?
apply(:sinterstore, keys(hash).push(key), &block)
end
# Returns a union with the sets generated from the passed hash.
#
@@ -253,27 +258,32 @@
# @example
# Event.search(day: "2009-09-11") do |search_results|
# events = search_results.all
# end
def search(hash, &block)
+ raise ArgumentError, "search expects a block" unless block_given?
apply(:sunionstore, keys(hash), &block)
end
def delete!
db.del(key)
end
# Apply a redis operation on a collection of sets. Note that
# the resulting set is removed inmediatly after use.
def apply(operation, source, &block)
- target = source.join(operation.to_s)
+ target = source.uniq.join("+")
db.send(operation, target, *source)
set = self.class.new(db, target, model)
block.call(set)
- set.delete!
+ set.delete! if source.size > 1
end
+ def inspect
+ "#<Set: #{raw.inspect}>"
+ end
+
private
# Transform a hash of attribute/values into an array of keys.
def keys(hash)
hash.inject([]) do |acc, t|
@@ -395,10 +405,14 @@
def self.[](id)
new(:id => id) if exists?(id)
end
+ def self.to_proc
+ Proc.new { |id| self[id] }
+ end
+
def self.all
@all ||= Attributes::Set.new(db, key(:all), self)
end
def self.attributes
@@ -553,9 +567,23 @@
def mutex
lock!
yield
unlock!
self
+ end
+
+ def inspect
+ everything = (attributes + collections + counters).map do |att|
+ value = begin
+ send(att)
+ rescue ModelIsNew
+ nil
+ end
+
+ [att, value.inspect]
+ end
+
+ "#<#{self.class}:#{id || "?"} #{everything.map {|e| e.join("=") }.join(" ")}>"
end
protected
def key(*args)