lib/hari/entity/repository.rb in hari-0.0.4 vs lib/hari/entity/repository.rb in hari-0.0.5
- old
+ new
@@ -2,21 +2,23 @@
class Entity
module Repository
extend ActiveSupport::Concern
def create_or_update
- run_callbacks(:save) { new? ? create : update }
+ run_callbacks(:save) { new? ? create : update }.tap do
+ @changed_attributes.clear
+ end
end
alias :save :create_or_update
def create
run_callbacks :create do
fail Hari::ValidationsFailed, self unless valid?
@id ||= generate_id
- self.created_at = Time.now
+ @created_at ||= Time.now
self.updated_at = Time.now
persist
end
self
@@ -32,11 +34,13 @@
self
end
def persist
- Hari.redis.set id, to_json
+ source = to_json
+ @previously_changed = changes
+ Hari.redis.set id, source
end
def delete
run_callbacks :destroy do
Hari.redis.del id
@@ -55,18 +59,22 @@
end
def find(*args)
options = args.extract_options!
args.flatten!
+ return if args.empty?
+
args = args.map { |a| a.to_s.gsub(/^hari\:/, '') }
args.one? ? find_one(args[0], options) : find_many(args, options)
end
def find_one(id, options = {})
from_json Hari.redis.get(id)
end
def find_many(ids, options = {})
+ return [] if ids.empty?
+
Hari.redis.mget(ids).map &method(:from_json)
end
end