spruz 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/spruz/bijection.rb +16 -1
- data/lib/spruz/version.rb +1 -1
- data/tests/test_spruz.rb +15 -1
- metadata +5 -12
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/spruz/bijection.rb
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
module Spruz
|
2
2
|
class Bijection < Hash
|
3
|
+
def self.[](*pairs)
|
4
|
+
pairs.size % 2 == 0 or
|
5
|
+
raise ArgumentError, "odd number of arguments for #{self}"
|
6
|
+
new.fill do |obj|
|
7
|
+
(pairs.size / 2).times do |i|
|
8
|
+
j = 2 * i
|
9
|
+
key = pairs[j]
|
10
|
+
value = pairs[j + 1]
|
11
|
+
obj.key?(key) and raise ArgumentError, "duplicate key #{key.inspect} for #{self}"
|
12
|
+
obj.inverted.key?(value) and raise ArgumentError, "duplicate value #{value.inspect} for #{self}"
|
13
|
+
obj[pairs[j]] = pairs[j + 1]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
3
18
|
def initialize(inverted = Bijection.new(self))
|
4
19
|
@inverted = inverted
|
5
20
|
end
|
@@ -21,7 +36,7 @@ module Spruz
|
|
21
36
|
end
|
22
37
|
|
23
38
|
def []=(key, value)
|
24
|
-
|
39
|
+
key?(key) and return
|
25
40
|
super
|
26
41
|
@inverted[value] = key
|
27
42
|
end
|
data/lib/spruz/version.rb
CHANGED
data/tests/test_spruz.rb
CHANGED
@@ -433,7 +433,6 @@ module Spruz
|
|
433
433
|
end
|
434
434
|
end
|
435
435
|
|
436
|
-
|
437
436
|
class BlankFullTest < Test::Unit::TestCase
|
438
437
|
require 'spruz/xt/deep_dup'
|
439
438
|
|
@@ -451,4 +450,19 @@ module Spruz
|
|
451
450
|
assert_same f, g
|
452
451
|
end
|
453
452
|
end
|
453
|
+
|
454
|
+
class BijectionTest < Test::Unit::TestCase
|
455
|
+
def test_bijection
|
456
|
+
assert_equal [ [ 1, 2 ], [ 3, 4 ] ], Spruz::Bijection[ 1, 2, 3, 4 ].to_a
|
457
|
+
assert_raise(ArgumentError) do
|
458
|
+
Spruz::Bijection[1,2,3]
|
459
|
+
end
|
460
|
+
assert_raise(ArgumentError) do
|
461
|
+
Spruz::Bijection[1,2,3,2]
|
462
|
+
end
|
463
|
+
assert_raise(ArgumentError) do
|
464
|
+
Spruz::Bijection[1,2,1,3]
|
465
|
+
end
|
466
|
+
end
|
467
|
+
end
|
454
468
|
end
|
metadata
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spruz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 1
|
9
|
-
version: 0.2.1
|
4
|
+
version: 0.2.2
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Florian Frank
|
@@ -14,7 +9,7 @@ autorequire:
|
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
11
|
|
17
|
-
date: 2010-11-
|
12
|
+
date: 2010-11-11 00:00:00 +01:00
|
18
13
|
default_executable:
|
19
14
|
dependencies: []
|
20
15
|
|
@@ -91,20 +86,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
86
|
requirements:
|
92
87
|
- - ">="
|
93
88
|
- !ruby/object:Gem::Version
|
94
|
-
segments:
|
95
|
-
- 0
|
96
89
|
version: "0"
|
90
|
+
version:
|
97
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
92
|
requirements:
|
99
93
|
- - ">="
|
100
94
|
- !ruby/object:Gem::Version
|
101
|
-
segments:
|
102
|
-
- 0
|
103
95
|
version: "0"
|
96
|
+
version:
|
104
97
|
requirements: []
|
105
98
|
|
106
99
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.3.
|
100
|
+
rubygems_version: 1.3.5
|
108
101
|
signing_key:
|
109
102
|
specification_version: 3
|
110
103
|
summary: Useful stuff.
|