use_context 1.0.0 → 1.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: fefdb2f5210efd1e2cc56fe41dc465cb61aa711fc658f29d606221335dfcaa51
4
- data.tar.gz: 4e429a7afe8d22962dc58109379fbf85cc8135d1713ed1d30b25ce7f16fa1222
3
+ metadata.gz: f4c30166e7e8f3c7a661205455d9095e632f31a349ed72213b80876730c89ab9
4
+ data.tar.gz: 7e38977774ad56f2ab3465088fb800137553b7d97037b126cb548fc64218fb1e
5
5
  SHA512:
6
- metadata.gz: c05082dfc9a79b6f92504d2f1a5928abae3b3fb7d2c38fba6c8c617031d80e0afe5d93dd21c4fe2d07e8fef00420c68fc6e3da824813e762f6f8cae439ce1ad1
7
- data.tar.gz: fe5474e402f9bf88b3d1f9738c46b2cb07884adee0cfd9314c5a6273a959a93169375b7097a068b867f30bf421e86af447393ec43756a6b2a0e3cab583616358
6
+ metadata.gz: 01ec5bb17b6bf570b9114a482180b8b3946f902fae756670d457803fd2b9b067d1cf1999138c97d214e0a46fbdeadc170b0a7acea140f9f2209d84bc6b36fadf
7
+ data.tar.gz: 216cfa9b71450937b76c00513f08901cf22c6b5744fde6df9978592fbc530328aa7c0cbeaf92230b03a53a21408ef5a20ae455d16193d5f9c69d1063bbaab94b
data/CHANGELOG.md CHANGED
@@ -1,2 +1,11 @@
1
+ # 1.1.0
2
+ * `use_context` now treats all argments after the context name as context keys to extract and yield to the block:
3
+ ```ruby
4
+ use_context(:foo, :bar, :baz) do |bar, baz|
5
+ # ...
6
+ end
7
+ ```
8
+ * Documentation updated to describe the new behavior of `use_context` as well as how to use the library as a mixin on a per-class or per-module basis.
9
+
1
10
  # 1.0.0
2
11
  * Birthday!
data/README.md CHANGED
@@ -10,7 +10,7 @@ use_context was inspired by this use-case, and originally proposed as an additio
10
10
 
11
11
  ## Usage
12
12
 
13
- There are three ways to use this gem. As some folks are understandably uncomfortable using monkeypatches - especially ones applied to core classes and modules - you are free to choose the one that fits your preferences.
13
+ There are four ways to use this gem. As some folks are understandably uncomfortable using monkeypatches - especially ones applied to core classes and modules - you are free to choose the one that fits your preferences.
14
14
 
15
15
  ### As a monkeypatch
16
16
 
@@ -55,6 +55,28 @@ provide_context(:welcome, { salutation: "Hello, world!" }) do
55
55
  end
56
56
  ```
57
57
 
58
+ ### As a mixin
59
+
60
+ If you'd rather opt-in to use_context's methods on a per-class or per-module basis, you can include the `ContextMethods` mixin:
61
+
62
+ ```ruby
63
+ require "use_context"
64
+
65
+ class MyClass
66
+ include UseContext::ContextMethods
67
+
68
+ def speak
69
+ use_context(:welcome) do |context|
70
+ puts context[:salutation]
71
+ end
72
+ end
73
+ end
74
+
75
+ UseContext.provide_context(:welcome, { salutation: "Hello, world!" }) do
76
+ MyClass.new.speak # prints "Hello, world!"
77
+ end
78
+ ```
79
+
58
80
  ### No magic
59
81
 
60
82
  If you'd rather avoid modifying `Kernel` altogether, use_context can also be used via the `UseContext` constant.
@@ -89,6 +111,18 @@ provide_context(:welcome, { salutation: "Hello, world!" }) do
89
111
  end
90
112
  ```
91
113
 
114
+ ## Extracting individual keys
115
+
116
+ The `use_context` method allows extracting individual keys from the current context. All arguments after the context name are treated as keys to extract. The extracted values are passed to the block in the order they are specified:
117
+
118
+ ```ruby
119
+ provide_context(:welcome, { salutation: "Hello", recipient: "world" }) do
120
+ use_context(:welcome, :salutation, :recipient) do |salutation, recipient|
121
+ puts "#{salutation}, #{recipient}!" # prints "Hello, world!"
122
+ end
123
+ end
124
+ ```
125
+
92
126
  ## Running Tests
93
127
 
94
128
  `bundle exec rake` should do the trick.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UseContext
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/use_context.rb CHANGED
@@ -21,6 +21,14 @@ module UseContext
21
21
  def [](_key)
22
22
  nil
23
23
  end
24
+
25
+ def empty?
26
+ true
27
+ end
28
+
29
+ def present?
30
+ false
31
+ end
24
32
  end
25
33
 
26
34
  class Context
@@ -37,6 +45,14 @@ module UseContext
37
45
 
38
46
  nil
39
47
  end
48
+
49
+ def empty?
50
+ false
51
+ end
52
+
53
+ def present?
54
+ true
55
+ end
40
56
  end
41
57
 
42
58
  class ContextStack
@@ -65,8 +81,14 @@ module UseContext
65
81
  context.pop if context
66
82
  end
67
83
 
68
- def use_context(name)
69
- yield UseContext.context[name]&.context || EmptyContext.instance
84
+ def use_context(name, *keys)
85
+ context = UseContext.context[name]&.context || EmptyContext.instance
86
+
87
+ if keys.empty?
88
+ yield context
89
+ else
90
+ yield keys.map { |key| context[key] }
91
+ end
70
92
  end
71
93
  end
72
94
 
data/test/kernel_test.rb CHANGED
@@ -100,4 +100,13 @@ class KernelTest < Minitest::Test
100
100
  end
101
101
  end
102
102
  end
103
+
104
+ def test_use_context_can_extract_given_keys
105
+ provide_context(:test, { setting1: :a, setting2: :b }) do
106
+ use_context(:test, :setting1, :setting2) do |setting1, setting2|
107
+ assert_equal :a, setting1
108
+ assert_equal :b, setting2
109
+ end
110
+ end
111
+ end
103
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: use_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro