Sha256: 8a96e0f81129dc9b3404fe7e5e21dbe9868f5a9ba7541870d1d902afa4f011b8

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Hari
  module Keys
    class Key

      attr_reader :node, :name, :options

      def initialize(node = nil, options = {})
        @node, @options = node, options
      end

      def key
        @key ||= begin
          prefix = node ? "#{Hari.node_key(node)}:" : ''
          prefix + name.to_s
        end
      end

      def delete!
        Hari.redis.del key
      end

      def exists?
        Hari.redis.exists key
      end

      def expire(milliseconds)
        Hari.redis.pexpire key, milliseconds
      end

      def expire_at(timestamp)
        Hari.redis.expireat key, ::Time.parse(timestamp).to_s
      end

      def persist
        Hari.redis.persist key
      end

      def type
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/keys/key.rb