Skip to content

Commit

Permalink
Update README.md with another example
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Aug 13, 2022
1 parent d39dc3d commit 7403ed8
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,35 @@ immutable_struct_ex.john?

### Other Examples
```ruby
user = { username: 'username', password: 'password', ssn: '123-70-9182' }
ImmutableStructEx.new(**user) do
REDACT = %i(username password).freeze
# Redactable, immutable struct
user = { username: 'jdoe', password: 'p@55w0rD', ssn: '123-70-9182' }
immutable_struct_ex = ImmutableStructEx.new(**user) do
REDACT = %i(password ssn).freeze

def inspect
to_s
end

def to_s
super.to_s.tap do |string|
REDACT.each do |redact|
string.gsub!(/( #{Regexp.quote(redact.to_s)}=")(.*?)(")/, '\1[REDACTED]\3')
end
end
end

def to_h
super.to_h.tap do |h|
REDACT.each { |redact| h[redact] = :redacted }
super.to_h.tap do |hash|
REDACT.each { |redact| hash[redact] = '[REDACTED]' }
end
end
end.to_h
#=> {:username=>:redacted, :password=>:redacted, :ssn=>"123-70-9182"}
end

immutable_struct_ex.inspect
#=> "#<struct username=\"jdoe\", password=\"[REDACTED]\", ssn=\"[REDACTED]\">"

immutable_struct_ex.to_h
#=> {:username=>"jdoe", :password=>"[REDACTED]", :ssn=>"[REDACTED]"}
```

## Installation
Expand Down

0 comments on commit 7403ed8

Please sign in to comment.