nasty 0.0.1388166636 → 0.0.1388166944

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a199b39461b16ea3634bf8b2f5d539cf68ca1bd
4
- data.tar.gz: ab5aff2b9257964399a87247f5726ad5a959561e
3
+ metadata.gz: 89915476bb30d44f78e092da9916dcd1f9cc0d4e
4
+ data.tar.gz: 00271ed65c769440f8c2df1600933405e31f19f6
5
5
  SHA512:
6
- metadata.gz: 8ae5b30bbca156ce1bfcbb317daf24a8ecf03e787f453f4b792574404f026f4951d0f6ba4b1fbe9b96fae6867e8853077c7fab5c8c4361b00612629c72847640
7
- data.tar.gz: 9ceddeb796de7c33fc0b756476fd666147f5d8a19ff77e255c40f92578b4f8d802c1e33726f43c07b2a96b1d35920684a46d79710bb525f6b42a2e111aecf50d
6
+ metadata.gz: 4163f06eaa5b09d03ecfb900ec4321d9b0150090a54d0d2fc6d0c46b30adcdb466b2c36f464e72a3f237e4baa2f8dacf67dc60cfe55cb95cb04ab7799da3175e
7
+ data.tar.gz: 30be1f9fe89b9879e7b2761cf9911601859ddef531ebb764352c50a7b2e8ad649530cdf1c6e4f78187f842eee8ae5dee50657111433c19cfdd3be81bfbbdaf27
data/lib/nasty.rb CHANGED
@@ -3,6 +3,7 @@ require "nasty/block_specification"
3
3
  require "nasty/command"
4
4
  require "nasty/composite_command"
5
5
  require "nasty/expose_binding"
6
+ require "nasty/identity_map"
6
7
  require "nasty/kernel"
7
8
  require "nasty/lambda_behaviours"
8
9
  require "nasty/lazy"
@@ -0,0 +1,23 @@
1
+ module Nasty
2
+ class IdentityMap
3
+ def initialize(items = {})
4
+ @items = items
5
+ end
6
+
7
+ def add(item)
8
+ @items[item.id] = item
9
+ end
10
+
11
+ def has_item_for?(id)
12
+ @items.has_key?(id)
13
+ end
14
+
15
+ def item_for(id)
16
+ @items[id]
17
+ end
18
+
19
+ def evict(item)
20
+ @items.reject! { |key, value| key == item.id }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ module Nasty
4
+ describe IdentityMap do
5
+ let(:sut) { IdentityMap.new }
6
+
7
+ context "when an item is added" do
8
+ let(:item) { Item.new(:id => 187) }
9
+
10
+ before { sut.add(item) }
11
+
12
+ it "indicates that an item with that id is available" do
13
+ sut.has_item_for?(item.id).should be_true
14
+ end
15
+
16
+ it "loads the item" do
17
+ sut.item_for(item.id).should == item
18
+ end
19
+
20
+ context "when an item is evicted" do
21
+ before { sut.evict(item) }
22
+
23
+ it "indicates that it does not have an item for the evicted id" do
24
+ sut.has_item_for?(item.id).should be_false
25
+ end
26
+
27
+ it "returns nothing" do
28
+ sut.item_for(item.id).should be_nil
29
+ end
30
+ end
31
+ end
32
+
33
+ context "when no items have been added" do
34
+ it "indicates that it does not have any items for any id" do
35
+ (0..10).each do |i|
36
+ sut.has_item_for?(i).should be_false
37
+ end
38
+ end
39
+ end
40
+
41
+ class Item
42
+ attr_reader :id
43
+
44
+ def initialize(attributes)
45
+ @id = attributes[:id]
46
+ end
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nasty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1388166636
4
+ version: 0.0.1388166944
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
@@ -85,6 +85,7 @@ files:
85
85
  - lib/nasty/command.rb
86
86
  - lib/nasty/composite_command.rb
87
87
  - lib/nasty/expose_binding.rb
88
+ - lib/nasty/identity_map.rb
88
89
  - lib/nasty/kernel.rb
89
90
  - lib/nasty/lambda_behaviours.rb
90
91
  - lib/nasty/lazy.rb
@@ -96,6 +97,7 @@ files:
96
97
  - spec/unit/block_specification_spec.rb
97
98
  - spec/unit/command_spec.rb
98
99
  - spec/unit/expose_binding_spec.rb
100
+ - spec/unit/identity_map_spec.rb
99
101
  - spec/unit/lambda_behaviours_spec.rb
100
102
  - spec/unit/lazy_spec.rb
101
103
  - spec/unit/log_spec.rb
@@ -129,6 +131,7 @@ test_files:
129
131
  - spec/unit/block_specification_spec.rb
130
132
  - spec/unit/command_spec.rb
131
133
  - spec/unit/expose_binding_spec.rb
134
+ - spec/unit/identity_map_spec.rb
132
135
  - spec/unit/lambda_behaviours_spec.rb
133
136
  - spec/unit/lazy_spec.rb
134
137
  - spec/unit/log_spec.rb