module Tantot::Observe::Helpers

Public Instance Methods

condition_proc(watch) click to toggle source
# File lib/tantot/observe.rb, line 6
def condition_proc(watch)
  attributes = watch.attributes
  options = watch.options
  proc do
    has_changes = attributes[:only].any? ? (self.destroyed? || (self._watch_changes.keys & attributes[:watched]).any?) : true
    has_changes && (!options.key?(:if) || self.instance_exec(&options[:if]))
  end
end
update_proc(watch) click to toggle source
# File lib/tantot/observe.rb, line 15
def update_proc(watch)
  proc do
    attributes = watch.attributes
    watched_changes =
      if attributes[:only].any?
        if self.destroyed?
          attributes[:watched].each.with_object({}) {|attr, hash| hash[attr] = [self.attributes[attr]]}
        else
          self._watch_changes.slice(*attributes[:watched])
        end
      else
        self._watch_changes
      end

    # If explicitly watching attributes, always include their values (if not already included through change tracking)
    attributes[:always].each do |attribute|
      watched_changes[attribute] = [self.attributes[attribute]] unless watched_changes.key?(attribute)
    end

    watch.agent.push(watch, self, watched_changes)
  end
end