immutable_struct_ex_redactable 1.1.0 → 1.2.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: a42c25462c52746ec41cd4342439fffbb683fec77c05bb5a4206e7d7b2e5476f
4
- data.tar.gz: e37bae62f51c0e8c0d8cc3acc624714f06ddbe94783509d6c0e37d84b03ca05f
3
+ metadata.gz: 321fc8418eb84c3fa0c62cb687e6811c2584c7f6a30575244b0b0468185bfbf3
4
+ data.tar.gz: dfeae468c5bdc53aaf6872e247c92bac995a736434ee0eff32acfb1a8970ad20
5
5
  SHA512:
6
- metadata.gz: c3668bb96227c69b6babd198c2ee832e87e41f94a7f9c1e30f51a4606a7fbb22e1f804208f741dd5a72f7f6c7ed7291b29dea863ebfe8338ef579cb9e6f76033
7
- data.tar.gz: e0b12b781c99c6e453c4c27653fa4992181d8bfe352b14e5f4653098d2553b80910b298f47b4b04fd780995a2289861fce7b15fe9a863b027858a2693c2cd101
6
+ metadata.gz: 38f3992c16fd65c313a95988ac4dd76cba8c8765d9cba60b91fa97f94845f988c83c081ee8d4fa091946b3731c34935e91e2a1ccb7542dff1fe2319a47d867ac
7
+ data.tar.gz: 81a4e7d9caad199dcf5a207321ae1ba3f8122cb295cf3f396ca5c113e7ceb08565f327aea2ccbc57d858d60db5d53c39b2cdc718a5399f6d227fc913d9d9409b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [1.2.0] - 2022-10-03
2
+ * Changes
3
+ * Add `ImmutableStructExRedactable::Configuration#redacted_unsafe` property. If this property is `true`, redacted field values will be retained and accessible as private methods on the struct named `unredacted_<field>`.
4
+ * Update README.md file.
5
+
1
6
  ## [1.1.0] - 2022-10-02
2
7
  * Changes
