Module: MaskableAttribute::ActsAsMaskableAttribute::ClassMethods
- Defined in:
- lib/maskable_attribute/acts_as_maskable_attribute.rb
Instance Method Summary collapse
-
#maskable_attribute(attribute_to_mask, masks, options = {}) ⇒ Object
Specifies an attribute to be masked, followed by masks to be made available to the attribute.
Instance Method Details
#maskable_attribute(attribute_to_mask, masks, options = {}) ⇒ Object
Specifies an attribute to be masked, followed by masks to be made available to the attribute.
Examples
class Foo < ActiveRecord::Base
maskable_attrribute :some_attribute,
[ :some_method_be_used_as_a_mask, :another_attribute_mask ]
end
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/maskable_attribute/acts_as_maskable_attribute.rb', line 19 def maskable_attribute(attribute_to_mask, masks, = {}) raise ArgumentError, "invalid argument (expected attribute)" unless column_names.include? attribute_to_mask.to_s cattr_accessor :masks self.masks ||= {} self.masks[attribute_to_mask] = masks self.masks define_method attribute_to_mask do send("maskable_#{attribute_to_mask}").to_s #masked_attribute attribute_to_mask, options end define_method "#{attribute_to_mask}=" do |value| #write_attribute attribute_to_mask, masked_attribute(attribute_to_mask, options).set(value) send :write_attribute, attribute_to_mask, value end define_method "maskable_#{attribute_to_mask}" do masked_attribute attribute_to_mask, end end |