lib/hari/keys/key.rb in hari-0.0.4 vs lib/hari/keys/key.rb in hari-0.0.5
- old
+ new
@@ -1,13 +1,13 @@
module Hari
module Keys
class Key
- attr_reader :node, :name
+ attr_reader :node, :name, :options
- def initialize(node = nil)
- @node = node
+ def initialize(node = nil, options = {})
+ @node, @options = node, options
end
def key
@key ||= begin
prefix = node ? "#{Hari.node_key(node)}:" : ''
@@ -39,9 +39,30 @@
Hari.redis.type key
end
def ttl
Hari.redis.ttl key
+ end
+
+ def serialize(value)
+ type = options[:type]
+
+ if type.nil? || value.nil?
+ value
+ else
+ value.kind_of?(Enumerable) ? value.map(&:to_json) : value.to_json
+ end
+ end
+
+ def desserialize(value)
+ type = options[:type]
+
+ if type.nil? || value.nil?
+ value
+ else
+ value.kind_of?(Enumerable) ? value.map { |v| type.from_json(v) }
+ : type.from_json(value)
+ end
end
end
end
end