redacted_struct 1.0.1 → 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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +17 -1
- data/lib/redacted_struct.rb +23 -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: 4d69e9affe18e7d2f75a375a5cf36cbaed7045b638ea4a2b3fe9113d6e3828ab
|
4
|
+
data.tar.gz: 20bb14b68d38c7f2edf524abdc3a7021a0fc65d5129a32a2642b119872c65d19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89181954e77992a479f20429c686bcaa1cacc12ec5d97bbc8a82ee5fc4db2f75316e1b3aca78f4b0bf97a1bd1c6f0b53df473c42b8ca3930a89153545bea3550
|
7
|
+
data.tar.gz: aa27dc39f2548a175287dcee00f88f4ced18c99a7dbefc57c9d027309ed4c6535fe970f579a0b128c44d31487cb2724b0ee1a497d80a4080cc0e8a1f3dcbcfd3
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -8,14 +8,30 @@ Config = RedactedStruct.new(
|
|
8
8
|
:username,
|
9
9
|
:password,
|
10
10
|
:timeout,
|
11
|
+
:url,
|
11
12
|
keyword_init: true,
|
12
13
|
allowed_members: [:username, :timeout]
|
13
14
|
)
|
14
15
|
|
15
|
-
Config.new(username: 'example', password: 'secret', timeout: 5)
|
16
|
+
config = Config.new(username: 'example', password: 'secret', timeout: 5, url: 'https://example.com')
|
16
17
|
=> #<struct Config username="example" password=[REDACTED] timeout=5>
|
17
18
|
```
|
18
19
|
|
20
|
+
## Pretty-Printing
|
21
|
+
|
22
|
+
The struct will automatically redact itself when pretty-printed via `#pp`:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'pp'
|
26
|
+
|
27
|
+
pp config
|
28
|
+
#<struct Config
|
29
|
+
username="example",
|
30
|
+
password=[REDACTED],
|
31
|
+
timeout=5,
|
32
|
+
url="https://example.com">
|
33
|
+
```
|
34
|
+
|
19
35
|
## Installation
|
20
36
|
|
21
37
|
Add this line to your application's Gemfile:
|
data/lib/redacted_struct.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# A subclass of Struct that redacts members by default, and can allow some to be printed
|
4
4
|
class RedactedStruct < Struct
|
5
|
-
VERSION = "1.0
|
5
|
+
VERSION = "1.1.0"
|
6
6
|
|
7
7
|
def self.new(*name_and_members, keyword_init: nil, allowed_members: [], &block)
|
8
8
|
super(*name_and_members, keyword_init: keyword_init, &block).tap do |struct_class|
|
@@ -35,4 +35,26 @@ class RedactedStruct < Struct
|
|
35
35
|
end
|
36
36
|
|
37
37
|
alias_method :to_s, :inspect
|
38
|
+
|
39
|
+
# Overrides for pp
|
40
|
+
# See https://github.com/ruby/pp/blob/3d925b5688b8226f653127d990a8dce48bced5fe/lib/pp.rb#L379-L391
|
41
|
+
# rubocop:disable all
|
42
|
+
def pretty_print(q)
|
43
|
+
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
44
|
+
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
45
|
+
q.breakable
|
46
|
+
q.text member.to_s
|
47
|
+
q.text '='
|
48
|
+
q.group(1) {
|
49
|
+
q.breakable ''
|
50
|
+
if allowed_members.include?(member)
|
51
|
+
q.pp self[member]
|
52
|
+
else
|
53
|
+
q.text "[REDACTED]"
|
54
|
+
end
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
end
|
59
|
+
# rubocop:enable all
|
38
60
|
end
|