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 <, :email, :phone)

class MaskedPerson < DelegateClass(Person)

  include MaskableAttributes

  masked_attributes :email, :phone


person ='Michael', '513-347-1111', '[email protected]')
masked =  # => "Michael" # => "************" # => "************"

Custom strategies

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" # => "HIDDEN" # => "HIDDEN"

lambda / Proc

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 } # => "************" # => "***********"


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 # => "************" # => "***********"

You can easily add more strategies yourself.

MaskableAttributes.strategies[:dashes] = lambda { |v| "-" * v.size }

masked_attributes :email, :phone, :with => :dashes # => "------------" # => "-----------"

Default string masking

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"

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


Copyright (c) 2010 Michael Guterl. See LICENSE for details.