Class: BubbleWrap::KVO::Registry
- Inherits:
-
Object
- Object
- BubbleWrap::KVO::Registry
- Defined in:
- motion/core/kvo.rb
Constant Summary collapse
- COLLECTION_OPERATIONS =
[ NSKeyValueChangeInsertion, NSKeyValueChangeRemoval, NSKeyValueChangeReplacement ]
- OPTION_MAP =
{ new: NSKeyValueChangeNewKey, old: NSKeyValueChangeOldKey }
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
- #add(target, key_path, &block) ⇒ Object
- #each_key_path ⇒ Object
-
#initialize(value_keys = [:old, :new]) ⇒ Registry
constructor
A new instance of Registry.
- #registered?(target, key_path) ⇒ Boolean
- #remove(target, key_path) ⇒ Object
- #remove_all ⇒ Object
Constructor Details
#initialize(value_keys = [:old, :new]) ⇒ Registry
Returns a new instance of Registry.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
28 29 30 |
# File 'motion/core/kvo.rb', line 28 def callbacks @callbacks end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
28 29 30 |
# File 'motion/core/kvo.rb', line 28 def keys @keys end |
Instance Method Details
#add(target, key_path, &block) ⇒ Object
48 49 50 51 52 53 54 |
# 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 ⇒ Object
77 78 79 80 81 82 83 |
# 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) ⇒ Boolean
69 70 71 |
# 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) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# 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 ⇒ Object
73 74 75 |
# File 'motion/core/kvo.rb', line 73 def remove_all callbacks.clear end |