[macruby-changes] [350] MacRuby/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Thu Jul 10 01:08:36 PDT 2008
Revision: 350
http://trac.macosforge.org/projects/ruby/changeset/350
Author: psychs at limechat.net
Date: 2008-07-10 01:08:35 -0700 (Thu, 10 Jul 2008)
Log Message:
-----------
Added convenient methods for RubyCocoa compatibility layer.
Modified Paths:
--------------
MacRuby/trunk/lib/osx/rubycocoa.rb
MacRuby/trunk/test-macruby/rubycocoa_test.rb
Modified: MacRuby/trunk/lib/osx/rubycocoa.rb
===================================================================
--- MacRuby/trunk/lib/osx/rubycocoa.rb 2008-07-10 06:10:25 UTC (rev 349)
+++ MacRuby/trunk/lib/osx/rubycocoa.rb 2008-07-10 08:08:35 UTC (rev 350)
@@ -66,6 +66,19 @@
raise NoMethodError, "undefined method `#{mname}' for #{inspect}:#{self.class}"
end
+
+ def to_ruby
+ case self
+ when NSDate
+ to_time
+ when NSNumber
+ integer? ? to_i : to_f
+ when NSAttributedString
+ string
+ else
+ self
+ end
+ end
end
module OSX
@@ -91,3 +104,224 @@
end
end
include OSX
+
+class NSUserDefaults
+ def [](key)
+ objectForKey(key)
+ end
+
+ def []=(key, obj)
+ setObject(obj, forKey:key)
+ end
+
+ def delete(key)
+ removeObjectForKey(key)
+ end
+end
+
+class NSIndexSet
+ def to_a
+ result = []
+ index = firstIndex
+ until index == NSNotFound
+ result << index
+ index = indexGreaterThanIndex(index)
+ end
+ return result
+ end
+
+ def inspect
+ "#<#{self.class} #{self.to_a.inspect}>"
+ end
+end
+
+class NSNumber
+ def to_i
+ self.intValue
+ end
+
+ def to_f
+ self.doubleValue
+ end
+
+ def float?
+ warn "#{caller[0]}: 'NSNumber#float?' is now deprecated and its use is discouraged, please use integer? instead."
+ CFNumberIsFloatType(self)
+ end
+
+ def integer?
+ !CFNumberIsFloatType(self)
+ end
+
+ def inspect
+ "#<#{self.class} #{self.description}>"
+ end
+end
+
+class NSDate
+ def to_time
+ Time.at(timeIntervalSince1970)
+ end
+
+ def inspect
+ "#<#{self.class} #{self.description}>"
+ end
+end
+
+class NSImage
+ def focus
+ lockFocus
+ begin
+ yield
+ ensure
+ unlockFocus
+ end
+ end
+end
+
+class NSRect
+ class << self
+ alias_method :orig_new, :new
+ def new(*args)
+ origin, size = case args.size
+ when 0
+ [[0, 0], [0, 0]]
+ when 2
+ [args[0], args[1]]
+ when 4
+ [args[0..1], args[2..3]]
+ else
+ raise ArgumentError, "wrong number of arguments (#{args.size} for either 0, 2 or 4)"
+ end
+ origin = NSPoint.new(*origin) unless origin.is_a?(NSPoint)
+ size = NSSize.new(*size) unless size.is_a?(NSSize)
+ orig_new(origin, size)
+ end
+ end
+
+ def x; origin.x; end
+ def y; origin.y; end
+ def width; size.width; end
+ def height; size.height; end
+ def x=(v); origin.x = v; end
+ def y=(v); origin.y = v; end
+ def width=(v); size.width = v; end
+ def height=(v); size.height = v; end
+ alias_method :old_to_a, :to_a # To remove a warning.
+ def to_a; [origin.to_a, size.to_a]; end
+ def center; NSPoint.new(NSMidX(self), NSMidY(self)); end
+
+ def contain?(arg)
+ case arg
+ when NSRect
+ NSContainsRect(self, arg)
+ when NSPoint
+ NSPointInRect(arg, self)
+ else
+ raise ArgumentError, "argument should be NSRect or NSPoint"
+ end
+ end
+
+ def empty?; NSIsEmptyRect(self); end
+ def inflate(dx, dy); inset(-dx, -dy); end
+ def inset(dx, dy); NSInsetRect(self, dx, dy); end
+ def integral; NSIntegralRect(self); end
+ def intersect?(rect); NSIntersectsRect(self, rect); end
+ def intersection(rect); NSIntersectionRect(self, rect); end
+ def offset(dx, dy); NSOffsetRect(self, dx, dy); end
+ def union(rect); NSUnionRect(self, rect); end
+ def inspect; "#<#{self.class} x=#{x}, y=#{y}, width=#{width}, height=#{height}>"; end
+end
+
+class NSPoint
+ def in?(rect); NSPointInRect(self, rect); end
+ alias_method :inRect?, :in?
+
+ def +(v)
+ if v.is_a?(NSSize)
+ NSPoint.new(x + v.width, y + v.height)
+ else
+ raise ArgumentException, "parameter should be NSSize"
+ end
+ end
+
+ def -(v)
+ if v.is_a?(NSSize)
+ NSPoint.new(x - v.width, y - v.height)
+ else
+ raise ArgumentException, "parameter should be NSSize"
+ end
+ end
+
+ def inspect; "#<#{self.class} x=#{x}, y=#{y}>"; end
+end
+
+class NSSize
+ def /(v); NSSize.new(width / v, height / v); end
+ def *(v); NSSize.new(width * v, height * v); end
+ def +(v); NSSize.new(width + v, height + v); end
+ def -(v); NSSize.new(width - v, height - v); end
+ def inspect; "#<#{self.class} width=#{width}, height=#{height}>"; end
+end
+
+class NSRange
+ class << self
+ alias_method :orig_new, :new
+ def new(*args)
+ location, length = case args.size
+ when 0
+ [0, 0]
+ when 1
+ if args.first.is_a?(Range)
+ range = args.first
+ [range.first, range.last - range.first + (range.exclude_end? ? 0 : 1)]
+ else
+ raise ArgumentError, "wrong type of argument #1 (expected Range, got #{args.first.class})"
+ end
+ when 2
+ if args.first.is_a?(Range)
+ range, count = args
+ first = range.first
+ first += count if first < 0
+ last = range.last
+ last += count if last < 0
+ len = last - first + (range.exclude_end? ? 0 : 1)
+ len = count - first if count < first + len
+ len = 0 if len < 0
+ [first, len]
+ else
+ [args[0], args[1]]
+ end
+ else
+ raise ArgumentError, "wrong number of arguments (#{args.size} for either 0, 1 or 2)"
+ end
+ orig_new(location, length)
+ end
+ end
+
+ def to_range
+ Range.new(location, location + length, true)
+ end
+
+ def size; length; end
+ def size=(v); length = v; end
+
+ def contain?(arg)
+ case arg
+ when NSRange
+ location <= arg.location and arg.location + arg.length <= location + length
+ when Numeric
+ NSLocationInRange(arg, self)
+ else
+ raise ArgumentError, "argument should be NSRange or Numeric"
+ end
+ end
+
+ def empty?; length == 0 || not_found?; end
+ def intersect?(range); !intersection(range).empty?; end
+ def intersection(range); NSIntersectionRange(self, range); end
+ def union(range); NSUnionRange(self, range); end
+ def max; location + length; end
+ def not_found?; location == NSNotFound; end
+ def inspect; "#<#{self.class} location=#{location}, length=#{length}>"; end
+end
Modified: MacRuby/trunk/test-macruby/rubycocoa_test.rb
===================================================================
--- MacRuby/trunk/test-macruby/rubycocoa_test.rb 2008-07-10 06:10:25 UTC (rev 349)
+++ MacRuby/trunk/test-macruby/rubycocoa_test.rb 2008-07-10 08:08:35 UTC (rev 350)
@@ -139,6 +139,10 @@
it "should not create MacRuby style method aliases for methods containing underscores if the arity doesn't match" do
@obj.respond_to?(:"method:notRubyCocoaStyle:").should.be false
end
+
+ it "should respond to to_ruby" do
+ @obj.respond_to?(:to_ruby).should.be true
+ end
end
describe 'OSX module' do
@@ -176,4 +180,61 @@
e.message.should.include 'OSX::DOES_NOT_EXIST_IN_TOPLEVEL_OBJECT'
end
end
-end
\ No newline at end of file
+end
+
+describe 'NSUserDefaults additions' do
+ before do
+ @obj = NSUserDefaults.alloc.init
+ end
+
+ it "should respond to [] and []=" do
+ @obj.respond_to?(:[]).should.be true
+ @obj.respond_to?(:[]=).should.be true
+ @obj['key'] = 'value'
+ @obj['key'].should.equal 'value'
+ end
+end
+
+describe 'NSIndexSet additions' do
+ before do
+ @obj = NSIndexSet.alloc.init
+ end
+
+ it "should respond to to_a" do
+ @obj.respond_to?(:to_a).should.be true
+ end
+end
+
+describe 'NSNumber additions' do
+ before do
+ @i = NSNumber.numberWithInt(42)
+ @f = NSNumber.numberWithDouble(42.42)
+ end
+
+ it "should respond to to_i and to_f" do
+ @i.respond_to?(:to_i).should.be true
+ @i.respond_to?(:to_f).should.be true
+ @i.to_i.should.equal 42
+ @f.to_f.should.equal 42.42
+ end
+end
+
+describe 'NSDate additions' do
+ before do
+ @obj = NSDate.alloc.init
+ end
+
+ it "should respond to to_time" do
+ @obj.respond_to?(:to_time).should.be true
+ end
+end
+
+describe 'NSImage additions' do
+ before do
+ @obj = NSImage.alloc.init
+ end
+
+ it "should respond to focus" do
+ @obj.respond_to?(:focus).should.be true
+ end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080710/217687f0/attachment-0001.html
More information about the macruby-changes
mailing list