super_struct 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/super_struct/version.rb +1 -1
- data/lib/super_struct.rb +10 -5
- data/spec/super_struct_spec.rb +10 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e828a1d5d4b44e8871df9d42113a60f4c7e2efd2
|
4
|
+
data.tar.gz: f87691e91a80444b8538b7f93eb0e66dc6f753cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53a6edabef8341451c30f9d33a9b0c0855d20f4ea16518fe1ea6e3827d621ae42e5fa7350a0df9fc536abe8f0d5dc7721a7f2fe60e2c8a011c55f8d8c8aa9b22
|
7
|
+
data.tar.gz: 296fd4b9114b66769babcbbc424a9083d114b024e35c0bb7a0f0a72446af45e07c652b9e71212be5347a51e554385f65738abb57dd2f0bf0ce76a267cb021834
|
data/lib/super_struct/version.rb
CHANGED
data/lib/super_struct.rb
CHANGED
@@ -28,18 +28,23 @@ module SuperStruct
|
|
28
28
|
self[member].deep_convert!
|
29
29
|
end
|
30
30
|
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
other.respond_to?(:attributes) && attributes == other.attributes
|
31
36
|
end
|
32
37
|
end
|
33
38
|
|
34
|
-
def self.new(
|
35
|
-
keys = if
|
36
|
-
|
39
|
+
def self.new(*input, &block)
|
40
|
+
keys = if input.first.respond_to?(:has_key?)
|
41
|
+
input.first.keys.sort.map(&:to_sym)
|
37
42
|
else
|
38
|
-
|
43
|
+
input.sort.map(&:to_sym)
|
39
44
|
end
|
40
45
|
|
41
46
|
Struct.new(*keys, &block).tap do |struct|
|
42
|
-
struct.send(:include, InstanceMethods)
|
47
|
+
struct.send(:include, ::SuperStruct::InstanceMethods)
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
data/spec/super_struct_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SuperStruct do
|
4
|
-
let(:constructor_input) { { foo:
|
4
|
+
let(:constructor_input) { { foo: { fiz: :bang } } }
|
5
5
|
let(:klass) { SuperStruct.new(constructor_input) }
|
6
6
|
let(:input) { constructor_input }
|
7
7
|
subject { klass.new(input) }
|
@@ -39,10 +39,10 @@ describe SuperStruct do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
describe '#new' do
|
42
|
-
context 'given
|
43
|
-
subject {
|
42
|
+
context 'given an array as input' do
|
43
|
+
subject { ::SuperStruct.new(:foo, :biz).new(:bar, :bang) }
|
44
44
|
|
45
|
-
it '
|
45
|
+
it 'uses the array as keys' do
|
46
46
|
expect(subject.foo).to eq :bang
|
47
47
|
end
|
48
48
|
end
|
@@ -55,18 +55,18 @@ describe SuperStruct do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
describe '#deep_convert!' do
|
58
|
-
let(:input) { { foo: { fiz: :bang } } }
|
59
|
-
subject { SuperStruct.from_hash(input) }
|
60
|
-
|
61
58
|
it 'converts all hashes within itself to SuperStructs' do
|
62
59
|
expect {
|
63
60
|
subject.deep_convert!
|
64
61
|
}.to change {
|
65
62
|
subject.foo
|
66
63
|
}.from(Hash).to(Struct)
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#==' do
|
68
|
+
it 'compares with attributes (not class since structs can be anonymous)' do
|
69
|
+
expect(subject).to eq klass.new(input)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|