data_types 1.0.0 → 1.1.0

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: d10672d37ed9a9d9078f149f3b5461b0832d87af
4
- data.tar.gz: 35f5997085de9732c2de7b2a65c976a9a28cc7f1
3
+ metadata.gz: b32c543113e49bddaced81642323a64dd7070482
4
+ data.tar.gz: 38722da0ce5a3bfb9e8f0e73901b459fb769b8e6
5
5
  SHA512:
6
- metadata.gz: 895dde9c53db720c9daf23bf8dfd2610d9ffd276f1cb505154b7bf3901d689ba7f6bc288633cf6512f450494e56845018269e43b38aa15382edbb190eee33d29
7
- data.tar.gz: 9948ee9c1429accbdcf45d30a2df553d68da1ad7651468b6250ff58e7b59adab62f4af44c543c3ff5cfbefde087763ebfaec0d893634bea43e311e9a8934c5de
6
+ metadata.gz: 793cd05b43b4856f58b625c4adcc47ab718105882fbde3c4be5f6b30b41e22560d4e813683911d5eaeeed033f07631800db0abc2702b69642e950a6eba3ccc64
7
+ data.tar.gz: 5144e1a2677b04b3dc18c02e7d726f9c2dba98e6397eed633696575e77d6ec510d549e67c4e84975e20d36465f5c57f551c4c8f5b3e178378f8d71fc3c47974c
@@ -1,10 +1,10 @@
1
- # The Pair class represents a pair of two pieces of data.
1
+ # The Pair class represents a pair of two pieces of data. The attributes are readers for custom = behavior.
2
2
  class Pair
3
3
  # @return [Object] The left data.
4
- attr_accessor :left
4
+ attr_reader :left
5
5
 
6
6
  # @return [Object] The right data.
7
- attr_accessor :right
7
+ attr_reader :right
8
8
 
9
9
  # Creates a new pair.
10
10
  # @param left [Object] The left piece of data.
@@ -14,6 +14,7 @@ class Pair
14
14
  def initialize(left, right)
15
15
  @left = left
16
16
  @right = right
17
+ @frozen = false
17
18
  end
18
19
 
19
20
  # Gets a string representation of the pair.
@@ -72,4 +73,60 @@ class Pair
72
73
  @right = left
73
74
  self
74
75
  end
76
+
77
+ # Returns true if self and other have the same left and right values.
78
+ # @param other [Pair] The other Pair
79
+ # @return [Boolean] Whether they are equivalent.
80
+ def eql?(other)
81
+ @left == other.left && @right == other.right
82
+ end
83
+
84
+ alias == eql?
85
+
86
+ # Returns whether self is frozen.
87
+ # @return [Boolean] Whether self is frozen.
88
+ def frozen?
89
+ @frozen
90
+ end
91
+
92
+ # Deep-freezes the Pair, freezing left and right, as well as the object itself.
93
+ # @return [Pair] self.
94
+ def freeze
95
+ unless frozen?
96
+ @left.freeze
97
+ @right.freeze
98
+ @frozen = true
99
+ end
100
+ self
101
+ end
102
+
103
+ # Sets right to the value.
104
+ # @raise RuntimeError when self is frozen.
105
+ # @param new [Any] The new right value.
106
+ # @return [void]
107
+ def right=(new)
108
+ if frozen?
109
+ raise RuntimeError.new("can't modify frozen Pair")
110
+ else
111
+ @right = new
112
+ end
113
+ end
114
+
115
+ # Sets left to the value.
116
+ # @raise RuntimeError when self is frozen.
117
+ # @param new [Any] The new left value.
118
+ # @return [void]
119
+ def left=(new)
120
+ if frozen?
121
+ raise RuntimeError.new("can't modify frozen Pair")
122
+ else
123
+ @left = new
124
+ end
125
+ end
126
+
127
+ # Returns a new copied version of the Pair, with copied values.
128
+ # @return [Pair] The new Pair
129
+ def clone
130
+ Pair.new(@left.dup, @right.dup)
131
+ end
75
132
  end
