use_context 1.0.0 → 1.1.1

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: 90aac299a67f5dbfbb1e2580c29021dafdd1ba06e0c4e6771a15faa6a673a3dc
4
+ data.tar.gz: 4241b82b90fb4c902bae71dc2609f4651dc2eaf3ccd3cea1bddc54d08fdc1916
5
5
  SHA512:
6
- metadata.gz: c05082dfc9a79b6f92504d2f1a5928abae3b3fb7d2c38fba6c8c617031d80e0afe5d93dd21c4fe2d07e8fef00420c68fc6e3da824813e762f6f8cae439ce1ad1
7
- data.tar.gz: fe5474e402f9bf88b3d1f9738c46b2cb07884adee0cfd9314c5a6273a959a93169375b7097a068b867f30bf421e86af447393ec43756a6b2a0e3cab583616358
6
+ metadata.gz: 7527e2b3c96f0470f4997bd366d3b8071f4a17a07b00d101e2fa6b868fa435a54e991fdd6997594374a989dd16b9dadd8deea1c1806da09749dd16307fd6b053
7
+ data.tar.gz: 9d23e93d4ff8c0c85c7d618fd2d38f2d46617fd048ba2ec39069fb3f0d529c1caba5f8cef50dd7d80eacc3087385096fc548f771d2f6827fb04a062f1042892c
data/CHANGELOG.md CHANGED
@@ -1,2 +1,14 @@
1
+ # 1.1.1
2
+ * Fix bug in context extraction that would yield an array instead of a value when only a single key was specified.
3
+
4
+ # 1.1.0
5
+ * `use_context` now treats all argments after the context name as context keys to extract and yield to the block:
6
+ ```ruby
7
+ use_context(:foo, :bar, :baz) do |bar, baz|
8
+ # ...
9
+ end
10
+ ```
11
+ * 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.
12
+
1
13
  # 1.0.0
2
14
  * 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.1"
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,21 @@ class KernelTest < Minitest::Test
100
100
  end
101
101
  end
102
102
  end
103
+
104
+ def test_use_context_can_extract_given_key
105
+ provide_context(:test, { setting1: :a, setting2: :b }) do
106
+ use_context(:test, :setting1) do |setting1|
107
+ assert_equal :a, setting1
108
+ end
109
+ end
110
+ end
111
+
112
+ def test_use_context_can_extract_given_keys
113
+ provide_context(:test, { setting1: :a, setting2: :b }) do
114
+ use_context(:test, :setting1, :setting2) do |setting1, setting2|
115
+ assert_equal :a, setting1
116
+ assert_equal :b, setting2
117
+ end
118
+ end
119
+ end
103
120
  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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro