polyfill-data 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fc564f62f95089c90ab7c84d558694513c676e2720e30a5587fda794b0446e2
4
- data.tar.gz: 2466958db19b441691124b8085c60dbb16ad9522b257aa647bb5d816f6bb0b42
3
+ metadata.gz: 80e9e19a876c3cbc13a20fb597baa45c3a5a17938c20b6cee2853facfbba5381
4
+ data.tar.gz: 9e64aea3b7c8a184a5887645ed4755268344a417a05676b96d51a6f9e16314ca
5
5
  SHA512:
6
- metadata.gz: '09a20ba5e0db01e1693fd27f63eafcd62e3cb6e7dbf49bc9f061e7bec5f9259f3cef29de156f5319791059ac152a169157dfbd4375b13e2f73f551511fb256b7'
7
- data.tar.gz: 056fa220c31d080a4b3c3a68d606772bc8791495d42abcad66e80d59146568255d45a613411741223b82ddcc34ee5f3978099ed2aff79e4a9257d6720331dbff
6
+ metadata.gz: 25002a3731483f15d8a2ddb33ec15a3b023fe92188b29587b4c1155a364c759dd499cd60af701169440c1666852e72959c9403ef69505febb445db71c83feab9
7
+ data.tar.gz: 869b512afad6f1e9b8c0d45300ebf69a0bee3ac95cd9fa5763c171866cbde0a0f89664da53fc2a5b840beb9f15950bb686ac85d905a2b4562164c70537fbdaf5
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 1.0.1
9
+
10
+ ### Added
11
+
12
+ - `define` uses the first argument as a constant name if it is a string
13
+ - `[]` class method as an alias to `new`
14
+ - `to_s` method as an alias to `inspect`
15
+ - `deconstruct` method
16
+
17
+ ### Fixed
18
+
19
+ - `initialize` only receives keyword arguments
20
+ - `inspect` includes comma separated list of members and values
21
+
22
+ ## 1.0.0
23
+
24
+ ### Added
25
+
26
+ - Initial implementation
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Polyfill - Data
2
2
 
