
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
maskable attributes provides a simple way to mask the output of a method. maskable attributes works really well with the decorator / presenter pattern. We are using maskable attributes to hide job posting information from users who are not signed in.
require 'delegate'
require 'maskable_attributes'
class Person < Struct.new(:name, :email, :phone)
end
class MaskedPerson < DelegateClass(Person)
include MaskableAttributes
masked_attributes :email, :phone
end
person = Person.new('Michael', '513-347-1111', 'foo@bar.com')
masked = MaskedPerson.new(person)
masked.name # => "Michael"
masked.phone # => "************"
masked.email # => "************"
You can provide a string, lambda, or a symbol (representing a predefined strategy) in order to further customize the masking.
If you provide a string, that string will always be used to mask the attributes.
masked_attributes :email, :phone, :with => "HIDDEN"
masked.phone # => "HIDDEN"
masked.email # => "HIDDEN"
If you provide a lambda it will be called with the original value passed to it. This allows the masking to be different based on the attribute value. You can see here we output the same number of stars as the length of the attribute
masked_attributes :email, :phone, :with => lambda { |v| "*" * v.size }
masked.phone # => "************"
masked.email # => "***********"
Currently masked_attributes only provides 1 predefined strategy named :stars, which provides the same functionality from the lambda example above.
masked_attributes :email, :phone, :with => :stars
masked.phone # => "************"
masked.email # => "***********"
You can easily add more strategies yourself.
MaskableAttributes.strategies[:dashes] = lambda { |v| "-" * v.size }
masked_attributes :email, :phone, :with => :dashes
masked.phone # => "------------"
masked.email # => "-----------"
The default masking strategy is a string with 12 *'s. You can override the default string masking if you choose to.
MaskableAttributes.default_masking = "HIDDEN"
Copyright (c) 2010 Michael Guterl. See LICENSE for details.
FAQs
Unknown package
We found that maskable_attributes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.