Class: RedisTags::RedisTag

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_tags/redis_tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name) ⇒ RedisTag

Returns a new instance of RedisTag.



8
9
10
11
12
# File 'lib/redis_tags/redis_tag.rb', line 8

def initialize(owner, name)
  @owner_class = owner.class.to_s.downcase
  @name = name.downcase.strip
  @owner = owner
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/redis_tags/redis_tag.rb', line 3

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



6
7
8
# File 'lib/redis_tags/redis_tag.rb', line 6

def owner
  @owner
end

#owner_classObject (readonly)

Returns the value of attribute owner_class.



4
5
6
# File 'lib/redis_tags/redis_tag.rb', line 4

def owner_class
  @owner_class
end

Class Method Details

.has_tags(klass, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/redis_tags/redis_tag.rb', line 30

def self.has_tags(klass, options = {})
  key_array = []
  if options[:tags].to_a.size == 1
    if options[:random].to_i > 0
      klass.redis_tags_engine.zrandmember RedisTags::RedisTag.tagged_with_key_for(klass, options[:tags]), options[:random].to_i
    elsif options[:since]
      klass.redis_tags_engine.zrangebyscore RedisTags::RedisTag.tagged_with_key_for(klass, options[:tags]), options[:since].to_i, Time.now.to_i
    else
      klass.redis_tags_engine.zrangebyscore RedisTags::RedisTag.tagged_with_key_for(klass, options[:tags]), Time.now.to_i - 7 * 60 * 60 * 24, Time.now.to_i
    end
  else
    options[:tags].to_a.each do |tag_name|
      key_array << RedisTag.tagged_with_key_for(klass, tag_name)
    end

    klass.redis_tags_engine.zinterstore RedisTags::RedisTag.intersect_key_for(klass, options[:tags]), key_array, {:aggregate => :max}

    if options[:random].to_i > 0
      klass.redis_tags_engine.zrandmember RedisTags::RedisTag.intersect_key_for(klass, options[:tags]), options[:random].to_i
    elsif options[:since]
      klass.redis_tags_engine.zrangebyscore RedisTags::RedisTag.intersect_key_for(klass, options[:tags]), options[:since].to_i, Time.now.to_i
    else
      klass.redis_tags_engine.zrangebyscore RedisTags::RedisTag.intersect_key_for(klass, options[:tags]), (Time.now.to_i - 7 * 60 * 60 * 24), Time.now.to_i
    end
  end
end

.register_tag_for_autocomplete(use_engine, tag_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/redis_tags/redis_tag.rb', line 18

def self.register_tag_for_autocomplete(use_engine, tag_name)
  partial_tag_name = ""
  tag_name.each_char do |char|
    partial_tag_name += char
    use_engine.sadd "tags:all:#{partial_tag_name}", tag_name
  end
end

.starts_with?(use_engine, partial_tag_name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/redis_tags/redis_tag.rb', line 14

def self.starts_with?(use_engine, partial_tag_name)
  use_engine.smembers "tags:all:#{partial_tag_name}"
end

Instance Method Details

#countObject



26
27
28
# File 'lib/redis_tags/redis_tag.rb', line 26

def count
  engine.zcard redis_key
end