Class: Petra::Proxies::Handlers::AttributeReadHandler

Inherits:
MissingMethodHandler show all
Defined in:
lib/petra/proxies/handlers/attribute_read_handler.rb

Instance Attribute Summary

Attributes inherited from MissingMethodHandler

#proxy

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MissingMethodHandler

add_constraint, constraints, #initialize, #queue_constraints

Constructor Details

This class inherits a constructor from Petra::Proxies::Handlers::MissingMethodHandler

Class Method Details

.identifierObject



9
10
11
# File 'lib/petra/proxies/handlers/attribute_read_handler.rb', line 9

def self.identifier
  :attribute_read
end

Instance Method Details

#applicable?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/petra/proxies/handlers/attribute_read_handler.rb', line 13

def applicable?(method_name)
  proxy.send(:__attribute_reader?, method_name)
end

#handle(method_name, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/petra/proxies/handlers/attribute_read_handler.rb', line 17

def handle(method_name, *args)
  if transaction.attribute_value?(@proxy, attribute: method_name)
    # As we read this attribute before, we have the value we read back then on record.
    # Therefore, we may check if the value changed in the mean time which would invalidate
    # the transaction (most likely).
    transaction.verify_attribute_integrity!(@proxy, attribute: method_name)

    transaction.attribute_value(@proxy, attribute: method_name).tap do |result|
      Petra.logger.debug "Served value from write set: #{method_name}  => #{result}", :yellow, :bold
    end
  elsif transaction.read_attribute_value?(@proxy, attribute: method_name)
    # If we didn't write the attribute before, we may at least have already read it.
    # In this case, we don't have to generate a new read log entry
    transaction.verify_attribute_integrity!(@proxy, attribute: method_name)

    # We also may simply return the last accepted read set value
    transaction.read_attribute_value(@proxy, attribute: method_name).tap do |result|
      Petra.logger.debug "Re-read attribute: #{method_name}  => #{result}", :yellow, :bold
    end
  else
    proxied_object.send(method_name, *args).tap do |val|
      transaction.log_attribute_read(@proxy, attribute: method_name, value: val, method: method_name)
    end
  end
end