typed_map 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 092755b6e9e3f9778c4c20756959b48d9f4ac4f9
4
- data.tar.gz: 76c5d56be0cb22bad9f1b4098549f63240bd810c
3
+ metadata.gz: 6cd52f9f3446e14988cc0265d0d67c9c85120c34
4
+ data.tar.gz: 83e384d74a31ef6b3abe37a6d13e776e28f6a6ee
5
5
  SHA512:
6
- metadata.gz: 1e86bcfbc64b9d1fdfac3107f536fbda61788709c4ee6c054e607a169a452612964d017f87123399ff79e4f5942bba81161eaab8aca4b156890f51a0456921bb
7
- data.tar.gz: e5d8087bb6ed099778cd677b50d43a9b9f4020510c405a22e483e04242b10cc891783ee8ee1971e9fc01b63b921abec6e374d7255ceba3c8d6815738ed92cf23
6
+ metadata.gz: 35ceb45f2709677c75fa430f321a2a7cbd55e42fe74218d8b7e30cea0cae5c82fdff90bc125b6d8a17a8fce298a9ddbc1d8487edde0ad0fd4cf6eedd44bb2792
7
+ data.tar.gz: a2209dbb09d1adedf14876244c8e0b60a8660731eb7330ac703ba0137ddbec53a5017d6df8a84471bbf3fe04431f92c5cc2d01c2a1e00cfeabd5091c36dc17c9
data/lib/typed_map.rb CHANGED
@@ -3,6 +3,14 @@ require "typed_map/version"
3
3
  class TypedMap
4
4
  attr_reader :ktype, :vtype
5
5
 
6
+ INTERFACE_METHOD_NAMES = %i[add keys [] has? count length to_a to_h].freeze
7
+
8
+ class << self
9
+ def interface_method_names
10
+ INTERFACE_METHOD_NAMES
11
+ end
12
+ end
13
+
6
14
  def initialize(ktype:, vtype:)
7
15
  raise ArgumentError, "'ktype' should be an instance of Class" unless ktype.instance_of?(Class)
8
16
  raise ArgumentError, "'vtype' should be an instance of Class" unless vtype.instance_of?(Class)
@@ -37,4 +45,18 @@ class TypedMap
37
45
 
38
46
  @map.has_key? k
39
47
  end
48
+
49
+ def count
50
+ @map.count
51
+ end
52
+
53
+ alias :length :count
54
+
55
+ def to_a
56
+ @map.to_a
57
+ end
58
+
59
+ def to_h
60
+ @map.dup
61
+ end
40
62
  end
@@ -1,3 +1,3 @@
1
1
  class TypedMap
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -26,6 +26,12 @@ RSpec.describe TypedMap do
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ describe ".interface_method_names" do
31
+ it "returns list of public method names" do
32
+ expect(subject.interface_method_names).to eq %i[add keys [] has? count length to_a to_h]
33
+ end
34
+ end
29
35
  end
30
36
 
31
37
  describe "instance" do
@@ -143,5 +149,115 @@ RSpec.describe TypedMap do
143
149
  end
144
150
  end
145
151
  end
152
+
153
+ describe "#count" do
154
+ context "when empty" do
155
+ it "returns 0" do
156
+ expect(subject.count).to be 0
157
+ end
158
+ end
159
+
160
+ context "when has one pair" do
161
+ before { subject.add :first, "first" }
162
+
163
+ it "returns 1" do
164
+ expect(subject.count).to be 1
165
+ end
166
+ end
167
+
168
+ context "when has three pairs" do
169
+ before do
170
+ subject.add :first, "first"
171
+ subject.add :second, "second"
172
+ subject.add :third, "third"
173
+ end
174
+
175
+ it "returns 3" do
176
+ expect(subject.count).to be 3
177
+ end
178
+ end
179
+ end
180
+
181
+ describe "#length" do
182
+ context "when empty" do
183
+ it "returns 0" do
184
+ expect(subject.length).to be 0
185
+ end
186
+ end
187
+
188
+ context "when has one pair" do
189
+ before { subject.add :first, "first" }
190
+
191
+ it "returns 1" do
192
+ expect(subject.length).to be 1
193
+ end
194
+ end
195
+
196
+ context "when has three pairs" do
197
+ before do
198
+ subject.add :first, "first"
199
+ subject.add :second, "second"
200
+ subject.add :third, "third"
201
+ end
202
+
203
+ it "returns 3" do
204
+ expect(subject.length).to be 3
205
+ end
206
+ end
207
+ end
208
+
209
+ describe "#to_a" do
210
+ context "when empty" do
211
+ it "returns empty array" do
212
+ expect(subject.to_a).to eq []
213
+ end
214
+ end
215
+
216
+ context "when has one pair" do
217
+ before { subject.add :first, "first" }
218
+
219
+ it "returns array with one pair" do
220
+ expect(subject.to_a).to eq [[:first, "first"]]
221
+ end
222
+ end
223
+
224
+ context "when hash many pairs" do
225
+ before do
226
+ subject.add :first, "first"
227
+ subject.add :second, "second"
228
+ end
229
+
230
+ it "returns array with many pairs" do
231
+ expect(subject.to_a).to eq [[:first, "first"], [:second, "second"]]
232
+ end
233
+ end
234
+ end
235
+
236
+ describe "#to_h" do
237
+ context "when empty" do
238
+ it "returns empty hash" do
239
+ expect(subject.to_h).to eq({})
240
+ end
241
+ end
242
+
243
+ context "when has one pair" do
244
+ before { subject.add :first, "first" }
245
+
246
+ it "returns hash with one pair" do
247
+ expect(subject.to_h).to eq({first: "first"})
248
+ end
249
+ end
250
+
251
+ context "when hash many pairs" do
252
+ before do
253
+ subject.add :first, "first"
254
+ subject.add :second, "second"
255
+ end
256
+
257
+ it "returns hash with many pairs" do
258
+ expect(subject.to_h).to eq({first: "first", second: "second"})
259
+ end
260
+ end
261
+ end
146
262
  end
147
263
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Melnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-25 00:00:00.000000000 Z
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,11 +118,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.5.1
121
+ rubygems_version: 2.2.2
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: TypedMap is a Hash with typed keys and values.
125
125
  test_files:
126
126
  - spec/spec_helper.rb
127
127
  - spec/typed_map_spec.rb
128
- has_rdoc: