Class: MaskableAttribute::MaskableAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/maskable_attribute/maskable_attribute.rb

Defined Under Namespace

Classes: InvalidMask

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options)
  @object = object
  @attribute = attribute
  @masks = Masks.new masks
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



3
4
5
# File 'lib/maskable_attribute/maskable_attribute.rb', line 3

def attribute
  @attribute
end

#masksObject

Returns the value of attribute masks.



3
4
5
# File 'lib/maskable_attribute/maskable_attribute.rb', line 3

def masks
  @masks
end

#objectObject

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

#demaskObject

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

#maskedObject 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_formatsObject



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

#unmaskedObject



38
39
40
# File 'lib/maskable_attribute/maskable_attribute.rb', line 38

def unmasked
  @object.read_attribute attribute
end