rifle 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rifle.rb +34 -6
  2. data/lib/rifle/version.rb +1 -1
  3. metadata +1 -1
data/lib/rifle.rb CHANGED
@@ -17,11 +17,30 @@ module Rifle
17
17
  class Processor
18
18
 
19
19
  def index_resource(urn, hash)
20
- words = traverse_object(hash, words)
20
+ # First get the old values
21
+ old_payload = get_payload_for_urn(urn)
22
+ if old_payload
23
+ old_words = traverse_object(old_payload)
24
+ old_metaphones = get_metaphones(old_words)
25
+ else
26
+ old_metaphones = []
27
+ end
28
+
29
+ # Now get the new ones
30
+ words = traverse_object(hash)
21
31
  metaphones = get_metaphones(words)
22
- metaphones.each do |metaphone|
23
- save_processed(urn, metaphone)
32
+
33
+ # Clear out words that have been removed
34
+ (old_metaphones - metaphones).each do |metaphone|
35
+ remove_urn_from_metaphone_set(urn, metaphone)
36
+ end
37
+
38
+ # And add the new ones
39
+ (metaphones - old_metaphones).each do |metaphone|
40
+ add_urn_to_metaphone_set(urn, metaphone)
24
41
  end
42
+
43
+ # Save the entire payload for future reference
25
44
  save_payload(urn, hash)
26
45
  metaphones
27
46
  end
@@ -48,7 +67,11 @@ module Rifle
48
67
 
49
68
  private
50
69
 
51
- def traverse_object(input, words)
70
+ def remove_old(urn)
71
+
72
+ end
73
+
74
+ def traverse_object(input, words = nil)
52
75
  words ||= Set.new
53
76
  examine_value(input, words)
54
77
  words
@@ -78,10 +101,14 @@ module Rifle
78
101
  ::Text::Metaphone.metaphone(words.to_a.join(' ')).split(' ')
79
102
  end
80
103
 
81
- def save_processed(urn, metaphone)
104
+ def add_urn_to_metaphone_set(urn, metaphone)
82
105
  Rifle.settings.redis.sadd("rifle:m:#{metaphone}", urn)
83
106
  end
84
107
 
108
+ def remove_urn_from_metaphone_set(urn, metaphone)
109
+ Rifle.settings.redis.srem("rifle:m:#{metaphone}", urn)
110
+ end
111
+
85
112
  def save_payload(urn, hash)
86
113
  Rifle.settings.redis.set("rifle:u:#{urn}", hash.to_json)
87
114
  end
@@ -91,7 +118,8 @@ module Rifle
91
118
  end
92
119
 
93
120
  def get_payload_for_urn(urn)
94
- JSON.parse(Rifle.settings.redis.get("rifle:u:#{urn}"))
121
+ payload = Rifle.settings.redis.get("rifle:u:#{urn}")
122
+ payload.nil? ? nil : JSON.parse(payload)
95
123
  end
96
124
 
97
125
  end
data/lib/rifle/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rifle
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rifle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: