class BubbleWrap::KVO::Registry

Constants

COLLECTION_OPERATIONS
OPTION_MAP

Attributes

callbacks[R]
keys[R]

Public Class Methods

new(value_keys = [:old, :new]) click to toggle source
# File motion/core/kvo.rb, line 30
def initialize(value_keys = [:old, :new])
  @keys = value_keys.inject([]) do |acc, key|
    value_change_key = OPTION_MAP[key]

    if value_change_key.nil?
      raise RuntimeError, "Unknown value change key #{key}. Possible keys: #{OPTION_MAP.keys}"
    end

    acc << value_change_key
  end

  @callbacks = Hash.new do |hash, key|
    hash[key] = Hash.new do |subhash, subkey|
      subhash[subkey] = Array.new
    end
  end
end

Public Instance Methods

add(target, key_path, &block) click to toggle source
# File motion/core/kvo.rb, line 48
def add(target, key_path, &block)
  return if target.nil? || key_path.nil? || block.nil?

  block.weak! if BubbleWrap.use_weak_callbacks?

  callbacks[target][key_path.to_s] << block
end
each_key_path() { |target, key_path| ... } click to toggle source
# File motion/core/kvo.rb, line 77
def each_key_path
  callbacks.each do |target, key_paths|
    key_paths.each_key do |key_path|
      yield target, key_path
    end
  end
end
registered?(target, key_path) click to toggle source
# File motion/core/kvo.rb, line 69
def registered?(target, key_path)
  callbacks[target].has_key? key_path.to_s
end
remove(target, key_path) click to toggle source
# File motion/core/kvo.rb, line 56
def remove(target, key_path)
  return if target.nil? || key_path.nil?

  key_path = key_path.to_s

  callbacks[target].delete(key_path)

  # If there no key_paths left for target, remove the target key
  if callbacks[target].empty?
    callbacks.delete(target)
  end
end
remove_all() click to toggle source
# File motion/core/kvo.rb, line 73
def remove_all
  callbacks.clear
end

Private Instance Methods

observeValueForKeyPath(key_path, ofObject: target, change: change, context: context) click to toggle source
# File motion/core/kvo.rb, line 87
def observeValueForKeyPath(key_path, ofObject: target, change: change, context: context)
  key_paths = callbacks[target] || {}
  blocks = key_paths[key_path] || []

  args = change.values_at(*keys)
  args << key_path

  blocks.each do |block|
    block.call(*args)
  end
end