youyouaidi 0.0.3 → 0.0.4

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: 1348322fab3186ecf944dd41ca64cae5316b4648
4
- data.tar.gz: f469604e1089be795b4d49bb533af942be2af1d2
3
+ metadata.gz: 1713ea510982e9d0ef36b7ea4b5601342bbb7673
4
+ data.tar.gz: 883a63e7466046595d329ae52bae3739b2275ad7
5
5
  SHA512:
6
- metadata.gz: 577327867e66034cf00973254f133b173c8799545916bb4403e7df4adcaf96c7f0f68570ebe620ef7b852c0110598857e667266b8cf5264ff4246ca3625065de
7
- data.tar.gz: 90418860a463cba9a14a5cfad83ae40fed522ca6fce33ecdd611c2bfbfc7f8c2e6ce1a06f6544426d4dcba3d0f0f41346270afee0b8369e03de3581efddb1058
6
+ metadata.gz: 957ee30738eb6f2d5d1bb7520ecb2768148bc2a4d6d90d3e725804f553034e446f34b669bfabc14e3a29e6dffc5731f86dd6e2262a2b2da6c3979b269776f484
7
+ data.tar.gz: 4a9520ae3df9cfbc6aa393d02c867a95eb690cfcff6bab6a2693f7e3a0ffd3cb9d3a4d74ed79e6beb537d1fdb471769f84848c91a74706c16bb2ae7ebc512fcb
data/README.md CHANGED
@@ -22,24 +22,58 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ ### Initializing UUIDs
26
+
25
27
  ```ruby
26
28
  uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
27
29
  uuid_short = '_oGOAbD9fsFFEHWSMal1v' # Same UUID in its short format
28
30
 
29
- Youyouaidi::UUID.valid? uuid_string # => true
30
-
31
31
  uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
32
32
  # => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
33
- # or alternatively a short UUID can be passed
33
+
34
+ # Alternatively a short UUID can be passed:
34
35
  uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
35
36
  # => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
37
+ ```
38
+
39
+
40
+ ### Validity check and conversions
41
+
42
+ ```ruby
43
+ uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
44
+ uuid = UUID uuid_string
45
+
46
+ Youyouaidi::UUID.valid? uuid_string # => true
36
47
 
37
48
  uuid.to_s # Returns the string representation of the UUID object
38
49
  # => '550e8400-e29b-41d4-a716-446655440000'
50
+
39
51
  uuid.to_short_s # Returns the short string representation of the UUID object
40
52
  # => '_oGOAbD9fsFFEHWSMal1v', alias for method: #to_param
41
53
  ```
42
54
 
55
+
56
+ ### Comparing UUIDs
57
+
58
+ ```ruby
59
+ uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
60
+ uuid = UUID uuid_string
61
+ similar_uuid = UUID uuid_string
62
+ other_uuid = UUID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
63
+
64
+ uuid == similar_uuid # Two UUID objects representing same UUID (#=== behaves similar for this)
65
+ # => true
66
+
67
+ uuid == other_uuid # Two UUID objects representing different UUIDs (#=== behaves similar for this)
68
+ # => false
69
+
70
+ uuid == uuid_string # Comparing a UUID object and its string representation with `=='
71
+ # => false
72
+
73
+ uuid === uuid_string # Comparing a UUID object and its string representation with `===' (case insensetive)
74
+ # => true
75
+ ```
76
+
43
77
  ## Contributing
44
78
 
45
79
  1. Fork it ( http://github.com/<my-github-username>/youyouaidi/fork )
@@ -4,7 +4,17 @@ class Youyouaidi::UUID
4
4
  def initialize(uuid_string, options = {})
5
5
  @converter = options[:converter] || Youyouaidi::Converter
6
6
  raise Youyouaidi::InvalidUUIDError.new "`#{uuid_string}' could not be converted to valid UUID" unless self.class.valid? uuid_string
