Permalink
Browse files

Start work on a key-resolver

  • Loading branch information...
1 parent fbfb155 commit 95b3e4737c04d26353b85cdc0ab027ce0e260340 @leehambley committed Mar 6, 2012
Showing with 23 additions and 12 deletions.
  1. +17 −11 lib/emotions.rb
  2. +6 −1 test/acceptance_test_key_loader.rb
View
@@ -80,19 +80,21 @@ def keys_matching(argument)
class KeyLoader
- def initialize(key)
- @object_class, @emotion, @object_id, @target_class = key.split(':')
+ attr_reader :key, :object, :target, :emotion
+
+ def initialize(key, id)
+ @key = key
+ @target_id = id
+ @object_class, @emotion, @object_id, @target_class = @key.split(':')
end
def object
Object.const_get(@object_class).find(object_id)
end
- private
-
- def object_id
- @object_id.to_i == @object_id ? @object_id : @object_id.to_i
- end
+ def target
+ Object.const_get(@target_class).find(target_id)
+ end
end
@@ -186,10 +188,14 @@ def initialize(*args)
end
self.class.send :define_method, :"#{emotion}_emotes" do
lookup_key_builder = KeyBuilder.new(object: self, emotion: emotion)
- keys = Emotions.backend.keys_matching(lookup_key_builder.key + "*")
- keys.collect do |key_name|
- key_loader = KeyLoader.new(key_name)
- Emotion.new(target: true, emotion: true, object: key_loader.object)
+ matching_keys = Emotions.backend.keys_matching(lookup_key_builder.key + "*")
+ puts lookup_key_builder.key + "*"
+ matching_keys.collect do |matching_key_name|
+ pairs = Emotions.backend.read_key(matching_key_name)
+ Hash[pairs].each do |id, time|
+ key_loader = KeyLoader.new(matching_key_name, id)
+ Emotion.new(target: key_loader.target, emotion: key_loader.emotion, object: key_loader.object, time: key_loader.time)
+ end
end
end
end
@@ -6,12 +6,17 @@ class AcceptanceTestEmotionsKeyLoader < MiniTest::Acceptance::TestCase
def test_it_should_call_find_for_both_keys
+ t = Time.now.utc
+
example_object = ExampleObject.new(123)
example_target = ExampleTarget.new(456)
- kl = KeyLoader.new("ExampleTarget:emotion:123")
+ kl = KeyLoader.new("ExampleTarget:emotion:123:ExampleObject", 123)
assert_equal example_object, kl.object
+ assert_equal example_target, kl.target
+ assert_equal :emotion, kl.emotion
+ assert_equal t, kl.time
end

0 comments on commit 95b3e47

Please sign in to comment.