Class: MaskableAttribute::Mask

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Mask

Returns a new instance of Mask.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/maskable_attribute/mask.rb', line 60

def initialize(options=nil)
  if options.is_a? Symbol
    self.name = (@method = options.to_sym)
    options = {}
  elsif options.is_a? Hash
    self.name = options.flatten.first
    options = options.flatten.last
    if options.is_a? Symbol or options.is_a? Proc
      @method = options
      options = {}
    else
      if options.is_a? Hash
        @method = options.delete(:method) || name.to_sym
      elsif options.is_a? Array
        @method = options.shift
        options = options.extract_options!
      else
        @method = name.to_sym
      end
    end
  end
  @formats = Formatting::Formats.new options
end

Instance Attribute Details

#accessor(*accessed_with) ⇒ Object

Returns the value of attribute accessor.



58
59
60
# File 'lib/maskable_attribute/mask.rb', line 58

def accessor
  @accessor
end

#formatsObject

Returns the value of attribute formats.



58
59
60
# File 'lib/maskable_attribute/mask.rb', line 58

def formats
  @formats
end

#methodObject

Returns the value of attribute method.



58
59
60
# File 'lib/maskable_attribute/mask.rb', line 58

def method
  @method
end

#nameObject

Returns the value of attribute name.



58
59
60
# File 'lib/maskable_attribute/mask.rb', line 58

def name
  @name
end

Instance Method Details

#accessed_byObject



117
118
119
120
121
# File 'lib/maskable_attribute/mask.rb', line 117

def accessed_by
  formats.names.map { |format| format.to_s + "_" + name}.tap do |accessed_by|
    accessed_by.push name unless formats.are_exclusive?
  end
end

#accessed_by?(possible_accessor) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/maskable_attribute/mask.rb', line 123

def accessed_by?(possible_accessor)
  accessed_by.include? possible_accessor
end

#unmask(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/maskable_attribute/mask.rb', line 97

def unmask(*args)
  object = args.first
  options = args.extract_options!
  format = (options[:formatted] || accessor).to_s.sub("_" + name.to_s, "").strip.to_sym

  formats.apply format do
    begin
      if @method.is_a? Proc
        @method.call object
      elsif @method.is_a? Symbol
        object.send(@method) if object.respond_to? @method
      else
        nil
      end
    rescue NoMethodError
      nil
    end
  end
end