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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/structure.rb +7 -4
  3. data/structure_test.rb +15 -8
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4694de108ebd90aae0a0077a210dea2e5d4280ea
4
- data.tar.gz: f7c78c9ba1eed15bce911abdf71fdbac979a6d5f
3
+ metadata.gz: 543ffd06f050b41405d2760cd7acc6e5ba3fd4f7
4
+ data.tar.gz: c8bb827aaed4acde43fd70386724d1c92c9bd759
5
5
  SHA512:
6
- metadata.gz: d09ca4303c082e13fd42af04282aee4727f482159559a16bd2ea3847e6374f39da91bf7d9ffd245b585d574e8d7c425e72f81f576db7897f100f8d1b1e2721ac
7
- data.tar.gz: d8384a70e163d54a6e11eb7ae115b0346ee7ed6cdee441f2c90ce99d5c09fe7d2a180815dc02f01b3e266973de82465489964aea22da829a2b97093930375573
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 { |k, v| "#{k}=#{v.inspect}" }
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 { |k, v| self.send("#{k}=", v) }
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
- @attribute_names << define_method(name, &blk)
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: 100)
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 test_class_casts_to_struct
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: 100)
30
- assert_equal({ latitude: 10, longitude: 100, name: 'foo' }, obj.attributes)
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 100, @location.longitude
42
+ assert_equal 20, @location.longitude
36
43
  end
37
44
 
38
45
  def test_returns_attributes
39
- assert_equal({ latitude: 10, longitude: 100 }, @location.attributes)
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: 100, latitude: 10)
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=100>', @location.inspect
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.2
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-09 00:00:00.000000000 Z
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: '2.1'
62
+ version: '1.9'
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - ">="