redacted_struct 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64c02de7881517f0bbb564741a2b999e8066a8de32f7aed539c86da981d5bddf
4
- data.tar.gz: ad4589962d6c3be459ffc88ef3712b3e59177b83cddef3b2526a0dfb11c2a2ad
3
+ metadata.gz: 4d69e9affe18e7d2f75a375a5cf36cbaed7045b638ea4a2b3fe9113d6e3828ab
4
+ data.tar.gz: 20bb14b68d38c7f2edf524abdc3a7021a0fc65d5129a32a2642b119872c65d19
5
5
  SHA512:
6
- metadata.gz: a4616807a9c76c8f01c63585d02c79a592b1eaed901263bfc1109ff29ceb165e3b860b6e9a5005d6fa772ab585fef04d3469b18a78466b91a10f56af438d0ee1
7
- data.tar.gz: ffe8ffa0f62360f78113cc98c4ee8972d1cb181682b3f10c87d63bc821ab24a25e62662b1e3485ab09f774f47a1e7b0681e04214037f4bbcfe936786f2c8c83a
6
+ metadata.gz: 89181954e77992a479f20429c686bcaa1cacc12ec5d97bbc8a82ee5fc4db2f75316e1b3aca78f4b0bf97a1bd1c6f0b53df473c42b8ca3930a89153545bea3550
7
+ data.tar.gz: aa27dc39f2548a175287dcee00f88f4ced18c99a7dbefc57c9d027309ed4c6535fe970f579a0b128c44d31487cb2724b0ee1a497d80a4080cc0e8a1f3dcbcfd3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.1.0] - 2021-03-24
4
+
5
+ - Override `Struct#pretty_print` to keep redacted fields redacted when
6
+ pretty-printing via `pp`
7
+
3
8
  ## [1.0.0] - 2021-03-24
4
9
 
5
10
  - Initial release
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:
@@ -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.1"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redacted_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Margolis