7
- @uuid = uuid_string.to_s
7
+ @uuid = uuid_string.to_s.downcase
8
+ end
9
+
10
+ def ==(other_object)
11
+ return false unless other_object.is_a? self.class
12
+ self.to_s == other_object.to_s
13
+ end
14
+
15
+ def ===(other_object)
16
+ return true if self == other_object
17
+ self.to_s == other_object.to_s.downcase
8
18
  end
9
19
 
10
20
  def to_i
@@ -1,3 +1,3 @@
1
1
  module Youyouaidi
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,212 @@
1
+ require 'spec_helper'
2
+
3
+ describe Youyouaidi::UUID do
4
+ describe '.new' do
5
+ let(:param) { '' }
6
+ let(:action) { Youyouaidi::UUID.new param }
7
+ subject { -> { action } }
8
+
9
+ context 'with valid uuid string' do
10
+ let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
11
+ subject { action }
12
+
13
+ it { should be_a Youyouaidi::UUID }
14
+ its(:to_s) { should eq param }
15
+ end
16
+
17
+ context 'with uuid in short format' do
18
+ let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
19
+ it { should raise_error Youyouaidi::InvalidUUIDError }
20
+ end
21
+
22
+ context 'with invalid uuid string' do
23
+ let(:param) { 'Kekse' }
24
+ it { should raise_error Youyouaidi::InvalidUUIDError }
25
+ end
26
+
27
+ context 'with non-uuid-string input' do
28
+ let(:param) { 1234 }
29
+ it { should raise_error Youyouaidi::InvalidUUIDError }
30
+ end
31
+ end
32
+
33
+ describe '.parse' do
34
+ let(:param) { '' }
35
+ let(:action) { Youyouaidi::UUID.parse param }
36
+ subject { action }
37
+
38
+ context 'with valid uuid string' do
39
+ let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
40
+
41
+ it { should be_a Youyouaidi::UUID }
42
+ end
43
+
44
+ context 'with uuid in short format' do
45
+ let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
46
+ let(:decoded_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
47
+
48
+ it { should be_a Youyouaidi::UUID }
49
+ its(:to_s) { should eq decoded_uuid }
50
+ end
51
+
52
+ context 'with invalid uuid string' do
53
+ let(:param) { 'Kekse' }
54
+
55
+ it 'raises error' do
56
+ expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
57
+ end
58
+ end
59
+
60
+ context 'with non-string input' do
61
+ let(:param) { 1234 }
62
+
63
+ it 'raises error' do
64
+ expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
65
+ end
66
+ end
67
+ end
68
+
69
+ shared_examples_for 'equality check for two UUID objects' do
70
+ let(:first_uuid_string) { 'aaaaaaaa-eeee-4444-aaaa-444444444444' }
71
+ let(:second_uuid_string) { '00000000-bbbb-2222-6666-000000000000' }
72
+ let(:first_uuid) { Youyouaidi::UUID.new first_uuid_string }
73
+ let(:second_uuid) { Youyouaidi::UUID.new second_uuid_string }
74
+
75
+ subject { action }
76
+
77
+ context 'passing a UUID object' do
78
+ context 'when comparing same instance' do
79
+ let(:action) { first_uuid.send described_method, first_uuid }
80
+ it { should be_true }
81
+ end
82
+
83
+ context 'when comparing different instances' do
84
+ let(:action) { first_uuid.send described_method, second_uuid }
85
+ context 'with same UUID strings' do
86
+ let(:second_uuid_string) { first_uuid_string }
87
+ it { should be_true }
88
+ end
89
+
90
+ context 'with different UUID strings' do
91
+ it { should be_false }
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '#== (equal operator)' do
98
+ let(:uuid_string) { 'aaaaaaaa-eeee-4444-aaaa-444444444444' }
99
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
100
+
101
+ let(:described_method) { :== }
102
+ it_behaves_like 'equality check for two UUID objects'
103
+
104
+ subject { action }
105
+
106
+ context 'passing a non-UUID object' do
107
+ let(:action) { uuid == test_object }
108
+
109
+ context 'when this is the UUID as string' do
110
+ let(:test_object) { uuid_string }
111
+ it { should be_false }
112
+ end
113
+
114
+ context 'when this is a random other object' do
115
+ let(:test_object) { '123' }
116
+ it { should be_false }
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#=== (equal operator)' do
122
+ let(:uuid_string) { 'aaaaaaaa-eeee-4444-aaaa-444444444444' }
123
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
124
+
125
+ let(:described_method) { :=== }
126
+ it_behaves_like 'equality check for two UUID objects'
127
+
128
+ subject { action }
129
+
130
+ context 'passing a non-UUID object' do
131
+ let(:action) { uuid === test_object }
132
+
133
+ context 'when this is the same UUID as string' do
134
+ context 'when string is upcase' do
135
+ let(:test_object) { uuid_string.upcase }
136
+ it { should be_true }
137
+ end
138
+ context 'when string is downcase' do
139
+ let(:test_object) { uuid_string.downcase }
140
+ it { should be_true }
141
+ end
142
+ end
143
+
144
+ context 'when this is a random other object' do
145
+ let(:test_object) { '123' }
146
+ it { should be_false }
147
+ end
148
+ end
149
+ end
150
+
151
+ describe '#to_s' do
152
+ let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
153
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
154
+ subject { uuid.to_s }
155
+
156
+ it { should be_a String }
157
+ it { should eq uuid_string }
158
+ end
159
+
160
+ describe '#to_i' do
161
+ let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
162
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
163
+ subject { uuid.to_i }
164
+
165
+ it { should be_a Bignum }
166
+ it { should eq 113059749145936325402354257176981405696 }
167
+ end
168
+
169
+ shared_examples_for 'method for short format' do
170
+ let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
171
+ let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
172
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
173
+
174
+ let(:action) { uuid.send method }
175
+ subject { action }
176
+
177
+ it { should be_a String }
178
+ it { should eq encoded_uuid }
179
+ end
180
+ describe '#to_short_string' do
181
+ let(:method) { :to_short_string }
182
+ it_behaves_like 'method for short format'
183
+ end
184
+ describe '#to_param' do
185
+ let(:method) { :to_param }
186
+ it_behaves_like 'method for short format'
187
+ end
188
+
189
+ describe '.valid?' do
190
+ let(:param) { '' }
191
+ subject { Youyouaidi::UUID.valid? param }
192
+
193
+ context 'with valid uuid' do
194
+ let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
195
+
196
+ it { should be_true }
197
+ end
198
+
199
+ context 'with invalid uuid' do
200
+ uuid_string = '550e8400-e29b-41d4-a716-446655440000'
201
+ encoded_uuid = '_oGOAbD9fsFFEHWSMal1v'
202
+ invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
203
+ "#{encoded_uuid}" ]
204
+
205
+ invalid_uuids.each do |invalid_uuid|
206
+ it "should return false for `#{invalid_uuid}`" do
207
+ expect(Youyouaidi::UUID.valid? invalid_uuid).to eq false
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youyouaidi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Fricke
@@ -61,7 +61,7 @@ files:
61
61
  - spec/kernel_patch_spec.rb
