taking 0.0.1 → 0.1.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: 67f7a8206a4021e66c7358aa6e66b15062c2d4f9328ee631ed905cb3553c2c19
4
- data.tar.gz: 9d16465bfb301e899ba41db495976fcb52f48ac32c9fb78e6c81d58840151dd5
3
+ metadata.gz: d3388945aa1a516c64e2dfe0cc1ad493959833678db0e49ae4fef6f1a35671cd
4
+ data.tar.gz: a65489f822a87f13e9df72bce1f37ee073dfdb6de2005632de5b2ec6dc8614e6
5
5
  SHA512:
6
- metadata.gz: bcc09db54032089779e946c0792f45b2e45eab17766d1946b51acac746821c2dc0ca8bc047cac62a86762eb1b3491fac8a93b440c1fa0aeee7382580ac2127b8
7
- data.tar.gz: ac6a108997bdb05af752420ec0bcef8b80f7d86aad38f0fd05318bcb8b377db78466ad47ad3dfe0684fbcf83f86c201899abcad038983c7dcc3b877528be033b
6
+ metadata.gz: c61b734dbd9be2515f8cf629beb2751d6b6c69a8609c4e033ab7b619266523edfba28ba6506ede7ee66debde0d21714a82fd9579844f0aa67560c3ca3c6d6a34
7
+ data.tar.gz: f068d1a2d3a3d92a9bc9435e6b1ac8c3bb44f28cc82a9ea3f5401da8ed4144a7d146607058d0eb64475ec8217c62172a79a6f1ffcc87386fb9400bb1c7424f47
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
data/README.md CHANGED
@@ -9,13 +9,27 @@ This is a very rudimentary implementation for testing, and may be expanded upon
9
9
  Elixir allows us to pattern match on methods, this allows us to do it using Ruby's one-liner methods and argument forwarding (`...`):
10
10
 
11
11
  ```ruby
12
+ Point = Struct.new(:x, :y)
13
+
12
14
  def handle_responses(...) = case Taking.from(...)
13
- in 1, 2, 3 then :numbers
14
- in 'a', 'b' then :strings
15
- in :a, :b then :symbols
16
- else false
15
+ in Point[x, 10 => y]
16
+ Point[x, y + 1]
17
+ in 1, 2, 3
18
+ :numbers
19
+ in 'a', 'b'
20
+ :strings
21
+ in :a, :b
22
+ :symbols
23
+ in x: 0, y: 0
24
+ :origin
25
+ in x: 0, y: (10..)
26
+ :north
27
+ else
28
+ false
17
29
  end
18
30
 
31
+ # Array-like
32
+
19
33
  handle_responses(1,2,3)
20
34
  # => :numbers
21
35
  handle_responses('a', 'b')
@@ -25,11 +39,7 @@ handle_responses(:a, :b)
25
39
  handle_responses(:nope?)
26
40
  # => false
27
41
 
28
- def handle_kw_responses(...) = case Taking.from_kw(...)
29
- in x: 0, y: 0 then :origin
30
- in x: 0, y: 10.. then :north
31
- else false
32
- end
42
+ # Hash-like
33
43
 
34
44
  handle_responses(x: 0, y: 0)
35
45
  # => :origin
@@ -37,6 +47,11 @@ handle_responses(x: 0, y: 15)
37
47
  # => :north
38
48
  handle_responses(x: 10, y: 15)
39
49
  # => :false
50
+
51
+ # Deconstructable Object
52
+
53
+ handle_responses(Point[1, 10])
54
+ # => Point[1, 11]
40
55
  ```
41
56
 
42
57
  ## Installation
data/lib/taking.rb CHANGED
@@ -1,12 +1,22 @@
1
1
  require "taking/version"
2
2
 
3
3
  module Taking
4
+ MATCH_METHODS = %i(deconstruct deconstruct_keys)
5
+
4
6
  # Unfolds Array arguments
5
7
  #
6
8
  # @param *args [Array[Any]]
7
9
  #
8
10
  # @return [Array[Any]]
9
- def self.from(*args) = args
11
+ def self.from(lead = nil, *args, **kwargs)
12
+ if args.empty? && kwargs.empty?
13
+ return can_deconstruct?(lead) ? lead : nil
14
+ end
15
+
16
+ kwargs.empty? ? [lead, *args] : kwargs
17
+ end
18
+
19
+ def self.from_list(*args) = args
10
20
 
11
21
  # Unfolds Keyword arguments
12
22
  #
@@ -14,4 +24,8 @@ module Taking
14
24
  #
15
25
  # @return [Hash[Any, Any]]
16
26
  def self.from_kw(**args) = args
27
+
28
+ def self.can_deconstruct?(o)
29
+ MATCH_METHODS.any? { o.respond_to?(_1) }
30
+ end
17
31
  end
@@ -1,3 +1,3 @@
1
1
  module Taking
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Weaver
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".ruby-version"
64
65
  - CODE_OF_CONDUCT.md
65
66
  - Gemfile
66
67
  - Gemfile.lock