Class: MaskableAttribute::MaskableAttribute
- Inherits:
-
Object
- Object
- MaskableAttribute::MaskableAttribute
- Defined in:
- lib/maskable_attribute/maskable_attribute.rb
Defined Under Namespace
Classes: InvalidMask
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#masks ⇒ Object
Returns the value of attribute masks.
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
-
#demask ⇒ Object
update an attribute to replace all masks in place i.e.
-
#initialize(object, attribute, masks, options) ⇒ MaskableAttribute
constructor
A new instance of MaskableAttribute.
- #masked ⇒ Object (also: #to_s)
- #masks_with_formats ⇒ Object
- #set(value) ⇒ Object
- #unmasked ⇒ Object
Constructor Details
#initialize(object, attribute, masks, options) ⇒ MaskableAttribute
Returns a new instance of MaskableAttribute.
5 6 7 8 9 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 5 def initialize(object, attribute, masks, ) @object = object @attribute = attribute @masks = Masks.new masks end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
3 4 5 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 3 def attribute @attribute end |
#masks ⇒ Object
Returns the value of attribute masks.
3 4 5 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 3 def masks @masks end |
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 3 def object @object end |
Instance Method Details
#demask ⇒ Object
update an attribute to replace all masks in place i.e. “somethingsome_maskcool” will become “somethingelsecool”
32 33 34 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 32 def demask @object.send :write_attribute, attribute, masked end |
#masked ⇒ Object Also known as: to_s
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 19 def masked value = unmasked if !value.blank? and value.match(/\{.*\}/) value.scan(/(?<={)[^}]+(?=})/).each do |mask| #mask: two_digit model_series mask_value = @masks[mask].unmask(@object) value = value.sub "{#{mask}}", mask_value.to_s unless mask_value.nil? end end value end |
#masks_with_formats ⇒ Object
15 16 17 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 15 def masks_with_formats @masks.formatted_names end |
#set(value) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 42 def set(value) unless value.blank? @masks.each do |mask| mask.accessed_by.each do |mask_accessor| value.sub! /#{"(?<!#{@protected_prefixes})" unless @protected_prefixes.blank?}#{mask.unmask(@object, :formatted => mask_accessor)}(?![^{]*})/, "{#{mask_accessor}}" unless mask.unmask(@object).blank? end end end value end |
#unmasked ⇒ Object
38 39 40 |
# File 'lib/maskable_attribute/maskable_attribute.rb', line 38 def unmasked @object.read_attribute attribute end |