@@ -1,13 +1,13 @@
1
- # Triplet class represents a set of 3 pieces of data.
1
+ # Triplet class represents a set of 3 pieces of data. The attributes are readers for custom = behavior.
2
2
  class Triplet
3
3
  # @return [Object] The first data piece.
4
- attr_accessor :left
4
+ attr_reader :left
5
5
 
6
6
  # @return [Object] The second data piece.
7
- attr_accessor :middle
7
+ attr_reader :middle
8
8
 
9
9
  # @return [Object] The third data piece.
10
- attr_accessor :right
10
+ attr_reader :right
11
11
 
12
12
  # Creates a new Triplet.
13
13
  # @param left [Object] The first data.
@@ -18,6 +18,7 @@ class Triplet
18
18
  @left = left
19
19
  @middle = middle
20
20
  @right = right
21
+ @frozen = false
21
22
  end
22
23
 
23
24
  # @return [String] The stringified version of the Triplet.
@@ -77,4 +78,73 @@ class Triplet
77
78
  @left = right
78
79
  self
79
80
  end
81
+
82
+ # Returns true if self and other have the same left and right values.
83
+ # @param other [Triplet] The other Triplet
84
+ # @return [Boolean] Whether they are equivalent.
85
+ def eql?(other)
86
+ @left == other.left &&@middle == other.middle && @right == other.right
87
+ end
88
+
89
+ alias == eql?
90
+
91
+ # Returns whether self is frozen.
92
+ # @return [Boolean] Whether self is frozen.
93
+ def frozen?
94
+ @frozen
95
+ end
96
+
97
+ # Deep-freezes the Triplet, freezing left, middle, and right, as well as the object itself.
98
+ # @return [Triplet] self.
99
+ def freeze
100
+ unless frozen?
101
+ @left.freeze
102
+ @middle.freeze
103
+ @right.freeze
104
+ @frozen = true
105
+ end
106
+ self
107
+ end
108
+
109
+ # Sets right to the value.
110
+ # @raise RuntimeError when self is frozen.
111
+ # @param new [Any] The new right value.
112
+ # @return [void]
113
+ def right=(new)
114
+ if frozen?
115
+ raise RuntimeError.new("can't modify frozen Triplet")
116
+ else
117
+ @right = new
118
+ end
119
+ end
120
+
121
+ # Sets middle to the value.
122
+ # @raise RuntimeError when self is frozen.
123
+ # @param new [Any] The new middle value.
124
+ # @return [void]
125
+ def middle=(new)
126
+ if frozen?
127
+ raise RuntimeError.new("can't modify frozen Triplet")
128
+ else
129
+ @middle = new
130
+ end
131
+ end
132
+
133
+ # Sets left to the value.
134
+ # @raise RuntimeError when self is frozen.
135
+ # @param new [Any] The new left value.
136
+ # @return [void]
137
+ def left=(new)
138
+ if frozen?
139
+ raise RuntimeError.new("can't modify frozen Triplet")
140
+ else
141
+ @left = new
142
+ end
143
+ end
144
+
145
+ # Returns a new copied version of the Triplet, with copied values.
146
+ # @return [Triplet] The new Triplet
147
+ def clone
148
+ Triplet.new(@left.dup, @middle.dup, @right.dup)
149
+ end
80
150
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_types
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-26 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: elifosterwy@gmail.com
@@ -19,8 +19,6 @@ files:
19
19
  - lib/data_types.rb
20
20
  - lib/data_types/pair.rb
21
21
  - lib/data_types/triplet.rb
22
- - spec/pair_test_cases.rb
23
- - spec/triplet_test_cases.rb
24
22
  homepage: https://github.com/elifoster/data_types
25
23
  licenses:
26
24
  - CC-BY-NC-ND-4.0
@@ -42,9 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
40
  version: '0'
43
41
  requirements: []
44
42
  rubyforge_project:
