Class: Lunar::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/lunar/index.rb

Constant Summary collapse

FUZZY_MAX_LENGTH =
100
FuzzyFieldTooLong =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, redis = nil) ⇒ Index

Returns a new instance of Index.



17
18
19
20
21
22
23
# File 'lib/lunar/index.rb', line 17

def initialize(prefix, redis = nil)
  @ns    = Lunar.nest[prefix]
  @attrs = {}
  @redis = (redis || Lunar.redis)
  @numeric_attrs = {}
  @fuzzy_attrs = {}
end

Instance Attribute Details

#nsObject (readonly)

Returns the value of attribute ns.



7
8
9
# File 'lib/lunar/index.rb', line 7

def ns
  @ns
end

Class Method Details

.create(prefix) ⇒ Object



9
10
11
# File 'lib/lunar/index.rb', line 9

def self.create(prefix)
  new(prefix).tap { |i| yield i }.index
end

.delete(prefix, key) ⇒ Object



13
14
15
# File 'lib/lunar/index.rb', line 13

def self.delete(prefix, key)
  new(prefix).tap { |i| i.key key }.delete 
end

Instance Method Details

#attr(field, value = nil) ⇒ Object



34
35
36
37
# File 'lib/lunar/index.rb', line 34

def attr(field, value = nil)
  @attrs[field.to_sym] = value if value
  @attrs[field.to_sym]
end

#attrs=(attrs) ⇒ Object



30
31
32
# File 'lib/lunar/index.rb', line 30

def attrs=(attrs)
  attrs.each { |k, v| attr k, v } 
end

#deleteObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lunar/index.rb', line 59

def delete
  keys = @redis.keys @ns[key]['*']
  keys.each do |k|
    field = k.gsub("#{ @ns[key] }:", '')
    words = @redis.smembers(k)
    words.each do |w|
      @redis.zrem @ns[field][Lunar.encode(w)], key
    end
    @redis.del k
  end

  fuzzy_keys = @redis.keys @ns[:Fuzzy][key]['*']
  fuzzy_keys.each do |k|
    field = k.gsub("#{ @ns[:Fuzzy][key] }:", '')
    words = @redis.smembers(k)
    words.each do |w|
      remove_fuzzy_values field
    end
    @redis.del k
  end
end

#fuzzy(field, value = nil) ⇒ Object



46
47
48
49
# File 'lib/lunar/index.rb', line 46

def fuzzy(field, value = nil)
  @fuzzy_attrs[field.to_sym] = value if value
  @fuzzy_attrs
end

#indexObject



51
52
53
54
55
56
57
# File 'lib/lunar/index.rb', line 51

def index
  index_str_attrs
  index_fuzzy_attrs
  index_numeric_attrs

  return self
end

#key(key = nil) ⇒ Object



25
26
27
28
# File 'lib/lunar/index.rb', line 25

def key(key = nil)
  @key = key if key
  @key
end

#numeric_attr(field, value = nil) ⇒ Object Also known as: integer, float



39
40
41
42
# File 'lib/lunar/index.rb', line 39

def numeric_attr(field, value = nil)
  @numeric_attrs[field.to_sym] = value if value
  @numeric_attrs[field.to_sym]
end