Class: Mindtrick::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/mindtrick/set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Set

Returns a new instance of Set.



5
6
7
8
9
10
# File 'lib/mindtrick/set.rb', line 5

def initialize(opts = {})
  @redis      = opts[:redis]      || Redis.new
  @prefix     = opts[:prefix]     || 'mndtrk'
  @max_terms  = opts[:max_terms]  || 250
  @max_length = opts[:max_length] || 15
end

Instance Attribute Details

#max_lengthObject (readonly)

Returns the value of attribute max_length.



4
5
6
# File 'lib/mindtrick/set.rb', line 4

def max_length
  @max_length
end

#max_termsObject (readonly)

Returns the value of attribute max_terms.



4
5
6
# File 'lib/mindtrick/set.rb', line 4

def max_terms
  @max_terms
end

#prefixObject (readonly)

Returns the value of attribute prefix.



4
5
6
# File 'lib/mindtrick/set.rb', line 4

def prefix
  @prefix
end

#redisObject (readonly)

Returns the value of attribute redis.



4
5
6
# File 'lib/mindtrick/set.rb', line 4

def redis
  @redis
end

Instance Method Details

#add(term) ⇒ Object



12
13
14
15
16
17
# File 'lib/mindtrick/set.rb', line 12

def add(term)
  fragmentize(term) do |k|
    redis.zincrby(k, 1, term)
    enforce_term_limit(k)
  end
end

#fragmentize(term) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/mindtrick/set.rb', line 28

def fragmentize(term)
  term = Text.new(term)
  term.each_fragment do |f|
    if f.length <= max_length
      yield f.prefixed(prefix)
    else
      break
    end
  end
end

#seed(term) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mindtrick/set.rb', line 19

def seed(term)
  fragmentize(term) do |k|
    unless redis.zscore(k, term)
      redis.zadd(k, 1, term)
      enforce_term_limit(k)
    end
  end
end

#suggest(partial, count = 10) ⇒ Object



39
40
41
42
# File 'lib/mindtrick/set.rb', line 39

def suggest(partial, count = 10)
  key = Text.new(partial).prefixed(prefix)
  redis.zrevrange key, 0, count
end