ruby_context 0.1.0 → 0.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 +4 -4
- data/README.md +44 -8
- data/lib/context/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa4cfd9c78dde837339350593fe18953bcfcc0972cf48935f0f9f2cc50caf167
|
4
|
+
data.tar.gz: 47bfd71c36db62e7ed4f6707422e3823fc196bcef2bd2e40a2bce81d91905686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f9754848dadb5e698c055170f7d10c8a8e5bacfbfa24896d6621607fb20244388de7340c643aecc9b71bb91abeb020bfbf1e61df59434c4ee7ab0569f60ab83
|
7
|
+
data.tar.gz: f11a80e627984406a0db3e82c7ba756245863a9929da7f2c394022bacbef6e2ee4a400a416da86778e6d50e723dc45152d5dec7a2de23347066aa70075ebc0e7
|
data/README.md
CHANGED
@@ -1,28 +1,64 @@
|
|
1
|
-
# Context
|
1
|
+
# Ruby Context
|
2
2
|
|
3
|
-
|
3
|
+
This gem allows you to define and override global values inspired in [Reactjs Context api](https://reactjs.org/docs/context.html)
|
4
4
|
|
5
|
-
|
5
|
+
You can use this gem in any ruby project, including Rails.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'context'
|
12
|
+
gem 'ruby-context'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
17
17
|
$ bundle
|
18
18
|
|
19
|
-
|
19
|
+
## Usage in Rails
|
20
20
|
|
21
|
-
|
21
|
+
imagine you want to override rails logger
|
22
22
|
|
23
|
-
|
23
|
+
Create a config/initializers/app_logger.rb
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
|
+
# Default value isn't necessary, but good
|
27
|
+
|
28
|
+
APP_LOGGER = Context::Context.new(default_value: Rails.logger)
|
29
|
+
```
|
30
|
+
|
31
|
+
Image you have a worker called send_mail_worker.rb and you want that every call to Rails.logger should be override to a different log.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class SendMailWorker
|
35
|
+
def do_work
|
36
|
+
email_logger = Logger.new
|
37
|
+
APP_LOGGER.with(email_logger) do
|
38
|
+
MailService.run!
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
So now every calls of APP_LOGGER will not call Rails.logger anymore, but email_logger!
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class MailService
|
48
|
+
def self.run!
|
49
|
+
APP_LOGGER.info "runing mail service"
|
50
|
+
OtherClass.execute!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class OtherClass
|
57
|
+
def self.execute!
|
58
|
+
APP_LOGGER.info "executing"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
26
62
|
|
27
63
|
## Development
|
28
64
|
|
data/lib/context/version.rb
CHANGED