Revision: 3967 http://trac.macosforge.org/projects/ruby/changeset/3967 Author: martinlagardette@apple.com Date: 2010-04-27 20:01:10 -0700 (Tue, 27 Apr 2010) Log Message: ----------- Make NSString, NSArray and NSDictionary yamlable Fixes the evil #666 Modified Paths: -------------- MacRuby/trunk/lib/yaml/rubytypes.rb MacRuby/trunk/spec/macruby/core/array_spec.rb MacRuby/trunk/spec/macruby/core/hash_spec.rb MacRuby/trunk/spec/macruby/core/string_spec.rb Modified: MacRuby/trunk/lib/yaml/rubytypes.rb =================================================================== --- MacRuby/trunk/lib/yaml/rubytypes.rb 2010-04-27 05:54:42 UTC (rev 3966) +++ MacRuby/trunk/lib/yaml/rubytypes.rb 2010-04-28 03:01:10 UTC (rev 3967) @@ -82,7 +82,7 @@ def taguri; "!ruby/struct:#{self.class.to_s.sub(/^Struct::/, '')}"; end end -class String +class NSString yaml_as "tag:yaml.org,2002:str" def to_yaml(output = nil) @@ -108,7 +108,7 @@ def taguri; "!ruby/exception:#{self.class}"; end end -class Array +class NSArray yaml_as "tag:yaml.org,2002:seq" def to_yaml(output = nil) @@ -120,7 +120,7 @@ end end -class Hash +class NSDictionary yaml_as "tag:yaml.org,2002:map" def to_yaml(output = nil) Modified: MacRuby/trunk/spec/macruby/core/array_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/core/array_spec.rb 2010-04-27 05:54:42 UTC (rev 3966) +++ MacRuby/trunk/spec/macruby/core/array_spec.rb 2010-04-28 03:01:10 UTC (rev 3967) @@ -61,4 +61,9 @@ a.size.should == 0 lambda { a << 123 }.should raise_error(RuntimeError) end + + it "can be transformed to yaml using #to_yaml" do + require 'yaml' + NSArray.arrayWithArray([1, 2, 42]).to_yaml.should == "--- \n- 1\n- 2\n- 42\n" + end end Modified: MacRuby/trunk/spec/macruby/core/hash_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/core/hash_spec.rb 2010-04-27 05:54:42 UTC (rev 3966) +++ MacRuby/trunk/spec/macruby/core/hash_spec.rb 2010-04-28 03:01:10 UTC (rev 3967) @@ -47,6 +47,10 @@ end describe "An NSDictionary object" do + before(:all) do + require 'yaml' + end + it "is an instance of the Hash class" do a = NSDictionary.dictionary a.is_a?(Hash).should == true @@ -61,4 +65,15 @@ a.size.should == 0 lambda { a[42] = 123 }.should raise_error(RuntimeError) end + + it "can be transformed to yaml using #to_yaml" do + NSDictionary.dictionaryWithDictionary({:a => "ok", :c => 42}).to_yaml.should == "--- \n:a: ok\n:c: 42\n" + end + + it "can include Foundation objects and be correctly transformed to yaml" do + a = NSString.stringWithString("a") + ok = NSString.stringWithString("ok") + ary = NSArray.arrayWithArray([42, 21]) + NSDictionary.dictionaryWithDictionary({a => ok, :c => ary}).to_yaml.should == "--- \na: ok\n:c:\n- 42\n- 21\n" + end end Modified: MacRuby/trunk/spec/macruby/core/string_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/core/string_spec.rb 2010-04-27 05:54:42 UTC (rev 3966) +++ MacRuby/trunk/spec/macruby/core/string_spec.rb 2010-04-28 03:01:10 UTC (rev 3967) @@ -63,4 +63,9 @@ it "forwards the block when calling a ruby method" do NSString.stringWithString("ybuRcaM").sub(/.+/) { |s| s.reverse }.should == "MacRuby" end + + it "can be transformed to yaml using #to_yaml" do + require 'yaml' + NSString.stringWithString("ok").to_yaml.should == "--- ok\n" + end end