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 +4 -4
- data/.ruby-version +1 -0
- data/README.md +24 -9
- data/lib/taking.rb +15 -1
- data/lib/taking/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3388945aa1a516c64e2dfe0cc1ad493959833678db0e49ae4fef6f1a35671cd
|
4
|
+
data.tar.gz: a65489f822a87f13e9df72bce1f37ee073dfdb6de2005632de5b2ec6dc8614e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
14
|
-
|
15
|
-
in
|
16
|
-
|
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
|
-
|
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)
|
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
|
data/lib/taking/version.rb
CHANGED
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
|
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
|