Commit 0ff5fcf

mo khan <mo@mokhan.ca>
2013-12-27 17:59:56
add simple context and key.
1 parent 87d2a09
lib/nasty/key.rb
@@ -0,0 +1,27 @@
+module Nasty
+  class Key
+    def initialize(key)
+      @key = key
+    end
+
+    def add_to(store, value)
+      store[to_sym] = value
+    end
+
+    def remove_from(store)
+      store[to_sym] = nil
+    end
+
+    def contained_in?(store)
+      item_from(store)
+    end
+
+    def item_from(store)
+      store[to_sym]
+    end
+
+    def to_sym
+      @key.to_sym
+    end
+  end
+end
lib/nasty/simple_context.rb
@@ -0,0 +1,23 @@
+module Nasty
+  class SimpleContext
+    def initialize(store = {})
+      @store = store
+    end
+
+    def add(key, value)
+      key.add_to(@store, value)
+    end
+
+    def remove(key)
+      key.remove_from(@store)
+    end
+
+    def contains?(key)
+      key.contained_in?(@store)
+    end
+
+    def item_for(key)
+      key.item_from(@store)
+    end
+  end
+end
lib/nasty.rb
@@ -5,10 +5,12 @@ require "nasty/composite_command"
 require "nasty/expose_binding"
 require "nasty/identity_map"
 require "nasty/kernel"
+require "nasty/key"
 require "nasty/lambda_behaviours"
 require "nasty/lazy"
 require "nasty/log"
 require "nasty/object"
+require "nasty/simple_context"
 require "nasty/version"
 
 module Nasty
spec/unit/simple_context_spec.rb
@@ -0,0 +1,63 @@
+require "spec_helper"
+
+module Nasty
+  describe SimpleContext do
+    let(:sut) { SimpleContext.new(store) }
+    let(:store) { Hash.new }
+
+    context "when adding an item" do
+      let(:key) { Key.new("artist") }
+      let(:item) { "bobby digital" }
+
+      before { sut.add(key, item) }
+
+      it "should add the item to the context" do
+        store[key.to_sym].should == item
+      end
+    end
+
+    context "when removing an item" do
+      let(:key) { Key.new("artist") }
+      let(:item) { "bobby digital" }
+
+      before :each do
+        sut.add(key, item)
+        sut.remove(key)
+      end
+
+      it "should remove the item from the store" do
+        store[key.to_sym].should be_nil
+      end
+    end
+
+    context "when checking if a key is in the context" do
+      context "when it is" do
+        let(:key) { Key.new("blah") }
+        before { sut.add(key, 'blah') }
+        let(:result) { sut.contains?(key) }
+
+        it "should return true" do
+          result.should be_true
+        end
+      end
+
+      context "when it is not" do
+        let(:key) { Key.new("blah") }
+        let(:result) { sut.contains?(key) }
+
+        it "should return false" do
+          result.should be_false
+        end
+      end
+    end
+    context "when retrieving an item" do
+      let(:key) { Key.new("name") }
+      before { sut.add(key, 'mo') }
+      let(:result) { sut.item_for(key) }
+
+      it "should return the correct item" do
+        result.should == 'mo'
+      end
+    end
+  end
+end