Class: AttrMasker::Attribute
- Inherits:
-
Object
- Object
- AttrMasker::Attribute
- Defined in:
- lib/attr_masker/attribute.rb
Overview
Holds the definition of maskable attribute.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #column_name ⇒ Object
-
#evaluate_option(option_name, model_instance) ⇒ Object
Evaluates option (typically
:if
or:unless
) on given model instance. -
#initialize(name, model, options) ⇒ Attribute
constructor
A new instance of Attribute.
- #marshal_data(data) ⇒ Object
-
#mask(model_instance) ⇒ Object
Mask the attribute on given model.
-
#should_mask?(model_instance) ⇒ Boolean
Evaluates the
:if
and:unless
attribute options on given instance. - #unmarshal_data(data) ⇒ Object
Constructor Details
#initialize(name, model, options) ⇒ Attribute
Returns a new instance of Attribute.
9 10 11 12 13 |
# File 'lib/attr_masker/attribute.rb', line 9 def initialize(name, model, ) @name = name.to_sym @model = model @options = end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/attr_masker/attribute.rb', line 7 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/attr_masker/attribute.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/attr_masker/attribute.rb', line 7 def @options end |
Instance Method Details
#column_name ⇒ Object
66 67 68 |
# File 'lib/attr_masker/attribute.rb', line 66 def column_name [:column_name] || name end |
#evaluate_option(option_name, model_instance) ⇒ Object
Evaluates option (typically :if
or :unless
) on given model instance. That option can be either a proc (a model is passed as an only argument), or a symbol (a method of that name is called on model instance).
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/attr_masker/attribute.rb', line 44 def evaluate_option(option_name, model_instance) option = [option_name] if option.is_a?(Symbol) model_instance.send(option) elsif option.respond_to?(:call) option.call(model_instance) else option end end |
#marshal_data(data) ⇒ Object
56 57 58 59 |
# File 'lib/attr_masker/attribute.rb', line 56 def marshal_data(data) return data unless [:marshal] [:marshaler].send([:dump_method], data) end |
#mask(model_instance) ⇒ Object
Mask the attribute on given model. Masking will be performed regardless of :if
and :unless
options. A should_mask?
method should be called separately to ensure that given object is eligible for masking.
The method returns the masked value but does not modify the object’s attribute.
If marshal
attribute’s option is true
, the attribute value will be loaded before masking, and dumped to proper storage format prior returning.
35 36 37 38 39 |
# File 'lib/attr_masker/attribute.rb', line 35 def mask(model_instance) value = unmarshal_data(model_instance.send(name)) masker_value = [:masker].call(.merge!(value: value)) marshal_data(masker_value) end |
#should_mask?(model_instance) ⇒ Boolean
Evaluates the :if
and :unless
attribute options on given instance. Returns true
or fasle
, depending on whether the attribute should be masked for this object or not.
18 19 20 21 22 23 |
# File 'lib/attr_masker/attribute.rb', line 18 def should_mask?(model_instance) not ( .key?(:if) && !evaluate_option(:if, model_instance) || .key?(:unless) && evaluate_option(:unless, model_instance) ) end |
#unmarshal_data(data) ⇒ Object
61 62 63 64 |
# File 'lib/attr_masker/attribute.rb', line 61 def unmarshal_data(data) return data unless [:marshal] [:marshaler].send([:load_method], data) end |