Class: Mindtrick::Set
- Inherits:
-
Object
- Object
- Mindtrick::Set
- Defined in:
- lib/mindtrick/set.rb
Instance Attribute Summary collapse
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#max_terms ⇒ Object
readonly
Returns the value of attribute max_terms.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #add(term) ⇒ Object
-
#initialize(opts = {}) ⇒ Set
constructor
A new instance of Set.
- #suggest(partial, count = 10) ⇒ Object
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_length ⇒ Object (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_terms ⇒ Object (readonly)
Returns the value of attribute max_terms.
4 5 6 |
# File 'lib/mindtrick/set.rb', line 4 def max_terms @max_terms end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
4 5 6 |
# File 'lib/mindtrick/set.rb', line 4 def prefix @prefix end |
#redis ⇒ Object (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 18 19 20 21 22 |
# File 'lib/mindtrick/set.rb', line 12 def add(term) term = Text.new(term) term.each_fragment do |f| if f.length <= max_length k = f.prefixed(prefix) redis.zincrby(k, 1, term) enforce_term_limit(k) end end term end |
#suggest(partial, count = 10) ⇒ Object
24 25 26 27 |
# File 'lib/mindtrick/set.rb', line 24 def suggest(partial, count = 10) key = Text.new(partial).prefixed(prefix) redis.zrevrange key, 0, count end |