lab42_data_class 0.3.3 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b6b4667c08da4511e2e4889bf35d21badc1018d7c8ce61efb66c65162c1ca0f
4
- data.tar.gz: 0206dff912a52398e24488fd53aaac9c65ceceb3706b38bb562aac37d6d9d296
3
+ metadata.gz: c6be60bd36bb2a9a538eb968067ff72eec653a4e9bc7dc3c8904558f803668e3
4
+ data.tar.gz: 4c1e75ab8907ce56ebcb71dd56d705afd9a1127e905ffe5344d846ddd0ade90a
5
5
  SHA512:
6
- metadata.gz: 23d410a7701bbdf4d175ab650e62fea5845c91d825d8346fa31955007571e2b862deccf478a831dab3e4404ef1d7f9f96018e13c672fe74a9592a534aceea186
7
- data.tar.gz: 1e349c67fa94a4633ad5e933e755a5ab762e3b13fa28cd5a7cdfdc18a70cfe7f5190dd9d2d0ac78474a44df2dcf4010785b155b8920706cd17da98125aa55ba7
6
+ metadata.gz: 33d4995e0048d390486d01bc3f07ad1be0e0dd5efbcf9693f656162e8691645be2e0b11e60df41defc771f9fc7108f1b35cef58aa4e0102b4912136094e06202
7
+ data.tar.gz: 02e403af2357e1b6b06e34afe9909777ab3f64a27d51843387ed20d1d349d96f7c9383fe22da386b42e7e5510180e6bc9a6a3d17c0b86cf16d1ff3f6cd02c8c3
data/README.md CHANGED
@@ -1,12 +1,14 @@
1
1
 
