class Darko::Watcher

Attributes

delegator[RW]

Public Class Methods

new(object, attribute=nil, log_to_file=false) click to toggle source
# File lib/darko/watcher.rb, line 4
def initialize object, attribute=nil, log_to_file=false
  @object = object
  @attribute = attribute
  @delegator = Darko::Delegator.new(attribute_target, log_to_file)
end

Public Instance Methods

disable!() click to toggle source

Disabling the Darko::Watcher will reset the object attribute to the original - removing the delegator

# File lib/darko/watcher.rb, line 16
def disable!
  swap_out_delegator
end
enable!() click to toggle source

Enabling the Darko::Watcher replaces the object with a Darko::Delegator to spy on mutations

# File lib/darko/watcher.rb, line 11
def enable!
  swap_in_delegator
end

Private Instance Methods

attribute_target() click to toggle source
# File lib/darko/watcher.rb, line 44
def attribute_target
  @object.send(target_getter_message, @attribute)
end
class_target?() click to toggle source
# File lib/darko/watcher.rb, line 31
def class_target?
  @object.is_a?(Class) && @object.class_variables.include?(@attribute)
end
swap_in_delegator() click to toggle source
# File lib/darko/watcher.rb, line 27
def swap_in_delegator
  @object.send(target_setter_message, @attribute, @delegator)
end
swap_out_delegator() click to toggle source
# File lib/darko/watcher.rb, line 22
def swap_out_delegator
  original_obj = @delegator.instance_variable_get(:@delegate_sd_obj)
  @object.send(target_setter_message, @attribute, original_obj)
end
target_getter_message() click to toggle source

Depending on if the target object is a class or instance changes what message we need to send, some memoization just to avoid if/elses - I'd like this to live elsewhere

# File lib/darko/watcher.rb, line 37
def target_getter_message
  @_target_getter_message ||= class_target? ? :class_variable_get : :instance_variable_get
end
target_setter_message() click to toggle source
# File lib/darko/watcher.rb, line 40
def target_setter_message
  @_target_setter_message ||= class_target? ? :class_variable_set : :instance_variable_set
end