Class: Petra::Components::ProxyCache
- Inherits:
-
Object
- Object
- Petra::Components::ProxyCache
- Defined in:
- lib/petra/components/proxy_cache.rb
Overview
This class encapsulates the methods a transaction may use to gather information about the objects which were used during its execution.
It also functions as an object cache, mapping proxy keys to (temporary) object proxies
Instance Method Summary collapse
-
#created(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects that were initialized and persisted during the transaction.
-
#created?(proxy) ⇒ Boolean
trueif the given object (proxy) was initialized AND persisted during the transaction. -
#current_numerical_id ⇒ Object
—————————————————————- Helpers —————————————————————-.
-
#destroyed(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects which were destroyed within the transaction.
-
#destroyed?(proxy) ⇒ Boolean
trueif the given object was destroyed during this transaction. -
#existing?(proxy) ⇒ Boolean
trueif the given object existed before the transaction started. -
#fateful(klass = nil) ⇒ Object
Objects that will have impact during the commit phase in order of their appearance during the transaction’s execution.
-
#fetch(key) ⇒ Petra::Proxies::ObjectProxy
(also: #[])
Returns the proxy for the given object key from cache.
- #inc_current_numerical_id ⇒ Object
-
#initialize(transaction) ⇒ ProxyCache
constructor
A new instance of ProxyCache.
-
#initialized(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects which were initialized within the transaction, but not yet object persisted.
-
#initialized?(proxy) ⇒ Boolean
trueif the given object (proxy) was initialized, but not yet persisted during this transaction. -
#initialized_or_created(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Like #created, but it will also include not-yet persisted objects of non-persisted sections.
-
#new?(proxy) ⇒ Boolean
trueif the given object did not exist outside of the transaction, meaning that it was initialized and optionally persisted during its execution. - #next_id ⇒ Object
-
#read(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects that were read during the transaction.
-
#read_attributes ⇒ Hash<Petra::Proxies::ObjectProxy, Array<String,Symbol>>
All attributes which were read during this transaction grouped by the objects (proxies) they belong to.
-
#verify_read_attributes!(force: false) ⇒ Object
Performs an integrity check on all attributes which were read in this transaction.
Constructor Details
#initialize(transaction) ⇒ ProxyCache
Returns a new instance of ProxyCache.
12 13 14 |
# File 'lib/petra/components/proxy_cache.rb', line 12 def initialize(transaction) @transaction = transaction end |
Instance Method Details
#created(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects that were initialized and persisted during the transaction
77 78 79 |
# File 'lib/petra/components/proxy_cache.rb', line 77 def created(klass = nil) filtered_objects(:created_objects, klass) end |
#created?(proxy) ⇒ Boolean
Returns true if the given object (proxy) was initialized AND persisted during the transaction.
121 122 123 |
# File 'lib/petra/components/proxy_cache.rb', line 121 def created?(proxy) created.include?(proxy) end |
#current_numerical_id ⇒ Object
Helpers
168 169 170 171 172 173 |
# File 'lib/petra/components/proxy_cache.rb', line 168 def current_numerical_id # FIXME: The string comparison will not work for numbers > 10! It has to be replaced with a numeric comparison! # FIXME: This also causes problems when the last new element is deleted and a new one created @current_numerical_id ||= (initialized_or_created.max_by(&:__object_id)&.__object_id || 'new_0') .match(/new_(\d+)/)[1].to_i end |
#destroyed(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Returns objects which were destroyed within the transaction.
112 113 114 |
# File 'lib/petra/components/proxy_cache.rb', line 112 def destroyed(klass = nil) filtered_objects(:destroyed_objects, klass) end |
#destroyed?(proxy) ⇒ Boolean
Returns true if the given object was destroyed during this transaction.
160 161 162 |
# File 'lib/petra/components/proxy_cache.rb', line 160 def destroyed?(proxy) destroyed.include?(proxy) end |
#existing?(proxy) ⇒ Boolean
Returns true if the given object existed before the transaction started.
151 152 153 |
# File 'lib/petra/components/proxy_cache.rb', line 151 def existing?(proxy) !new?(proxy) end |
#fateful(klass = nil) ⇒ Object
Objects that will have impact during the commit phase in order of their appearance during the transaction’s execution.
55 56 57 |
# File 'lib/petra/components/proxy_cache.rb', line 55 def fateful(klass = nil) filtered_objects(:objects, klass) end |
#fetch(key) ⇒ Petra::Proxies::ObjectProxy Also known as: []
Returns the proxy for the given object key from cache. If there is no valid object cached yet, the given block is executed and its result saved under the given key.
23 24 25 26 27 28 |
# File 'lib/petra/components/proxy_cache.rb', line 23 def fetch(key) @cache ||= {} return @cache[key.to_s] if @cache.key?(key.to_s) fail ArgumentError, "Object `#{key}` is not cached and no block was given." unless block_given? @cache[key.to_s] = yield end |
#inc_current_numerical_id ⇒ Object
175 176 177 |
# File 'lib/petra/components/proxy_cache.rb', line 175 def inc_current_numerical_id @current_numerical_id = current_numerical_id + 1 end |
#initialized(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Returns objects which were initialized within the transaction, but not yet object persisted.
101 102 103 |
# File 'lib/petra/components/proxy_cache.rb', line 101 def initialized(klass = nil) filtered_objects(:initialized_objects, klass) end |
#initialized?(proxy) ⇒ Boolean
Returns true if the given object (proxy) was initialized, but not yet persisted during this transaction. This means in particular that the object did not exist before the transaction started.
132 133 134 |
# File 'lib/petra/components/proxy_cache.rb', line 132 def initialized?(proxy) initialized.include?(proxy) end |
#initialized_or_created(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Like #created, but it will also include not-yet persisted objects of non-persisted sections
89 90 91 |
# File 'lib/petra/components/proxy_cache.rb', line 89 def initialized_or_created(klass = nil) filtered_objects(:initialized_or_created_objects, klass) end |
#new?(proxy) ⇒ Boolean
Returns true if the given object did not exist outside of the transaction, meaning that it was initialized and optionally persisted during its execution.
142 143 144 |
# File 'lib/petra/components/proxy_cache.rb', line 142 def new?(proxy) current_section.recently_initialized_object?(proxy) || initialized_or_created.include?(proxy) end |
#next_id ⇒ Object
179 180 181 |
# File 'lib/petra/components/proxy_cache.rb', line 179 def next_id format('new_%05d', inc_current_numerical_id) end |
#read(klass = nil) ⇒ Array<Petra::Proxies::ObjectProxy>
Objects that were read during the transaction
66 67 68 |
# File 'lib/petra/components/proxy_cache.rb', line 66 def read(klass = nil) filtered_objects(:read_objects, klass) end |
#read_attributes ⇒ Hash<Petra::Proxies::ObjectProxy, Array<String,Symbol>>
Returns All attributes which were read during this transaction grouped by the objects (proxies) they belong to.
42 43 44 45 46 47 48 49 |
# File 'lib/petra/components/proxy_cache.rb', line 42 def read_attributes sections.each_with_object({}) do |section, h| section.read_attributes.each do |proxy, attributes| h[proxy] ||= [] h[proxy] = (h[proxy] + attributes).uniq end end end |
#verify_read_attributes!(force: false) ⇒ Object
Performs an integrity check on all attributes which were read in this transaction
188 189 190 191 192 |
# File 'lib/petra/components/proxy_cache.rb', line 188 def verify_read_attributes!(force: false) read_attributes.each do |proxy, attributes| attributes.each { |a| verify_attribute_integrity!(proxy, attribute: a, force: force) } end end |