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
- #fragmentize(term) ⇒ Object
-
#initialize(opts = {}) ⇒ Set
constructor
A new instance of Set.
- #seed(term) ⇒ Object
- #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 |
# 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 |