structure 0.27.0 → 0.27.1

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 +49 -0
  3. data/structure_test.rb +53 -0
  4. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 341847011b94385b6f9db184126c65b671aaea6a
4
- data.tar.gz: 479f90296cf23777214e967acbe1f19529edb905
3
+ metadata.gz: 20c2098f812188505392409a1c8b0e17493a5168
4
+ data.tar.gz: 73f8e68576effb6d797666917539b994acdcff19
5
5
  SHA512:
6
- metadata.gz: 39a77be13c27e5b73f3ef9e7e3dcf7c1ac51636b409de4f7dcd761f6bcaee6e8454f7c29742bb1f3707e7907924599a00843b70e9c0ecc77242927a40e7ea799
7
- data.tar.gz: 488720d77236008080d9dedeba9cf31c57d17085a3f60a57c2f71c1309e1f7261c5386100fe746bfc038448ba3bc505bc04deec76abee99a486e0489c6581ffa
6
+ metadata.gz: 2823dd41bbc096c9eed4c1ef3efc23ea74f230ef65c854929a169def3176b0dc65d250e1840af1e225dc3f5b527475b1457808f57ada095d673d82b4f17ce35f
7
+ data.tar.gz: 6bf92a74813ac1a1eea066b09aeae15ffc8c58acc32a4638624ab0d4f312aebb0af8679bdca8ea4cbbc0bbc7c4df4e7c7cf64c387e8909967e371daf1c5295c3
data/structure.rb ADDED
@@ -0,0 +1,49 @@
1
+ module Structure
2
+ def self.included(base)
3
+ base
4
+ .extend(ClassMethods)
5
+ .instance_variable_set(:@attribute_names, [])
6
+ end
7
+
8
+ def attributes
9
+ self.class.attribute_names.reduce({}) { |ret, name|
10
+ ret.update(name => self.send(name))
11
+ }
12
+ end
13
+
14
+ def ==(other)
15
+ attributes == other.attributes
16
+ end
17
+
18
+ def inspect
19
+ "#<#{self.class} #{
20
+ attributes
21
+ .map { |k, v| "#{k}=#{v.inspect}" }
22
+ .join(', ')
23
+ }>"
24
+ end
25
+
26
+ alias_method :to_h, :attributes
27
+ alias_method :eql?, :==
28
+ alias_method :to_s, :inspect
29
+
30
+ module ClassMethods
31
+ attr_reader :attribute_names
32
+
33
+ def to_struct
34
+ Struct.new(name, *attribute_names) do
35
+ def initialize(data = {})
36
+ data.each { |k, v| self.send("#{k}=", v) }
37
+ end
38
+ end
39
+ end
40
+
41
+ def inherited(subclass)
42
+ subclass.instance_variable_set(:@attribute_names, attribute_names.dup)
43
+ end
44
+
45
+ def attribute(name, &blk)
46
+ @attribute_names << define_method(name, &blk)
47
+ end
48
+ end
49
+ end
data/structure_test.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './structure'
4
+
5
+ Location = Struct.new(:res) do
6
+ include Structure
7
+ [:latitude, :longitude].each { |key| attribute(key) { res.fetch(key) } }
8
+ end
9
+
10
+ class StructureTest < MiniTest::Test
11
+ def setup
12
+ @location = Location.new(latitude: 10, longitude: 100)
13
+ end
14
+
15
+ def test_class_returns_attribute_names
16
+ assert_equal [:latitude, :longitude], Location.attribute_names
17
+ end
18
+
19
+ def test_class_casts_to_struct
20
+ struct = Location.to_struct
21
+ assert_equal 'Struct::Location', struct.name
22
+ assert_equal 1, struct.new(latitude: 1).latitude
23
+ end
24
+
25
+ def test_subclassing_does_not_have_side_effects
26
+ subclass = Class.new(Location) do
27
+ attribute(:name) { 'foo' }
28
+ end
29
+ obj = subclass.new(latitude: 10, longitude: 100)
30
+ assert_equal({ latitude: 10, longitude: 100, name: 'foo' }, obj.attributes)
31
+ end
32
+
33
+ def test_attributes
34
+ assert_equal 10, @location.latitude
35
+ assert_equal 100, @location.longitude
36
+ end
37
+
38
+ def test_returns_attributes
39
+ assert_equal({ latitude: 10, longitude: 100 }, @location.attributes)
40
+ assert_equal @location.to_h, @location.attributes
41
+ end
42
+
43
+ def test_compares
44
+ @other = Location.new(longitude: 100, latitude: 10)
45
+ assert @location == @other
46
+ assert @location.eql?(@other)
47
+ end
48
+
49
+ def test_pretty_inspects
50
+ assert_equal '#<Location latitude=10, longitude=100>', @location.inspect
51
+ assert_equal @location.to_s, @location.inspect
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.0
4
+ version: 0.27.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
@@ -46,6 +46,8 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - LICENSE
48
48
  - README.md
49
+ - structure.rb
50
+ - structure_test.rb
49
51
  homepage:
50
52
  licenses: []
51
53
  metadata: {}