45
- rubygems_version: 2.4.5.1
43
+ rubygems_version: 2.5.1
46
44
  signing_key:
47
45
  specification_version: 4
48
46
  summary: A Ruby gem that adds new object types like pairs and triplets.
49
47
  test_files: []
50
- has_rdoc:
@@ -1,53 +0,0 @@
1
- require 'test/unit'
2
- require_relative '../lib/data_types/pair'
3
-
4
- class PairTestCases < Test::Unit::TestCase
5
- def test_string
6
- pair = Pair.new(1, 2)
7
- assert_equal('1, 2', pair.to_s)
8
- end
9
-
10
- def test_array
11
- pair = Pair.new(1, 2)
12
- assert_equal([1, 2], pair.to_a)
13
- end
14
-
15
- def test_hash
16
- pair = Pair.new(1, 2)
17
- assert_equal({ left: 1, right: 2 }, pair.to_h)
18
- end
19
-
20
- def test_include?
21
- pair = Pair.new(1, 2)
22
- assert_equal(true, pair.include?(1))
23
- assert_equal(true, pair.include?(2))
24
- assert_equal(false, pair.include?(3))
25
- end
26
-
27
- def test_flip
28
- pair = Pair.new(1, 2)
29
- assert_equal(1, pair.flip.right)
30
- assert_equal(2, pair.flip.left)
31
- end
32
-
33
- def test_flip!
34
- pair = Pair.new(1, 2)
35
- pair.flip!
36
- assert_equal(1, pair.right)
37
- assert_equal(2, pair.left)
38
- end
39
-
40
- def test_empty?
41
- pair = Pair.new(1, 2)
42
- assert_equal(false, pair.empty?)
43
- pair.clear
44
- assert_equal(true, pair.empty?)
45
- end
46
-
47
- def test_clear
48
- pair = Pair.new(1, 2)
49
- pair.clear
50
- assert_nil(pair.left)
51
- assert_nil(pair.right)
52
- end
53
- end
@@ -1,57 +0,0 @@
1
- require 'test/unit'
2
- require_relative '../lib/data_types/triplet'
3
-
4
- class TripletTestCases < Test::Unit::TestCase
5
- def test_string
6
- triplet = Triplet.new(1, 2, 3)
7
- assert_equal('1, 2, 3', triplet.to_s)
8
- end
9
-
10
- def test_array
11
- triplet = Triplet.new(1, 2, 3)
12
- assert_equal([1, 2, 3], triplet.to_a)
13
- end
14
-
15
- def test_hash
16
- triplet = Triplet.new(1, 2, 3)
17
- assert_equal({ left: 1, middle: 2, right: 3 }, triplet.to_h)
18
- end
19
-
20
- def test_include?
21
- triplet = Triplet.new(1, 2, 3)
22
- assert_equal(true, triplet.include?(1))
23
- assert_equal(true, triplet.include?(2))
24
- assert_equal(true, triplet.include?(3))
25
- assert_equal(false, triplet.include?(4))
26
- end
27
-
28
- def test_flip
29
- triplet = Triplet.new(1, 2, 3)
30
- assert_equal(1, triplet.flip.right)
31
- assert_equal(2, triplet.flip.middle)
32
- assert_equal(3, triplet.flip.left)
33
- end
34
-
35
- def test_flip!
36
- triplet = Triplet.new(1, 2, 3)
37
- triplet.flip!
38
- assert_equal(1, triplet.right)
39
- assert_equal(2, triplet.middle)
40
- assert_equal(3, triplet.left)
41
- end
42
-
43
- def test_empty?
44
- triplet = Triplet.new(1, 2, 3)
45
- assert_equal(false, triplet.empty?)
46
- triplet.clear
47
- assert_equal(true, triplet.empty?)
48
- end
49
-
50
- def test_clear
51
- triplet = Triplet.new(1, 2, 3)
52
- triplet.clear
53
- assert_nil(triplet.left)
54
- assert_nil(triplet.middle)
55
- assert_nil(triplet.right)
56
- end
57
- end