Module: NoBrainer::Document::Store::ClassMethods
- Defined in:
- lib/no_brainer/document/store.rb
Instance Method Summary collapse
-
#_store_accessors_module ⇒ Object
:nodoc:.
- #store(store_attribute, options = {}) ⇒ Object
- #store_accessor(store_attribute, *keys, prefix: nil, suffix: nil) ⇒ Object
- #stored_attributes ⇒ Object
Instance Method Details
#_store_accessors_module ⇒ Object
:nodoc:
216 217 218 219 220 221 222 |
# File 'lib/no_brainer/document/store.rb', line 216 def _store_accessors_module # :nodoc: @_store_accessors_module ||= begin mod = Module.new include mod mod end end |
#store(store_attribute, options = {}) ⇒ Object
111 112 113 |
# File 'lib/no_brainer/document/store.rb', line 111 def store(store_attribute, = {}) store_accessor(store_attribute, [:accessors], **.slice(:prefix, :suffix)) if .has_key? :accessors end |
#store_accessor(store_attribute, *keys, prefix: nil, suffix: nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/no_brainer/document/store.rb', line 115 def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil) keys = keys.flatten accessor_prefix = case prefix when String, Symbol "#{prefix}_" when TrueClass "#{store_attribute}_" else "" end accessor_suffix = case suffix when String, Symbol "_#{suffix}" when TrueClass "_#{store_attribute}" else "" end field store_attribute, type: Hash, default: {} unless has_field?(store_attribute) define_method("#{store_attribute}=") do |value| super(value) if value.is_a?(Hash) || value.nil? end _store_accessors_module.module_eval do keys.each do |key| accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}" define_method("#{accessor_key}=") do |value| write_store_attribute(store_attribute, key, value) end define_method(accessor_key) do read_store_attribute(store_attribute, key) end define_method("#{accessor_key}_changed?") do return false unless __send__("#{store_attribute}_changed?") prev_store, new_store = changes[store_attribute] if NoBrainer.rails4? (prev_store && prev_store[key.to_s]) != (new_store && new_store[key.to_s]) else (prev_store && prev_store.dig(key)) != (new_store && new_store.dig(key)) end end define_method("#{accessor_key}_change") do return unless __send__("#{store_attribute}_changed?") prev_store, new_store = changes[store_attribute] if NoBrainer.rails4? [(prev_store && prev_store[key.to_s]), (new_store && new_store[key.to_s])] else [(prev_store && prev_store.dig(key)), (new_store && new_store.dig(key))] end end define_method("#{accessor_key}_was") do return unless __send__("#{store_attribute}_changed?") prev_store, _new_store = changes[store_attribute] if NoBrainer.rails4? (prev_store && prev_store[key.to_s]) else (prev_store && prev_store.dig(key)) end end # NoBrainer doesn't have `attribute_will_change!` so those methods # can't be implemented yet. # See https://github.com/NoBrainerORM/nobrainer/pull/190 # # define_method("saved_change_to_#{accessor_key}?") do # return false unless __send__("saved_change_to_#{store_attribute}?") # prev_store, new_store = __send__("saved_change_to_#{store_attribute}") # prev_store&.dig(key) != new_store&.dig(key) # end # define_method("saved_change_to_#{accessor_key}") do # return unless __send__("saved_change_to_#{store_attribute}?") # prev_store, new_store = __send__("saved_change_to_#{store_attribute}") # [prev_store&.dig(key), new_store&.dig(key)] # end # define_method("#{accessor_key}_before_last_save") do # return unless __send__("saved_change_to_#{store_attribute}?") # prev_store, _new_store = __send__("saved_change_to_#{store_attribute}") # prev_store&.dig(key) # end end end # assign new store attribute and create new hash to ensure that each class in the hierarchy # has its own hash of stored attributes. self.local_stored_attributes ||= {} self.local_stored_attributes[store_attribute] ||= [] self.local_stored_attributes[store_attribute] |= keys end |
#stored_attributes ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/no_brainer/document/store.rb', line 224 def stored_attributes parent = superclass.respond_to?(:stored_attributes) ? superclass.stored_attributes : {} if local_stored_attributes parent.merge!(local_stored_attributes) { |k, a, b| a | b } end parent end |