Module: OSX::NSKVCBehaviorAttachment

Includes:
NSKVCAccessorUtil
Included in:
OCClsWrapper
Defined in:
src/ruby/osx/objc/oc_import.rb

Overview

Utility for Key-Value Coding.

Instance Method Summary (collapse)

Instance Method Details

- (Object) kvc_accessor(*args)

Defines accessor methods from given names. Writer methods aware Key-Value Observing.



623
624
625
626
# File 'src/ruby/osx/objc/oc_import.rb', line 623

def kvc_accessor(*args)
  kvc_reader(*args)
  kvc_writer(*args)
end

- (Object) kvc_array_accessor(*args)

Define accessors that send change notifications for an array. The array instance variable must respond to the following methods:

  • length

  • [index]

  • [index]=

  • insert(index,obj)

  • delete_at(index)

Notifications are only sent for accesses through the Cocoa methods: countOfKey, objectInKeyAtIndex, insertObject_inKeyAtIndex, removeObjectFromKeyAtIndex, replaceObjectInKeyAtIndex_withObject



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'src/ruby/osx/objc/oc_import.rb', line 677

def kvc_array_accessor(*args)
  args.each do |key|
    keyname = key.to_s
    keyname[0..0] = keyname[0..0].upcase
    self.addRubyMethod_withType("countOf#{keyname}".to_sym, "i4@8:12")
    self.addRubyMethod_withType("objectIn#{keyname}AtIndex:".to_sym, "@4@8:12i16")
    self.addRubyMethod_withType("insertObject:in#{keyname}AtIndex:".to_sym, "@4@8:12@16i20")
    self.addRubyMethod_withType("removeObjectFrom#{keyname}AtIndex:".to_sym, "@4@8:12i16")
    self.addRubyMethod_withType("replaceObjectIn#{keyname}AtIndex:withObject:".to_sym, "@4@8:12i16@20")
    # get%s:range: - unimplemented. You can implement this method for performance improvements.
    self.class_eval <<-EOT,__FILE__,__LINE__+1
      def countOf#{keyname}()
        @#{key.to_s}.length
      end

      def objectIn#{keyname}AtIndex(index)
        @#{key.to_s}[index]
      end

      def insertObject_in#{keyname}AtIndex(obj, index)
        indexes = OSX::NSIndexSet.indexSetWithIndex(index)
 if self.class.automaticallyNotifiesObserversForKey('#{key.to_s}')
   willChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeInsertion, indexes, #{key.inspect})
   @#{key.to_s}.insert(index, obj)
   didChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeInsertion, indexes, #{key.inspect})
 else
   @#{key.to_s}.insert(index, obj)
 end
        nil
      end

      def removeObjectFrom#{keyname}AtIndex(index)
        indexes = OSX::NSIndexSet.indexSetWithIndex(index)
 if self.class.automaticallyNotifiesObserversForKey('#{key.to_s}')
   willChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeRemoval, indexes, #{key.inspect})
   @#{key.to_s}.delete_at(index)
   didChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeRemoval, indexes, #{key.inspect})
 else
   @#{key.to_s}.delete_at(index)
 end
        nil
      end

      def replaceObjectIn#{keyname}AtIndex_withObject(index, obj)
        indexes = OSX::NSIndexSet.indexSetWithIndex(index)
 if self.class.automaticallyNotifiesObserversForKey('#{key.to_s}')
   willChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeReplacement, indexes, #{key.inspect})
   @#{key.to_s}[index] = obj
   didChange_valuesAtIndexes_forKey(OSX::NSKeyValueChangeReplacement, indexes, #{key.inspect})
 else
   @#{key.to_s}[index] = obj
 end
        nil
      end
    EOT
  end
end

- (Object) kvc_depends_on(keys, *dependencies)

see sample/CurrencyConverter/.



629
630
631
632
633
# File 'src/ruby/osx/objc/oc_import.rb', line 629

def kvc_depends_on(keys, *dependencies)
  dependencies.flatten.each do |dependentKey|
    setKeys_triggerChangeNotificationsForDependentKey(Array(keys), dependentKey)
  end
end

- (Object) kvc_reader(*args)

See Also:



595
596
597
# File 'src/ruby/osx/objc/oc_import.rb', line 595

def kvc_reader(*args)
  attr_reader(*args)
end

- (Object) kvc_wrapper(*keys)

Defines accesor methods for keys defined in Objective-C, such as NSUserDefaultsController and NSManagedObject.



637
638
639
640
# File 'src/ruby/osx/objc/oc_import.rb', line 637

def kvc_wrapper(*keys)
  kvc_wrapper_reader(*keys)
  kvc_wrapper_writer(*keys)
end

- (Object) kvc_wrapper_reader(*keys)

See Also:



643
644
645
646
647
648
649
650
651
# File 'src/ruby/osx/objc/oc_import.rb', line 643

def kvc_wrapper_reader(*keys)
  keys.flatten.compact.each do |key|
    class_eval <<-EOE_KVC_WRAPPER,__FILE__,__LINE__+1
      def #{key}
        valueForKey("#{key}")
      end
    EOE_KVC_WRAPPER
  end
end

- (Object) kvc_wrapper_writer(*keys)

See Also:



654
655
656
657
658
659
660
661
662
# File 'src/ruby/osx/objc/oc_import.rb', line 654

def kvc_wrapper_writer(*keys)
  keys.flatten.compact.each do |key|
    class_eval <<-EOE_KVC_WRAPPER,__FILE__,__LINE__+1
      def #{key}=(val)
        setValue_forKey(val, "#{key}")
      end
    EOE_KVC_WRAPPER
  end
end

- (Object) kvc_writer(*args)

See Also:



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'src/ruby/osx/objc/oc_import.rb', line 600

def kvc_writer(*args)
  args.flatten.each do |key|
	next if method_defined?(kvc_setter_wrapper(key))
    setter = key.to_s + '='
    attr_writer(key) unless method_defined?(setter)
    alias_method kvc_internal_setter(key), setter
    self.class_eval <<-EOE_KVC_WRITER,__FILE__,__LINE__+1
      def #{kvc_setter_wrapper(key)}(value)
 if self.class.automaticallyNotifiesObserversForKey('#{key.to_s}')
   willChangeValueForKey('#{key.to_s}')
   send('#{kvc_internal_setter(key)}', value)
   didChangeValueForKey('#{key.to_s}')
 else
   send('#{kvc_internal_setter(key)}', value)
 end
      end
    EOE_KVC_WRITER
    alias_method setter, kvc_setter_wrapper(key)
  end
end