Module: Tantot::Observe::Helpers

Included in:
Tantot::Observe
Defined in:
lib/tantot/observe.rb

Instance Method Summary collapse

Instance Method Details

#condition_proc(watch) ⇒ Object



6
7
8
9
10
11
12
13
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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