62
62
  - spec/spec_helper.rb
63
63
  - spec/youyouaidi/converter_spec.rb
64
- - spec/youyouaidi_spec.rb
64
+ - spec/youyouaidi/uuid_spec.rb
65
65
  - youyouaidi.gemspec
66
66
  homepage: https://github.com/nicolas-fricke/youyouaidi
67
67
  licenses:
@@ -91,5 +91,5 @@ test_files:
91
91
  - spec/kernel_patch_spec.rb
92
92
  - spec/spec_helper.rb
93
93
  - spec/youyouaidi/converter_spec.rb
94
- - spec/youyouaidi_spec.rb
94
+ - spec/youyouaidi/uuid_spec.rb
95
95
  has_rdoc:
@@ -1,131 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Youyouaidi do
4
- describe Youyouaidi::UUID do
5
- describe '.new' do
6
- let(:param) { '' }
7
- let(:action) { Youyouaidi::UUID.new param }
8
- subject { -> { action } }
9
-
10
- context 'with valid uuid string' do
11
- let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
12
- subject { action }
13
-
14
- it { should be_a Youyouaidi::UUID }
15
- end
16
-
17
- context 'with uuid in short format' do
18
- let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
19
- it { should raise_error Youyouaidi::InvalidUUIDError }
20
- end
21
-
22
- context 'with invalid uuid string' do
23
- let(:param) { 'Kekse' }
24
- it { should raise_error Youyouaidi::InvalidUUIDError }
25
- end
26
-
27
- context 'with non-uuid-string input' do
28
- let(:param) { 1234 }
29
- it { should raise_error Youyouaidi::InvalidUUIDError }
30
- end
31
- end
32
-
33
- describe '.parse' do
34
- let(:param) { '' }
35
- let(:action) { Youyouaidi::UUID.parse param }
36
- subject { action }
37
-
38
- context 'with valid uuid string' do
39
- let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
40
-
41
- it { should be_a Youyouaidi::UUID }
42
- end
43
-
44
- context 'with uuid in short format' do
45
- let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
46
- let(:decoded_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
47
-
48
- it { should be_a Youyouaidi::UUID }
49
- its(:to_s) { should eq decoded_uuid }
50
- end
51
-
52
- context 'with invalid uuid string' do
53
- let(:param) { 'Kekse' }
54
-
55
- it 'raises error' do
56
- expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
57
- end
58
- end
59
-
60
- context 'with non-string input' do
61
- let(:param) { 1234 }
62
-
63
- it 'raises error' do
64
- expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
65
- end
66
- end
67
- end
68
-
69
- describe '#to_s' do
70
- let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
71
- let(:uuid) { Youyouaidi::UUID.new uuid_string }
72
- subject { uuid.to_s }
73
-
74
- it { should be_a String }
75
- it { should eq uuid_string }
76
- end
77
-
78
- describe '#to_i' do
79
- let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
80
- let(:uuid) { Youyouaidi::UUID.new uuid_string }
81
- subject { uuid.to_i }
82
-
83
- it { should be_a Bignum }
84
- it { should eq 113059749145936325402354257176981405696 }
85
- end
86
-
87
- shared_examples_for 'method for short format' do
88
- let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
89
- let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
90
- let(:uuid) { Youyouaidi::UUID.new uuid_string }
91
-
92
- let(:action) { uuid.send method }
93
- subject { action }
94
-
95
- it { should be_a String }
96
- it { should eq encoded_uuid }
97
- end
98
- describe '#to_short_string' do
99
- let(:method) { :to_short_string }
100
- it_behaves_like 'method for short format'
101
- end
102
- describe '#to_param' do
103
- let(:method) { :to_param }
104
- it_behaves_like 'method for short format'
105
- end
106
-
107
- describe '.valid?' do
108
- let(:param) { '' }
109
- subject { Youyouaidi::UUID.valid? param }
110
-
111
- context 'with valid uuid' do
112
- let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
113
-
114
- it { should be_true }
115
- end
116
-
117
- context 'with invalid uuid' do
118
- uuid_string = '550e8400-e29b-41d4-a716-446655440000'
119
- encoded_uuid = '_oGOAbD9fsFFEHWSMal1v'
120
- invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
121
- "#{encoded_uuid}" ]
122
-
123
- invalid_uuids.each do |invalid_uuid|
124
- it "should return false for `#{invalid_uuid}`" do
125
- expect(Youyouaidi::UUID.valid? invalid_uuid).to eq false
126
- end
127
- end
128
- end
129
- end
130
- end
131
- end