Module: WriteOnce::HasWriteOnceAttributes
- Defined in:
- lib/write_once.rb
Overview
:nodoc:
Instance Method Summary collapse
- #_write_attribute(attr_name, value) ⇒ Object
- #attr_is_write_only?(attr_name) ⇒ Boolean
- #handle_error(attr_name, value) ⇒ Object
- #valid_change?(attr_name, new_value) ⇒ Boolean
- #write_attribute(attr_name, value) ⇒ Object
Instance Method Details
#_write_attribute(attr_name, value) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/write_once.rb', line 80 def _write_attribute(attr_name, value) if !new_record? && attr_is_write_only?(attr_name) && !valid_change?(attr_name, value) handle_error(attr_name, value) end super end |
#attr_is_write_only?(attr_name) ⇒ Boolean
68 69 70 |
# File 'lib/write_once.rb', line 68 def attr_is_write_only?(attr_name) self.class.write_once_attribute?(attr_name.to_s) end |
#handle_error(attr_name, value) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/write_once.rb', line 88 def handle_error(attr_name, value) if WriteOnce.config.enforce_errors raise WriteOnceAttributeError, attr_name else WriteOnce.config.logger.warn( message: "Write once attribute #{attr_name} changed", old_value: send(attr_name), new_value: value, ) end end |
#valid_change?(attr_name, new_value) ⇒ Boolean
63 64 65 66 |
# File 'lib/write_once.rb', line 63 def valid_change?(attr_name, new_value) current_value = send(attr_name) current_value.nil? || current_value == new_value end |
#write_attribute(attr_name, value) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/write_once.rb', line 72 def write_attribute(attr_name, value) if !new_record? && attr_is_write_only?(attr_name) && !valid_change?(attr_name, value) handle_error(attr_name, value) end super end |