Module: Persistence::Object::Complex::Attributes::AttributesHash
- Includes:
- AccessorUtilities::AccessorMath
- Defined in:
- lib/persistence/object/complex/attributes/attributes_hash.rb
Overview
Module used for common methods for attributes hashes.
Instance Method Summary collapse
-
#add(key, reader_writer_accessor) ⇒ Object
Adds :reader, :writer or :accessor to key.
-
#add_without_hooks(key, reader_writer_accessor) ⇒ Object
Adds :reader, :writer or :accessor to key.
-
#delete(attribute) ⇒ :reader, ...
Deletes attribute and updates corresponding hashes/arrays.
-
#has_attributes?(attribute_name, ...) ⇒ true, false
Query whether this hash includes attribute(s).
-
#post_set_hook(key, value) ⇒ Object
post_set_hook #.
-
#pre_set_hook(key, value) ⇒ Object
pre_set_hook #.
-
#subtract(key, reader_writer_accessor) ⇒ Object
Subtracts :reader, :writer or :accessor to key.
-
#subtract_without_hooks(key, reader_writer_accessor) ⇒ Object
Subtracts :reader, :writer or :accessor from key.
Instance Method Details
#add(key, reader_writer_accessor) ⇒ Object
Adds :reader, :writer or :accessor to key.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 42 def add( key, reader_writer_accessor ) # figure out actual addition value existing_status = self[ key ] if actual_status = status_minus_other_status( reader_writer_accessor, existing_status ) # if we have an addition value, do so directly new_status = status_plus_other_status( existing_status, actual_status ) store_without_hooks( key, new_status ) # update corresponding structures for addition unless @without_hooks update_for_addition( key, actual_status ) end end return new_status end |
#add_without_hooks(key, reader_writer_accessor) ⇒ Object
Adds :reader, :writer or :accessor to key.
Used to prevent loops when array/hash relays to other arrays/hashes.
75 76 77 78 79 80 81 82 83 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 75 def add_without_hooks( key, reader_writer_accessor ) @without_hooks = true add( key, reader_writer_accessor ) @without_hooks = false end |
#delete(attribute) ⇒ :reader, ...
Deletes attribute and updates corresponding hashes/arrays.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 150 def delete( attribute ) deleted_reader_writer_accessor_setting = super( attribute ) unless @without_hooks update_for_subtraction( attribute, :accessor ) end return deleted_reader_writer_accessor_setting end |
#has_attributes?(attribute_name, ...) ⇒ true, false
Query whether this hash includes attribute(s).
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 175 def has_attributes?( *attributes ) has_attributes = false attributes.each do |this_attribute| break unless has_attributes = has_key?( this_attribute ) end return has_attributes end |
#post_set_hook(key, value) ⇒ Object
post_set_hook #
29 30 31 32 33 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 29 def post_set_hook( key, value ) # create method in configuration_instance end |
#pre_set_hook(key, value) ⇒ Object
pre_set_hook #
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 13 def pre_set_hook( key, value ) case value when nil, :reader, :writer, :accessor else raise ArgumentError, 'Permitted values: :reader, :writer, :accessor.' end return value end |
#subtract(key, reader_writer_accessor) ⇒ Object
Subtracts :reader, :writer or :accessor to key.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 92 def subtract( key, reader_writer_accessor ) # figure out actual subtraction value existing_status = self[ key ] result_value = status_minus_other_status( existing_status, reader_writer_accessor ) if actual_status = status_minus_other_status( reader_writer_accessor, result_value ) # if we have an actual value we are subtracting, do so directly (no hooks) if new_status = status_minus_other_status( existing_status, actual_status ) store_without_hooks( key, new_status ) elsif existing_status delete( key ) end # update corresponding structures for subtraction (pass actually-subtracted value) unless @without_hooks update_for_subtraction( key, actual_status ) end end return result_value end |
#subtract_without_hooks(key, reader_writer_accessor) ⇒ Object
Subtracts :reader, :writer or :accessor from key.
Used to prevent loops when array/hash relays to other arrays/hashes.
129 130 131 132 133 134 135 136 137 |
# File 'lib/persistence/object/complex/attributes/attributes_hash.rb', line 129 def subtract_without_hooks( key, reader_writer_accessor ) @without_hooks = true subtract( key, reader_writer_accessor ) @without_hooks = false end |