Permalink
Browse files

Streamline memory recall

  • Loading branch information...
1 parent c0f2e07 commit 36459acc2f833b4f091209d5fc55b415ab9327d2 @laserlemon laserlemon committed Feb 24, 2012
Showing with 10 additions and 6 deletions.
  1. +2 −3 lib/json_spec/memory.rb
  2. +8 −3 spec/json_spec/memory_spec.rb
@@ -5,12 +5,11 @@ def memory
end
def memorize(key, value)
- memory[key] = value
+ memory[key.to_sym] = value
end
def remember(json)
- return json if memory.empty?
- json.gsub(/%\{(#{memory.keys.map{|k| Regexp.quote(k) }.join("|")})\}/){ memory[$1] }
+ memory.empty? ? json : json % memory
end
def forget
@@ -6,21 +6,26 @@
end
it "memorizes strings" do
+ JsonSpec.memorize(:key, "value")
+ JsonSpec.memory.should == {:key => "value"}
+ end
+
+ it "symbolizes keys" do
JsonSpec.memorize("key", "value")
- JsonSpec.memory.should == {"key" => "value"}
+ JsonSpec.memory.should == {:key => "value"}
end
it "regurgitates unremembered strings" do
JsonSpec.remember("foo%{bar}").should == "foo%{bar}"
end
it "remembers strings" do
- JsonSpec.memorize("bar", "baz")
+ JsonSpec.memorize(:bar, "baz")
JsonSpec.remember("foo%{bar}").should == "foobaz"
end
it "forgets" do
- JsonSpec.memorize("key", "value")
+ JsonSpec.memorize(:key, "value")
JsonSpec.forget
JsonSpec.memory.should == {}
end

0 comments on commit 36459ac

Please sign in to comment.