Module: BubbleWrap::KVO

Defined in:
motion/core/kvo.rb

Constant Summary collapse

COLLECTION_OPERATIONS =
[ NSKeyValueChangeInsertion, NSKeyValueChangeRemoval, NSKeyValueChangeReplacement ]
DEFAULT_OPTIONS =
NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld

Instance Method Summary collapse

Instance Method Details

#observe(*arguments, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'motion/core/kvo.rb', line 24

def observe(*arguments, &block)
  unless [1,2].include?(arguments.length)
    raise ArgumentError, "wrong number of arguments (#{arguments.length} for 1 or 2)"
  end

  key_path = arguments.pop
  target   = arguments.pop || self

  target.addObserver(self, forKeyPath:key_path, options:DEFAULT_OPTIONS, context:nil) unless registered?(target, key_path)
  add_observer_block(target, key_path, &block)
end

#unobserve(*arguments) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'motion/core/kvo.rb', line 36

def unobserve(*arguments)
  unless [1,2].include?(arguments.length)
    raise ArgumentError, "wrong number of arguments (#{arguments.length} for 1 or 2)"
  end

  key_path = arguments.pop
  target   = arguments.pop || self

  return unless registered?(target, key_path)

  target.removeObserver(self, forKeyPath:key_path)
  remove_observer_block(target, key_path)
end

#unobserve_allObject



50
51
52
53
54
55
56
57
58
59
# File 'motion/core/kvo.rb', line 50

def unobserve_all
  return if @targets.nil?

  @targets.each do |target, key_paths|
    key_paths.each_key do |key_path|
      target.removeObserver(self, forKeyPath:key_path)
    end
  end
  remove_all_observer_blocks
end