Sha256: 14aa611e21cc9d09a51f5bff0a6b435b13b1158243649f4ce5be95ea3df67b9c

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module MaskableAttribute
  class MaskableAttribute
    attr_accessor :object, :attribute, :masks

    def initialize(object, attribute, masks, options)
      @object = object
      @attribute = attribute
      @masks = Masks.new masks
    end

    def masks
      @masks.names
    end

    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

    # update an attribute to replace all masks in place
    # i.e. "something{some_mask}cool" will become "somethingelsecool"
    def demask
      @object.send :write_attribute, attribute, masked
    end

    alias :to_s :masked

    def unmasked
      @object.read_attribute attribute
    end

    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

    class InvalidMask < RuntimeError
      attr :mask, :obj
      def initialize(mask, obj)
        @mask = mask
        @obj = obj
      end

      def to_s
        "Invalid mask '#{@mask}' for #{@obj.class.name}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maskable_attribute-0.0.7 lib/maskable_attribute/maskable_attribute.rb