3
8
  * Fix rubocop violations
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- immutable_struct_ex_redactable (1.1.0)
4
+ immutable_struct_ex_redactable (1.2.0)
5
5
  immutable_struct_ex (~> 0.3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -17,14 +17,13 @@
17
17
 
18
18
  `immutable_struct_ex_redactable` maintains all the functionality of the *immutable_struct_ex* gem, but allows you to create immutable structs that can be configured to redact field values using standard gem configuration (`ImmutableStructExRedactable.configure { |config| ... }`) or by passing configuration options to the appropriate method (`ImmutableStructExRedactable.create_with(config, ...)`)
19
19
 
20
+ ### Future Enhancements
21
+ - Future functionality will probably accept regex pattern for redacting field values (e.g. 'gen***@***.com').
22
+
20
23
  ## Usage
21
24
 
22
25
  Follow the instructions for [Installation](#installation) first, to incorporate this gem into your code.
23
26
 
24
- ### Enhancements
25
- - Future functionality may include regex pattern for redacting field values (e.g. 'gen***@***.com').
26
- - Availability of redacted values as private fields/methods for use in `&blocks`.
27
-
28
27
  ### Basic usage
29
28
 
30
29
  #### Configure Gem Global Defaults
@@ -58,9 +57,10 @@ ImmutableStructExRedactable.create(**fields)
58
57
  => #<struct first="Jane", last="Smith", password="[REDACTED]", dob="[REDACTED]">
59
58
 
60
59
  ```
61
- NOTE: Setting the global defaults in the above manner will affect **every** *immutable_struct_ex_redactable* struct you create unless you override the global configuration options by passing a custom configuration.
62
60
 
63
- #### Overriding the Global Configuration Operions
61
+ NOTE: Setting the global defaults in the above manner will affect **every** *immutable_struct_ex_redactable* struct instance you create unless you override the global configuration options, by passing a custom configuration.
62
+
63
+ #### Overriding the Global Configuration Options
64
64
 
65
65
  To override the global configuration options, you may do so by calling the `ImmutableStructExRedactable#create_with` method in the following manner:
66
66
 
@@ -83,6 +83,45 @@ ImmutableStructExRedactable.create_with(custom_config, **fields)
83
83
  => #<struct first="John", last="Smith", password="[NO WAY JOSE]", dob="[NO WAY JOSE]">
84
84
  ```
85
85
 
86
+ ### Access to the Original Redacted Field Values
87
+
88
+ By default, *immutable_struct_ex_redactable* **will not** allow access to redacted field values; that is, field values marked for redaction via the global configuration (`ImmutableStructExRedactable::Configuration#redacted`) or by overriding the global configuration by passing a custom configuration (`ImmutableStructExRedactable.create_with(my_config, ...)`). However, if you really *need* access to redacted field values in their original, *un*redacted form, you can turn on the `ImmutableStructExRedactable::Configuration#redacted_unsafe` option in the global configuration or turn this same option on when passing a custom configuration. Turning the `redacted_unsafe` configuration option on in either scenario will instruct *immutable_struct_ex_redactable* to create *private methods* on structs created that will allow access to the original *un*redacted field values via `send:`. The *private methods* created that will allow access to the original *un*redacted field values, will have the following naming convention:
89
+
90
+ ```ruby
91
+ unredacted_<redacted field>
92
+ # Where <redacted field> == the Symbol of the redacted field.
93
+ # For example: unredacted_password, unredacted_dob, unredacted_ssn, etc.
94
+ ```
95
+
96
+ For example:
97
+
98
+ ```ruby
99
+ custom_config = ImmutableStructExRedactable::Configuration.new.tap do |config|
100
+ config.redacted_unsafe = true
101
+ end
102
+
103
+ hash = { username: 'jsmith', password: 'p@ssw0rd' }
104
+ struct = ImmutableStructExRedactable.create_with(custom_config, **hash) do
105
+ def to_h_unredacted
106
+ {
107
+ username: username,
108
+ password: send(:unredacted_password)
109
+ }
110
+ end
111
+ end
112
+ => #<struct username="jsmith", password="******">
113
+
114
+ struct.unredacted_password
115
+ #=> NoMethodError: private method `unredacted_password' called for #<struct username="jsmith", password="******">...
116
+
117
+ struct.send :unredacted_password
118
+ #=> "p@ssw0rd"
119
+
120
+ struct.to_h_unredacted
121
+ #=> {:username=>"jsmith", :password=>"p@ssw0rd"}
122
+
123
+ ```
124
+
86
125
  ### &blocks are Permitted
87
126
 
88
127
  Current `immutable_struct_ex` gem functionality is still available, so passing `&block` is permitted. See [immutable_struct_ex](https://rubygems.org/gems/immutable_struct_ex) gem for more details:
@@ -38,17 +38,33 @@ module ImmutableStructExRedactable
38
38
  # @return [String] the label that should replace redacted field values.
39
39
  attr_accessor :redacted_label
40
40
 
41
+ # Gets/sets the redacted unsafe switch that determines whether or not
42
+ # redacted field values are retained as private methods named
43
+ # #unredacted_<field> on the struct returned. If this configuration
44
+ # property is true, redacted field values will be retained and
45
+ # accessible as private methods on the struct.
46
+ #
47
+ # The default is false.
48
+ #
49
+ # @return [Bool] the unsafe switch value.
50
+ attr_accessor :redacted_unsafe
51
+
41
52
  # The constructor; calls {#reset}.
42
53
  def initialize
43
54
  reset
44
55
  end
45
56
 
57
+ def redacted_unsafe?
58
+ @redacted_unsafe
59
+ end
60
+
46
61
  # Resets the configuration settings to their default values.
47
62
  #
48
63
  # @return [void]
49
64
  def reset
50
65
  @redacted = %i[password]
51
66
  @redacted_label = '******'
67
+ @redacted_unsafe = false
52
68
  end
53
69
  end
54
70
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImmutableStructExRedactable
4
+ module RedactedAccessible
5
+ class << self
6
+ def included(base)
7
+ base.extend ClassModules
8
+ end
9
+ end
10
+
11
+ module ClassModules
12
+ def redacted_accessible_module_for(hash:, config:)
13
+ Module.new do
14
+ config.redacted.each do |attr|
15
+ unredacted_attr_method = "unredacted_#{attr}"
16
+ code = <<~CODE
17
+ def #{unredacted_attr_method}
18
+ "#{hash[attr]}"
19
+ end
20
+ private :#{unredacted_attr_method}
21
+ CODE
22
+ class_eval code
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImmutableStructExRedactable
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -2,9 +2,12 @@
2
2
 
3
3
  require 'immutable_struct_ex'
4
4
  require_relative 'immutable_struct_ex_redactable/configuration'
5
+ require_relative 'immutable_struct_ex_redactable/redacted_accessible'
5
6
  require_relative 'immutable_struct_ex_redactable/version'
6
7
 
7
8
  module ImmutableStructExRedactable
9
+ include RedactedAccessible
10
+
8
11
  module_function
9
12
 
10
13
  def create(**hash, &block)
@@ -13,12 +16,19 @@ module ImmutableStructExRedactable
13
16
  end
14
17
 
15
18
  def create_with(config, **hash, &block)
19
+ if config.redacted_unsafe?
20
+ redacted_private_accessible_module =
21
+ redacted_accessible_module_for(hash: hash, config: config)
22
+ end
23
+
16
24
  config.redacted.each do |attr|
17
25
  next unless hash.key? attr
18
26
 
19
27
  hash[attr] = config.redacted_label
20
28
  end
21
29
 
22
- ImmutableStructEx.new(**hash, &block)
30
+ ImmutableStructEx.new(**hash, &block).tap do |struct|
31
+ struct.extend(redacted_private_accessible_module) if config.redacted_unsafe?
32
+ end
23
33
  end
24
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immutable_struct_ex_redactable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gene M. Angelo, Jr.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-02 00:00:00.000000000 Z
11
+ date: 2022-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: immutable_struct_ex
@@ -47,6 +47,7 @@ files:
47
47
  - immutable_struct_ex_redactable.gemspec
48
48
  - lib/immutable_struct_ex_redactable.rb
49
49
  - lib/immutable_struct_ex_redactable/configuration.rb
50
+ - lib/immutable_struct_ex_redactable/redacted_accessible.rb
50
51
  - lib/immutable_struct_ex_redactable/version.rb
51
52
  - sig/immutable_struct_ex_redactable.rbs
52
53
  homepage: https://github.com/gangelo/immutable_struct_ex_redactable