Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle arrays properly

  • Loading branch information...
commit e9e44dc55e545dced865a8c0f2a0a55685dbb3e9 1 parent bda4be5
@dam5s authored
Showing with 56 additions and 36 deletions.
  1. +2 −2 app.rb
  2. +54 −34 spec.rb
View
4 app.rb
@@ -7,8 +7,8 @@ def to_oc
oc = JSON.pretty_generate(self, indent: ' ')
.gsub('{', '@{')
.gsub('[', '@[')
- .gsub(/:\s([0-9]+)/, ": @\\1")
- .gsub(/:\snull/, ": [NSNull null]")
+ .gsub(/(:\s|^\s*)([0-9]+)/, "\\1@\\2")
+ .gsub(/(:\s|^\s*)null/, "\\1[NSNull null]")
.gsub(/"([^"]+)"/, '@"\\1"')
end
end
View
88 spec.rb
@@ -2,54 +2,74 @@
require './app'
describe 'Object #to_oc' do
- it 'should handle hashes and strings' do
- {"foo" => "bar"}.to_oc.should == %Q|@{
+
+ describe 'values in hashes' do
+ it 'should handle hashes and strings' do
+ {"foo" => "bar"}.to_oc.should == %Q|@{
@"foo": @"bar"
}|
- end
+ end
- it 'should handle integers' do
- {"foo" => 12345}.to_oc.should == %Q|@{
- @"foo": @12345
+ it 'should handle numbers' do
+ {
+ "foo" => 12345,
+ "bar" => 123.45
+ }.to_oc.should == %Q|@{
+ @"foo": @12345,
+ @"bar": @123.45
}|
- end
+ end
- it 'should handle floats' do
- {"foo" => 123.45}.to_oc.should == %Q|@{
- @"foo": @123.45
+ it 'should handle null' do
+ {"foo" => nil}.to_oc.should == %Q|@{
+ @"foo": [NSNull null]
}|
- end
+ end
- it 'should handle hashes' do
- {"foo" => "bar", "food" => "baz"}.to_oc.should == %Q|@{
- @"foo": @"bar",
- @"food": @"baz"
+ it 'should handle dates and urls as strings' do
+ {
+ "foo" => "2012-12-31 00:05:15",
+ "bar" => "http://url.com"
+ }.to_oc.should == %Q|@{
+ @"foo": @"2012-12-31 00:05:15",
+ @"bar": @"http://url.com"
}|
+ end
end
- it 'should handle arrays' do
- ["foo", "bar", "baz"].to_oc.should == %Q|@[
+ describe 'values in arrays' do
+ it 'should handle strings' do
+ ["foo", "bar"].to_oc.should == %Q|@[
@"foo",
- @"bar",
- @"baz"
+ @"bar"
]|
- end
+ end
- it 'should handle null' do
- {"foo" => nil}.to_oc.should == %Q|@{
- @"foo": [NSNull null]
-}|
- end
+ it 'should handle numbers' do
+ [12, 34, 56.78].to_oc.should == %Q|@[
+ @12,
+ @34,
+ @56.78
+]|
+ end
- it 'should handle dates as strings' do
- {"foo" => "2012-12-31 00:05:15"}.to_oc.should == %Q|@{
- @"foo": @"2012-12-31 00:05:15"
-}|
- end
+ it 'should handle null' do
+ [nil, nil].to_oc.should == %Q|@[
+ [NSNull null],
+ [NSNull null]
+]|
+ end
- it 'should handle urls as strings' do
- {"foo" => "http://url.com"}.to_oc.should == %Q|@{
- @"foo": @"http://url.com"
-}|
+ it 'should handle dates and urls as strings' do
+ [
+ "2012-12-31 00:05:15",
+ "http://url.com"
+ ].to_oc.should == %Q|@[
+ @"2012-12-31 00:05:15",
+ @"http://url.com"
+]|
+ end
end
+
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.