structure 0.27.2 → 0.27.3
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 +4 -4
- data/structure.rb +7 -4
- data/structure_test.rb +15 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 543ffd06f050b41405d2760cd7acc6e5ba3fd4f7
|
4
|
+
data.tar.gz: c8bb827aaed4acde43fd70386724d1c92c9bd759
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a9a4282f5186062d454f901f1b518afd9054fd8041add41ac97339ba6a01cef52581113eb03bdb31244aa4b2f29d4db55b7f80553032beaeb5ac281132a101b
|
7
|
+
data.tar.gz: 5484b1f51b1c8c6ff7fc0e6e0200fba2b703fddfa38de3782c1df37943ac9e387e858f7c227005a69bbd28d93839c928152828dba8ef9c11688a9a0c3af0f632
|
data/structure.rb
CHANGED
@@ -18,7 +18,7 @@ module Structure
|
|
18
18
|
def inspect
|
19
19
|
"#<#{self.class} #{
|
20
20
|
attributes
|
21
|
-
.map { |
|
21
|
+
.map { |key, val| "#{key}=#{val.inspect}" }
|
22
22
|
.join(', ')
|
23
23
|
}>"
|
24
24
|
end
|
@@ -31,19 +31,22 @@ module Structure
|
|
31
31
|
attr_reader :attribute_names
|
32
32
|
|
33
33
|
def to_struct
|
34
|
+
return Struct.const_get(name, false) if Struct.const_defined?(name, false)
|
35
|
+
|
34
36
|
Struct.new(name, *attribute_names) do
|
35
37
|
def initialize(data = {})
|
36
|
-
data.each { |
|
38
|
+
data.each { |key, val| self.send("#{key}=", val) }
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
43
|
def inherited(subclass)
|
42
|
-
subclass.instance_variable_set(:@attribute_names, attribute_names.dup)
|
44
|
+
subclass.instance_variable_set(:@attribute_names, @attribute_names.dup)
|
43
45
|
end
|
44
46
|
|
45
47
|
def attribute(name, &blk)
|
46
|
-
|
48
|
+
define_method(name, &blk)
|
49
|
+
@attribute_names << name
|
47
50
|
end
|
48
51
|
end
|
49
52
|
end
|
data/structure_test.rb
CHANGED
@@ -9,45 +9,52 @@ end
|
|
9
9
|
|
10
10
|
class StructureTest < MiniTest::Test
|
11
11
|
def setup
|
12
|
-
@location = Location.new(latitude: 10, longitude:
|
12
|
+
@location = Location.new(latitude: 10, longitude: 20)
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_class_returns_attribute_names
|
16
16
|
assert_equal [:latitude, :longitude], Location.attribute_names
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def test_casts_itself_to_struct
|
20
20
|
struct = Location.to_struct
|
21
21
|
assert_equal 'Struct::Location', struct.name
|
22
22
|
assert_equal 1, struct.new(latitude: 1).latitude
|
23
23
|
end
|
24
24
|
|
25
|
+
def test_cast_to_struct_only_once
|
26
|
+
out, err = capture_io do
|
27
|
+
2.times { Location.to_struct }
|
28
|
+
end
|
29
|
+
assert_empty err
|
30
|
+
end
|
31
|
+
|
25
32
|
def test_subclassing_does_not_have_side_effects
|
26
33
|
subclass = Class.new(Location) do
|
27
34
|
attribute(:name) { 'foo' }
|
28
35
|
end
|
29
|
-
obj = subclass.new(latitude: 10, longitude:
|
30
|
-
assert_equal({ latitude: 10, longitude:
|
36
|
+
obj = subclass.new(latitude: 10, longitude: 20)
|
37
|
+
assert_equal({ latitude: 10, longitude: 20, name: 'foo' }, obj.attributes)
|
31
38
|
end
|
32
39
|
|
33
40
|
def test_attributes
|
34
41
|
assert_equal 10, @location.latitude
|
35
|
-
assert_equal
|
42
|
+
assert_equal 20, @location.longitude
|
36
43
|
end
|
37
44
|
|
38
45
|
def test_returns_attributes
|
39
|
-
assert_equal({ latitude: 10, longitude:
|
46
|
+
assert_equal({ latitude: 10, longitude: 20 }, @location.attributes)
|
40
47
|
assert_equal @location.to_h, @location.attributes
|
41
48
|
end
|
42
49
|
|
43
50
|
def test_compares
|
44
|
-
@other = Location.new(longitude:
|
51
|
+
@other = Location.new(longitude: 20, latitude: 10)
|
45
52
|
assert @location == @other
|
46
53
|
assert @location.eql?(@other)
|
47
54
|
end
|
48
55
|
|
49
56
|
def test_pretty_inspects
|
50
|
-
assert_equal '#<Location latitude=10, longitude=
|
57
|
+
assert_equal '#<Location latitude=10, longitude=20>', @location.inspect
|
51
58
|
assert_equal @location.to_s, @location.inspect
|
52
59
|
end
|
53
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.27.
|
4
|
+
version: 0.27.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hakan Ensari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
59
|
requirements:
|
60
60
|
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '
|
62
|
+
version: '1.9'
|
63
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - ">="
|