3
- Add the Ruby 3.2 Data class to earlier Rubies.
3
+ Add the Ruby 3.2 [Data](https://docs.ruby-lang.org/en/3.2/Data.html) class to earlier Rubies.
4
4
 
5
5
  ## Installation
6
6
 
@@ -14,7 +14,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
14
 
15
15
  Polyfill-Data is cryptographically signed. To be sure the gem you install hasn’t been tampered with:
16
16
 
17
- Add my the key (if you haven’t already) as a trusted certificate
17
+ Add the public key (if you haven’t already) as a trusted certificate
18
18
 
19
19
  gem cert --add <(curl -Ls https://raw.github.com/saturnflyer/polyfill-data/main/certs/saturnflyer.pem)
20
20
 
data/lib/data.rb CHANGED
@@ -15,6 +15,11 @@ else
15
15
  raise ArgumentError if args.any?(/=/)
16
16
  klass = ::Class.new(self, &block)
17
17
 
18
+ if args.first.is_a?(String)
19
+ name = args.shift
20
+ Data.const_set(name, klass)
21
+ end
22
+
18
23
  klass.define_singleton_method(:members) { args.map{ _1.intern } }
19
24
 
20
25
  klass.define_singleton_method(:new) do |*new_args, **new_kwargs, &block|
@@ -31,6 +36,9 @@ else
31
36
  instance.send(:initialize, **args_to_hash, &block)
32
37
  end
33
38
  end
39
+ class << klass
40
+ alias_method :[], :new
41
+ end
34
42
 
35
43
  args.map do |arg|
36
44
  if klass.method_defined?(arg)
@@ -48,16 +56,12 @@ else
48
56
  self.class.members
49
57
  end
50
58
 
51
- def initialize(*args, **kwargs)
52
- @attributes = case
53
- when args.empty? && kwargs.empty?
54
- {}
55
- when args.empty?
56
- kwargs
57
- else
58
- raise ArgumentError unless args.length == members.length
59
- Hash[members.zip(args)]
60
- end
59
+ def initialize(**kwargs)
60
+ @attributes = kwargs
61
+ end
62
+
63
+ def deconstruct
64
+ to_a
61
65
  end
62
66
 
63
67
  def deconstruct_keys(array)
@@ -67,6 +71,10 @@ else
67
71
  @attributes.slice(*array)
68
72
  end
69
73
 
74
+ def to_a
75
+ @attributes.values
76
+ end
77
+
70
78
  def to_h(&block)
71
79
  @attributes.to_h(&block)
72
80
  end
@@ -89,8 +97,9 @@ else
89
97
  insect_key = key.to_s.start_with?("@") ? ":#{key}" : key
90
98
  "#{insect_key}=#{value}"
91
99
  end
92
- %(#<#{name} #{attribute_markers.join(" ")}>)
100
+ %(#<#{name} #{attribute_markers.join(", ")}>)
93
101
  end
102
+ alias_method :to_s, :inspect
94
103
 
95
104
  def with(**kwargs)
96
105
  self.class.new(*@attributes.merge(kwargs).values)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Polyfill
4
4
  module Data
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.1"
6
6
  end
7
7
  end
data/test/test_data.rb CHANGED
@@ -118,6 +118,9 @@ class TestData < Minitest::Test
118
118
  assert_equal({foo: 1}, test.deconstruct_keys(%i[foo baz]))
119
119
  assert_raises(TypeError) { test.deconstruct_keys(0) }
120
120
 
121
+ assert_equal([1, 2], test.deconstruct)
122
+ assert_equal([1, 2], test.to_a)
123
+
121
124
  assert_kind_of(Integer, test.hash)
122
125
  end
123
126
 
@@ -133,6 +136,11 @@ class TestData < Minitest::Test
133
136
  klass = Data.define(:@a)
134
137
  o = klass.new(1)
135
138
  assert_equal("#<data :@a=1>", o.inspect)
139
+
140
+ klass = Data.define(:one, :two)
141
+ o = klass.new(1,2)
142
+ assert_equal("#<data one=1, two=2>", o.inspect)
143
+ assert_equal("#<data one=1, two=2>", o.to_s)
136
144
  end
137
145
 
138
146
  def test_equal
@@ -167,4 +175,19 @@ class TestData < Minitest::Test
167
175
  assert_equal([], test.members)
168
176
  assert_equal({}, test.to_h)
169
177
  end
178
+
179
+ def test_square_braces
180
+ klass = Data.define(:amount, :unit)
181
+
182
+ distance = klass[10, 'km']
183
+
184
+ assert_equal(10, distance.amount)
185
+ assert_equal('km', distance.unit)
186
+ end
187
+
188
+ def test_namespaced_constant
189
+ klass = Data.define("Measure", :amount, :unit)
190
+
191
+ assert_equal(Data::Measure, klass)
192
+ end
170
193
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyfill-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
@@ -34,7 +34,7 @@ cert_chain:
34
34
  dxkiKqcX+yzo9RJLD9l/E1AWX125r1Fhiif4l6ehdl7Vllc3NQUOm1abdmHtCYjw
35
35
  dG3yPWBWzzN4ovoBRqsuTJbF1wjkCjl5ex5KhfYbeDc=
36
36
  -----END CERTIFICATE-----
37
- date: 2023-01-19 00:00:00.000000000 Z
37
+ date: 2023-01-20 00:00:00.000000000 Z
38
38
  dependencies: []
39
39
  description: Add the ruby 3.2 Data class to older rubies
40
40
  email:
@@ -43,6 +43,7 @@ executables: []
43
43
  extensions: []
44
44
  extra_rdoc_files: []
45
45
  files:
46
+ - CHANGELOG.md
46
47
  - README.md
47
48
  - Rakefile
48
49
  - certs/saturnflyer.pem
metadata.gz.sig CHANGED
@@ -1,3 +1 @@
1
-
2
- }d94@T'��ε\�,���Ӧ �;e����8F�x�K��*�����,� �++'qաt�ԝ�k �*,�㑩�Ci{&��H���8���ֱ7!#��R����ڎ˟M�A������.�p��뽏��c֭5�����D{C��[�l��D\��e��1>&�Z���^�:� !n�5�끼�V��8"�o�e��8mK�G*��F�l��� N�s��88:��c�'+"�ۊ�����Pt�,����<&��aV��A舀
3
- [:�I{y%[���Vi@���v���8�e���������sw���^���
1
+ �B��s���,3QWo���]�DU���5�R��Dr�O*�S0Ҏ�6���X@XK�skQ��1`s����ܜ�B@�b�T�F{�C@��,��Th,�7�:�"B@UT���T��Y �x8X]��)8}���_q�����B`Kt{6bD�d�sN9���)X����]$ 2ԧ&[8v�/Ί�]�x�MZ��He����~b+�� �'*���$�4 M�D=�C�E܃��)��b!J��WD�8Sa���`��~�:��g5�\}� �P��8����G�7�]j��b�W����g%) X]5�<b��.���T���t|@�T~�kWG�P���㳓J�Ω��`��w�=���O��榐H�G)Es�iL\�C��III+:�F