polyfill-data 1.0.0 → 1.0.2

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: 156f764bced601a2ebf8cc7a6ee095d63a4d3f813ee3e9fe7c3b273ce68f007f
4
+ data.tar.gz: d7425307509c331ac61c33541da58effcbbb1f5b6ce675ede734b27c2acf0ed3
5
5
  SHA512:
6
- metadata.gz: '09a20ba5e0db01e1693fd27f63eafcd62e3cb6e7dbf49bc9f061e7bec5f9259f3cef29de156f5319791059ac152a169157dfbd4375b13e2f73f551511fb256b7'
7
- data.tar.gz: 056fa220c31d080a4b3c3a68d606772bc8791495d42abcad66e80d59146568255d45a613411741223b82ddcc34ee5f3978099ed2aff79e4a9257d6720331dbff
6
+ metadata.gz: 76a56991f1759077af526ef30236fcde606cd7fdc4dd9b5264e6d3012ad02e5a27feafa2dcf6900a54cd47e25525ee221702e07f41b6178f5cbb3cb7c4c2abfc
7
+ data.tar.gz: 995d6359685044724e2fda6a31e8dab8c73cbdee97dc49034ef7c45ee39a04a3a05ab7ec3046f78705e931316e2957a0992cbe3b3a36c1fd7d43794e89ee22ca
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
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.2
9
+
10
+ ### Fixed
11
+
12
+ - freeze after initializing
13
+ - `to_a` removed
14
+ - `deconstruct` fixed to return array in order of members as defined
15
+
16
+ ## 1.0.1
17
+
18
+ ### Added
19
+
20
+ - `define` uses the first argument as a constant name if it is a string
21
+ - `[]` class method as an alias to `new`
22
+ - `to_s` method as an alias to `inspect`
23
+ - `deconstruct` method
24
+
25
+ ### Fixed
26
+
27
+ - `initialize` only receives keyword arguments
28
+ - `inspect` includes comma separated list of members and values
29
+
30
+ ## 1.0.0
31
+
32
+ ### Added
33
+
34
+ - 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,21 +15,27 @@ 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|
21
- arg_comparison = new_kwargs.any? ? new_kwargs.keys : new_args
22
- if arg_comparison.size != args.size
23
- raise ArgumentError
26
+
27
+ init_kwargs = if new_args.any?
28
+ Hash[members.take(new_args.size).zip(new_args)]
29
+ else
30
+ new_kwargs
24
31
  end
32
+
25
33
  self.allocate.tap do |instance|
26
- args_to_hash = if !new_kwargs.any?
27
- Hash[members.take(new_args.size).zip(new_args)]
28
- else
29
- new_kwargs
30
- end
31
- instance.send(:initialize, **args_to_hash, &block)
32
- end
34
+ instance.send(:initialize, **init_kwargs, &block)
35
+ end.freeze
36
+ end
37
+ class << klass
38
+ alias_method :[], :new
33
39
  end
34
40
 
35
41
  args.map do |arg|
@@ -48,16 +54,12 @@ else
48
54
  self.class.members
49
55
  end
50
56
 
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
57
+ def initialize(**kwargs)
58
+ @attributes = Hash[members.map {|m| [m,kwargs[m]] }]
59
+ end
60
+
61
+ def deconstruct
62
+ @attributes.values
61
63
  end
62
64
 
63
65
  def deconstruct_keys(array)
@@ -89,11 +91,12 @@ else
89
91
  insect_key = key.to_s.start_with?("@") ? ":#{key}" : key
90
92
  "#{insect_key}=#{value}"
91
93
  end
92
- %(#<#{name} #{attribute_markers.join(" ")}>)
94
+ %(#<#{name} #{attribute_markers.join(", ")}>)
93
95
  end
96
+ alias_method :to_s, :inspect
94
97
 
95
98
  def with(**kwargs)
96
- self.class.new(*@attributes.merge(kwargs).values)
99
+ self.class.new(**@attributes.merge(kwargs))
97
100
  end
98
101
  end
99
102
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Polyfill
4
4
  module Data
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.2"
6
6
  end
7
7
  end
data/test/test_data.rb CHANGED
@@ -118,6 +118,11 @@ 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
+ test = klass.new(bar: 2, foo: 1)
122
+ assert_equal([1, 2], test.deconstruct)
123
+
124
+ assert_predicate(test, :frozen?)
125
+
121
126
  assert_kind_of(Integer, test.hash)
122
127
  end
123
128
 
@@ -133,6 +138,11 @@ class TestData < Minitest::Test
133
138
  klass = Data.define(:@a)
134
139
  o = klass.new(1)
135
140
  assert_equal("#<data :@a=1>", o.inspect)
141
+
142
+ klass = Data.define(:one, :two)
143
+ o = klass.new(1,2)
144
+ assert_equal("#<data one=1, two=2>", o.inspect)
145
+ assert_equal("#<data one=1, two=2>", o.to_s)
136
146
  end
137
147
 
138
148
  def test_equal
@@ -167,4 +177,19 @@ class TestData < Minitest::Test
167
177
  assert_equal([], test.members)
168
178
  assert_equal({}, test.to_h)
169
179
  end
180
+
181
+ def test_square_braces
182
+ klass = Data.define(:amount, :unit)
183
+
184
+ distance = klass[10, 'km']
185
+
186
+ assert_equal(10, distance.amount)
187
+ assert_equal('km', distance.unit)
188
+ end
189
+
190
+ def test_namespaced_constant
191
+ klass = Data.define("Measure", :amount, :unit)
192
+
193
+ assert_equal(Data::Measure, klass)
194
+ end
170
195
  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.2
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-21 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
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubygems_version: 3.2.27
79
+ rubygems_version: 3.1.6
79
80
  signing_key:
80
81
  specification_version: 4
81
82
  summary: Add the ruby 3.2 Data class to older rubies
metadata.gz.sig CHANGED
Binary file