2
- [![Gem Version](http://img.shields.io/gem/v/lab42_data_class.svg)](https://rubygems.org/gems/lab42_data_class)
2
+ [![Issue Count](https://codeclimate.com/github/RobertDober/lab42_data_class/badges/issue_count.svg)](https://codeclimate.com/github/RobertDober/lab42_data_class)
3
3
  [![CI](https://github.com/robertdober/lab42_data_class/workflows/CI/badge.svg)](https://github.com/robertdober/lab42_data_class/actions)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/RobertDober/lab42_data_class/badge.svg?branch=main)](https://coveralls.io/github/RobertDober/lab42_data_class?branch=main)
5
+ [![Gem Version](http://img.shields.io/gem/v/lab42_data_class.svg)](https://rubygems.org/gems/lab42_data_class)
6
+ [![Gem Downloads](https://img.shields.io/gem/dt/lab42_data_class.svg)](https://rubygems.org/gems/lab42_data_class)
5
7
 
6
8
 
7
9
  # Lab42::DataClass
8
10
 
9
- An immutable dataclass
11
+ An immutable Dataclass, Tuples and Triples
10
12
 
11
13
  ## Usage
12
14
 
@@ -31,6 +33,8 @@ require 'lab42/data_class'
31
33
 
32
34
  Well let us [speculate about](https://github.com/RobertDober/speculate_about) it to find out:
33
35
 
36
+ ## Context `DataClass`
37
+
34
38
  ### Context: `DataClass` function
35
39
 
36
40
  Given
@@ -276,6 +280,62 @@ Then we can pass it as keyword arguments
276
280
  expect(extract_value(**my_class.new)).to eq([1, base: 2])
277
281
  ```
278
282
 
283
+ ## Context: `Pair` and `Triple`
284
+
285
+ Two special cases of a `DataClass` which behave like `Tuple` of size 2 and 3 in _Elixir_
286
+
287
+
288
+ They distinguish themselves from `DataClass` classes by accepting only positional arguments, and
289
+ cannot be converted to hashes.
290
+
291
+ These are actually two classes and not class factories as they have a fixed interface , but let us speculate about them to learn what they can do for us.
292
+
293
+ ### Context: Constructor functions
294
+
295
+ Given a pair
296
+ ```ruby
297
+ let(:token) { Pair("12", 12) }
298
+ let(:node) { Triple("42", 4, 2) }
299
+ ```
300
+
301
+ Then we can access their elements
302
+ ```ruby
303
+ expect(token.first).to eq("12")
304
+ expect(token.second).to eq(12)
305
+ expect(node.first).to eq("42")
306
+ expect(node.second).to eq(4)
307
+ expect(node.third).to eq(2)
308
+ ```
309
+
310
+ And we can treat them like _Indexable_
311
+ ```ruby
312
+ expect(token[1]).to eq(12)
313
+ expect(token[-2]).to eq("12")
314
+ expect(node[2]).to eq(2)
315
+ ```
316
+
317
+ And convert them to arrays of course
318
+ ```ruby
319
+ expect(token.to_a).to eq(["12", 12])
320
+ expect(node.to_a).to eq(["42", 4, 2])
321
+ ```
322
+
323
+ And they behave like arrays in pattern matching too
324
+ ```ruby
325
+ token => [str, int]
326
+ node => [root, lft, rgt]
327
+ expect(str).to eq("12")
328
+ expect(int).to eq(12)
329
+ expect(root).to eq("42")
330
+ expect(lft).to eq(4)
331
+ expect(rgt).to eq(2)
332
+ ```
333
+
334
+ And of course the factory functions are equivalent to the constructors
335
+ ```ruby
336
+ expect(token).to eq(Lab42::Pair.new("12", 12))
337
+ expect(node).to eq(Lab42::Triple.new("42", 4, 2))
338
+ ```
279
339
 
280
340
  # LICENSE
281
341
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Lab42
4
4
  module DataClass
5
- VERSION = "0.3.3"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: Apache-2.0
@@ -1,12 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './data_class/proxy'
4
+ require_relative './pair'
5
+ require_relative './triple'
4
6
 
5
7
  module Kernel
6
8
  def DataClass(*args, **kwds, &blk)
7
9
  proxy = Lab42::DataClass::Proxy.new(*args, **kwds, &blk)
8
10
  proxy.define_class!
9
11
  end
12
+
13
+ def Pair(first, second)
14
+ Lab42::Pair.new(first, second)
15
+ end
16
+
17
+ def Triple(first, second, third)
18
+ Lab42::Triple.new(first, second, third)
19
+ end
10
20
  end
11
21
 
12
22
  # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lab42
4
+ module EqAndPatterns
5
+ def [](idx)
6
+ to_a[idx]
7
+ end
8
+
9
+ def ==(other)
10
+ other.is_a?(self.class) &&
11
+ to_a == other.to_a
12
+ end
13
+
14
+ def deconstruct(*)
15
+ to_a
16
+ end
17
+ end
18
+ end
19
+ # SPDX-License-Identifier: Apache-2.0
data/lib/lab42/pair.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'eq_and_patterns'
4
+ module Lab42
5
+ class Pair
6
+ attr_reader :first, :second
7
+ include EqAndPatterns
8
+
9
+ def to_a
10
+ [first, second]
11
+ end
12
+
13
+ private
14
+
15
+ def initialize(first, second)
16
+ @first = first
17
+ @second = second
18
+ end
19
+ end
20
+ end
21
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'eq_and_patterns'
4
+ module Lab42
5
+ class Triple
6
+ attr_reader :first, :second, :third
7
+ include EqAndPatterns
8
+
9
+ def to_a
10
+ [first, second, third]
11
+ end
12
+
13
+ private
14
+
15
+ def initialize(first, second, third)
16
+ @first = first
17
+ @second = second
18
+ @third = third
19
+ end
20
+ end
21
+ end
22
+ # SPDX-License-Identifier: Apache-2.0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_data_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-31 00:00:00.000000000 Z
11
+ date: 2022-02-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  An Immutable DataClass for Ruby
@@ -26,6 +26,9 @@ files:
26
26
  - lib/lab42/data_class/proxy.rb
27
27
  - lib/lab42/data_class/proxy/mixin.rb
28
28
  - lib/lab42/data_class/version.rb
29
+ - lib/lab42/eq_and_patterns.rb
30
+ - lib/lab42/pair.rb
31
+ - lib/lab42/triple.rb
29
32
  homepage: https://github.com/robertdober/lab42_data_class
30
33
  licenses:
31
34
  - Apache-2.0