Sha256: 92a8b90a326da750bef2c085151b50c783ac61d16f477e8f5cc242ca98dc6421

Contents?: true

Size: 1.03 KB

Versions: 21

Compression:

Stored size: 1.03 KB

Contents

#!/usr/bin/env ruby

require 'set'
require 'redis'

module Flapjack
  module Data

    class Tag < ::Set

      attr_accessor :name

      def initialize(opts = {})
        raise "Redis connection not set" unless @redis = opts[:redis]

        @name  = opts[:name]
        preset = @redis.smembers(@name)
        enum = opts[:create] || []
        @redis.sadd(@name, enum) unless enum.empty?
        super(enum | preset)
      end

      def self.find(name, opts)
        self.new(:name => name,
                 :redis => opts[:redis])
      end

      def self.create(name, enum = [], opts)
        self.new(:name => name,
                 :create => enum,
                 :redis => opts[:redis])
      end

      def add(o)
        @redis.sadd(@name, o) unless o.empty?
        super(o)
      end

      def delete(o)
        @redis.srem(@name, o)
        super(o)
      end

      def merge(o)
        @redis.sadd(@name, o) unless o.empty?
        super(o)
      end

      def to_json(*a)
        self.to_a.to_json(*a)
      end

    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
flapjack-0.7.0 lib/flapjack/data/tag.rb