Class: MaskableAttribute::Mask
- Inherits:
-
Object
- Object
- MaskableAttribute::Mask
- Defined in:
- lib/maskable_attribute/mask.rb
Instance Attribute Summary collapse
-
#accessor(*accessed_with) ⇒ Object
Returns the value of attribute accessor.
-
#formats ⇒ Object
Returns the value of attribute formats.
-
#method ⇒ Object
Returns the value of attribute method.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #accessed_by ⇒ Object
- #accessed_by?(possible_accessor) ⇒ Boolean
-
#initialize(options = nil) ⇒ Mask
constructor
A new instance of Mask.
- #unmask(*args) ⇒ Object
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(=nil) if .is_a? Symbol self.name = (@method = .to_sym) = {} elsif .is_a? Hash self.name = .flatten.first = .flatten.last if .is_a? Symbol or .is_a? Proc @method = = {} else if .is_a? Hash @method = .delete(:method) || name.to_sym elsif .is_a? Array @method = .shift = . else @method = name.to_sym end end end @formats = Formatting::Formats.new 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 |
#formats ⇒ Object
Returns the value of attribute formats.
58 59 60 |
# File 'lib/maskable_attribute/mask.rb', line 58 def formats @formats end |
#method ⇒ Object
Returns the value of attribute method.
58 59 60 |
# File 'lib/maskable_attribute/mask.rb', line 58 def method @method end |
#name ⇒ Object
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_by ⇒ Object
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
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 = args. format = ([: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 |