motion-support 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6bd52ad26c40a4b8f4a38e2e1f1dd86cb30b5a4
4
+ data.tar.gz: 278c0956ae07db17b8ac6cd905a85c98ecdd497f
5
+ SHA512:
6
+ metadata.gz: da57f98259563e1bcc7d3f1966a096149565f39ecd2dea875c3a8172334b9ddd84b6c82ded42a5837ebd435d4290b5c60384433b50249c2c6e1959336427649e
7
+ data.tar.gz: 76836035e618fc9cc54840e5313a1f763cf32aa861b54699d2b993120b692a0b35d4c88664dc384aa34b664b3cde045ec4ff5dc2c3fa9ca2e6135d02a6d7d421
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: objective-c
2
+ osx_image: xcode7.1
3
+ script: bundle exec rake spec
4
+ deploy:
5
+ provider: rubygems
6
+ api_key:
7
+ secure: BA4jK37Z5iul3szro9p1YP7MScgbQ9ITEiJD01FBjQGBPXWteomYD9NF0dV0aDk+RHAQEogoX5Ubkmt8MKlo/72nosMGR56n+IbNkmcdpsykg8Bb62e/ZuYpnu7OvK3M9cgeq+HskHsRPQWoDoM5GnkNHMfx7ecp1a1bJ8eP614=
8
+ gem: motion-support
9
+ on:
10
+ tags: true
11
+ repo: rubymotion/motion-support
File without changes
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # MotionSupport
2
+ [![Build Status](https://travis-ci.org/rubymotion/motion-support.svg?branch=master)](https://travis-ci.org/rubymotion/motion-support)
3
+ [![Gem Version](https://img.shields.io/gem/v/motion-support.svg)](https://rubygems.org/gems/motion-support)
4
+ [![MIT Licensed](https://img.shields.io/github/license/rubymotion/motion-support.svg)](https://github.com/rubymotion/motion-support/blob/master/LICENSE)
2
5
 
3
6
  This is a port of the parts of ActiveSupport that make sense for RubyMotion.
4
7
 
@@ -20,7 +23,7 @@ gem 'motion-support'
20
23
 
21
24
  # API docs
22
25
 
23
- [Are here](http://www.rubydoc.info/github/tkadauke/motion-support/master/frames).
26
+ [Are here](http://www.rubydoc.info/github/rubymotion/motion-support/master).
24
27
 
25
28
  # Partial loading
26
29
 
@@ -170,7 +173,7 @@ module Mod
170
173
  mattr_accessor :foo, :bar
171
174
  attr_internal :baz
172
175
  delegate :boo, :to => :foo
173
-
176
+
174
177
  remove_method :baz
175
178
  end
176
179
  ```
@@ -304,7 +307,6 @@ Specifically:
304
307
  * String inquiry methods are missing
305
308
  * String multibyte methods are missing
306
309
  * `String#html_safe` and `ERB` extensions are not needed in RubyMotion
307
- * `Object#to_json` and subclasses are missing
308
310
  * `Range#to_s(:db)` was removed
309
311
  * The `rfc822` time format was removed, since it relies on time zone support.
310
312
  * Extensions to `LoadError` and `NameError` were removed
@@ -317,7 +319,6 @@ Specifically:
317
319
  Things to do / to decide:
318
320
 
319
321
  * RubyMotion lacks a `Date` class. in `_stdlib` there is a stub of a Date class that makes the `Date` extensions work. This stub needs to be completed.
320
- * Implement `Object#to_json`, probably best if implemented on top of Cocoa APIs
321
322
  * Implement `Object#to_xml`
322
323
  * Implement `Hash#from_xml`
323
324
  * Do we need `Hash#extractable_options?`?
@@ -344,12 +345,6 @@ Things to do / to decide:
344
345
 
345
346
  ActiveSupport was originally written as part of Ruby on Rails by David Heinemeier Hansson. Over the years, countless contributors made many additions. They made this library possible.
346
347
 
347
- # License
348
-
349
- MotionSupport is released under the MIT license:
350
-
351
- <http://www.opensource.org/licenses/MIT>
352
-
353
348
  # Forking
354
349
 
355
350
  Feel free to fork and submit pull requests!
@@ -1,5 +1,3 @@
1
1
  require 'motion-require'
2
2
 
3
3
  Motion::Require.all(Dir.glob(File.expand_path('../../motion/**/*.rb', __FILE__)))
4
-
5
- at_exit { puts "\nWARNING: Automatic dependency detection does not work with motion-support. Turn it off in your Rakefile:\n\n app.detect_dependencies = false\n\n" if Motion::Project::App.config.detect_dependencies }
@@ -7,5 +7,8 @@ class NSDictionary
7
7
  end
8
8
  end
9
9
 
10
- delegate :symbolize_keys, :with_indifferent_access, :nested_under_indifferent_access, :to => :to_hash
10
+ delegate :symbolize_keys, :symbolize_keys!, :deep_symbolize_keys, :deep_symbolize_keys!,
11
+ :stringify_keys, :stringify_keys!, :deep_stringify_keys!, :deep_stringify_keys,
12
+ :deep_transform_keys, :deep_transform_keys!,
13
+ :with_indifferent_access, :nested_under_indifferent_access, :to => :to_hash
11
14
  end
@@ -40,10 +40,4 @@ class Numeric
40
40
  end
41
41
  end
42
42
  end
43
-
44
- # Stub method to return a pseudo-JSON value from a number. It just returns a string by calling to_s.
45
- # This should work most of the time.
46
- def to_json
47
- to_s
48
- end
49
43
  end
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ class Object
4
+ # Serializes the object to a hash then the hash using Cocoa's NSJSONSerialization
5
+ def to_json
6
+ attributes = {}
7
+
8
+ self.instance_variables.each do |attribute|
9
+ key = attribute.to_s.gsub('@', '')
10
+ attributes[key] = self.instance_variable_get(attribute)
11
+ end
12
+
13
+ attributes.to_json
14
+ end
15
+ end
16
+
17
+ class NilClass
18
+ # Returns 'null'.
19
+ def to_json
20
+ 'null'
21
+ end
22
+ end
23
+
24
+ class TrueClass
25
+ # Returns +self+ as string.
26
+ def to_json
27
+ self.to_s
28
+ end
29
+ end
30
+
31
+ class FalseClass
32
+ # Returns +self+ as string.
33
+ def to_json
34
+ self.to_s
35
+ end
36
+ end
37
+
38
+ class JSONString
39
+ # JSON escape characters
40
+ class << self
41
+ ESCAPED_CHARS = {
42
+ "\u0000" => '\u0000', "\u0001" => '\u0001',
43
+ "\u0002" => '\u0002', "\u0003" => '\u0003',
44
+ "\u0004" => '\u0004', "\u0005" => '\u0005',
45
+ "\u0006" => '\u0006', "\u0007" => '\u0007',
46
+ "\u0008" => '\b', "\u0009" => '\t',
47
+ "\u000A" => '\n', "\u000B" => '\u000B',
48
+ "\u000C" => '\f', "\u000D" => '\r',
49
+ "\u000E" => '\u000E', "\u000F" => '\u000F',
50
+ "\u0010" => '\u0010', "\u0011" => '\u0011',
51
+ "\u0012" => '\u0012', "\u0013" => '\u0013',
52
+ "\u0014" => '\u0014', "\u0015" => '\u0015',
53
+ "\u0016" => '\u0016', "\u0017" => '\u0017',
54
+ "\u0018" => '\u0018', "\u0019" => '\u0019',
55
+ "\u001A" => '\u001A', "\u001B" => '\u001B',
56
+ "\u001C" => '\u001C', "\u001D" => '\u001D',
57
+ "\u001E" => '\u001E', "\u001F" => '\u001F',
58
+ "\u2028" => '\u2028', "\u2029" => '\u2029',
59
+ '"' => '\"',
60
+ '\\' => '\\\\',
61
+ '>' => '\u003E',
62
+ '<' => '\u003C',
63
+ '&' => '\u0026'}
64
+
65
+ ESCAPE_REGEX = /[\u0000-\u001F\u2028\u2029"\\><&]/u
66
+
67
+ def escape(string)
68
+ %("#{string.gsub ESCAPE_REGEX, ESCAPED_CHARS}")
69
+ end
70
+ end
71
+ end
72
+
73
+ class String
74
+ # Returns JSON-escaped +self+.
75
+ def to_json
76
+ JSONString.escape self
77
+ end
78
+ end
79
+
80
+ class Symbol
81
+ # Returns +self+ as string.
82
+ def to_json
83
+ self.to_s
84
+ end
85
+ end
86
+
87
+ class Numeric
88
+ # Returns +self+.
89
+ def to_json
90
+ self
91
+ end
92
+ end
93
+
94
+ # For more complex objects (Array/Hash):
95
+ # Convert an object into a "JSON-ready" representation composed of
96
+ # primitives like Hash, Array, String, Numeric, and true/false/nil.
97
+ # Recursively calls #as_json to the object to recursively build a
98
+ # fully JSON-ready object.
99
+ #
100
+ # This allows developers to implement #as_json without having to
101
+ # worry about what base types of objects they are allowed to return
102
+ # or having to remember to call #as_json recursively.
103
+
104
+ class Array
105
+ def as_json
106
+ map { |v| (v.respond_to?(:as_json) ? v.as_json : v) }
107
+ end
108
+
109
+ # Calls <tt>as_json</tt> on all its elements and converts to a string.
110
+ def to_json
111
+ NSJSONSerialization.dataWithJSONObject(as_json, options: 0, error: nil).to_s
112
+ end
113
+ end
114
+
115
+ class Hash
116
+ # Ensure there are valid keys/values
117
+ def as_json
118
+ Hash[map { |k,v| [k.to_s, (v.respond_to?(:as_json) ? v.as_json : v)] }]
119
+ end
120
+
121
+ # Serializes the hash object using Cocoa's NSJSONSerialization
122
+ def to_json
123
+ # JSON keys must be strings, and any sub-objects also serialized
124
+ NSJSONSerialization.dataWithJSONObject(as_json, options: 0, error: nil).to_s
125
+ end
126
+ end
data/motion/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MotionSupport
2
- VERSION = "0.2.6"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,59 @@
1
+ describe ".to_json" do
2
+ describe "object" do
3
+ it "should serialize attributes" do
4
+ foo = Object.new
5
+ class << foo
6
+ attr_accessor :test
7
+ end
8
+ foo.test = 'bar'
9
+
10
+ foo.to_json.should == '{"test":"bar"}'
11
+ end
12
+ end
13
+
14
+ describe "nil" do
15
+ it "should return null" do
16
+ nil.to_json.should == 'null'
17
+ end
18
+ end
19
+
20
+ describe "true, false" do
21
+ it "should return self as string" do
22
+ true.to_json.should == 'true'
23
+ false.to_json.should == 'false'
24
+ end
25
+ end
26
+
27
+ describe "string" do
28
+ it "should escape the required characters" do
29
+ 'A string with special \ " &'.to_json.should == "\"A string with special \\\\ \\\" \\u0026\""
30
+ end
31
+ end
32
+
33
+ describe "numeric" do
34
+ it "should return self" do
35
+ 2.days.to_json.should == 172800
36
+ end
37
+ end
38
+
39
+ describe "array" do
40
+ it "should convert mixed type array" do
41
+ [true, false, nil, {:foo => :bar}, 'fizz', ''].to_json.should == '[true,false,null,{"foo":"bar"},"fizz",""]'
42
+ end
43
+ end
44
+
45
+ describe "hash" do
46
+ it "should convert a string hash" do
47
+ {}.to_json.should == '{}'
48
+ { :hello => :world }.to_json.should == '{"hello":"world"}'
49
+ { :hello => "world" }.to_json.should == '{"hello":"world"}'
50
+ { "hello" => 10 }.to_json.should == '{"hello":10}'
51
+ { "hello" => { "world" => "hi" } }.to_json.should == '{"hello":{"world":"hi"}}'
52
+ {:hello => "world", "say_bye" => true}.to_json.should == '{"hello":"world","say_bye":true}'
53
+ end
54
+
55
+ it "should convert a number hash" do
56
+ {10 => 20, 30 => 40, 50 => 60}.to_json.should == '{"10":20,"30":40,"50":60}'
57
+ end
58
+ end
59
+ end
@@ -3,30 +3,30 @@ describe "Duration" do
3
3
  it "should be a day" do
4
4
  MotionSupport::Duration.should === 1.day
5
5
  end
6
-
6
+
7
7
  it "should not be an int" do
8
8
  MotionSupport::Duration.should.not === 1.day.to_i
9
9
  end
10
-
10
+
11
11
  it "should not be a string" do
12
12
  MotionSupport::Duration.should.not === 'foo'
13
13
  end
14
14
  end
15
-
15
+
16
16
  describe "equals" do
17
17
  it "should equal itself" do
18
18
  1.day.should == 1.day
19
19
  end
20
-
20
+
21
21
  it "should equal integer representation" do
22
22
  1.day.should == 1.day.to_i
23
23
  end
24
-
24
+
25
25
  it "should not equal string" do
26
26
  1.day.should.not == 'foo'
27
27
  end
28
28
  end
29
-
29
+
30
30
  describe "inspect" do
31
31
  it "should convert to string representation" do
32
32
  0.seconds.inspect.should == '0 seconds'
@@ -39,7 +39,7 @@ describe "Duration" do
39
39
  1.fortnight.inspect.should == '14 days'
40
40
  end
41
41
  end
42
-
42
+
43
43
  describe "arithmetic" do
44
44
  it "should not break when subtracting time from itself" do
45
45
  lambda { Date.today - Date.today }.should.not.raise
@@ -52,45 +52,45 @@ describe "Duration" do
52
52
  def test_plus_with_time
53
53
  assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration"
54
54
  end
55
-
55
+
56
56
  end
57
-
57
+
58
58
  describe "fractions" do
59
59
  describe "days" do
60
60
  it "should support fractional days" do
61
61
  1.5.weeks.should == (86400 * 7) * 1.5
62
62
  1.7.weeks.should == (86400 * 7) * 1.7
63
63
  end
64
-
64
+
65
65
  it "should support since" do
66
66
  t = 2.years.ago
67
67
  1.5.days.since(t).should == 36.hours.since(t)
68
68
  end
69
-
69
+
70
70
  it "should support ago" do
71
71
  t = 2.years.ago
72
72
  1.5.days.ago(t).should == 36.hours.ago(t)
73
73
  end
74
74
  end
75
-
75
+
76
76
  describe "weeks" do
77
77
  it "should support fractional weeks" do
78
78
  1.5.days.should == 86400 * 1.5
79
79
  1.7.days.should == 86400 * 1.7
80
80
  end
81
-
81
+
82
82
  it "should support since" do
83
83
  t = 2.years.ago
84
84
  1.5.weeks.since(t).should == (7 * 36).hours.since(t)
85
85
  end
86
-
86
+
87
87
  it "should support ago" do
88
88
  t = 2.years.ago
89
89
  1.5.weeks.ago(t).should == (7 * 36).hours.ago(t)
90
90
  end
91
91
  end
92
92
  end
93
-
93
+
94
94
  describe "delegation" do
95
95
  it "should delegate with block" do
96
96
  counter = 0
@@ -98,10 +98,4 @@ describe "Duration" do
98
98
  counter.should == 60
99
99
  end
100
100
  end
101
-
102
- describe "to_json" do
103
- it "should convert to json" do
104
- 2.days.to_json.should == '172800'
105
- end
106
- end
107
101
  end
@@ -18,15 +18,51 @@ describe "NSDictionary" do
18
18
  @hash.values.should == ['bar']
19
19
  end
20
20
  end
21
-
22
- describe "symbolize_keys" do
23
- it "should work for NSDictionary instances" do
24
- dict = NSMutableDictionary.alloc.init
25
- dict.setValue('bar', forKey:'foo')
26
- dict.symbolize_keys.should == { :foo => 'bar' }
21
+
22
+ def describe_with_bang(method, &block)
23
+ bang_method = "#{method}!"
24
+
25
+ describe(method) do
26
+ it "should work for NSDictionary instances" do
27
+ block.call(method)
28
+ end
29
+ end
30
+
31
+ describe(bang_method) do
32
+ it "should work for NSDictionary instances" do
33
+ block.call(bang_method)
34
+ end
27
35
  end
28
36
  end
29
37
 
38
+ describe_with_bang "symbolize_keys" do |method|
39
+ dict = NSMutableDictionary.alloc.init
40
+ dict.setValue('bar', forKey:'foo')
41
+ dict.send(method).should == { :foo => 'bar' }
42
+ end
43
+
44
+ describe_with_bang "deep_symbolize_keys" do |method|
45
+ dict = NSMutableDictionary.alloc.init
46
+ innerDict = NSMutableDictionary.alloc.init
47
+ innerDict.setValue('foobar', forKey: 'bar')
48
+ dict.setValue(innerDict, forKey:'foo')
49
+ dict.send(method).should == {foo: {bar: 'foobar'}}
50
+ end
51
+
52
+ describe_with_bang "stringify_keys" do |method|
53
+ dict = NSMutableDictionary.alloc.init
54
+ dict.setValue('bar', forKey: :foo)
55
+ dict.send(method).should == {'foo' => 'bar'}
56
+ end
57
+
58
+ describe_with_bang "deep_stringify_keys" do |method|
59
+ dict = NSMutableDictionary.alloc.init
60
+ innerDict = NSMutableDictionary.alloc.init
61
+ innerDict.setValue('foobar', forKey: :bar)
62
+ dict.setValue(innerDict, forKey: :foo)
63
+ dict.send(method).should == {'foo' => {'bar' => 'foobar'}}
64
+ end
65
+
30
66
  describe "with_indifferent_access" do
31
67
  it "should work for NSDictionary instances" do
32
68
  dict = NSMutableDictionary.alloc.init
metadata CHANGED
@@ -1,54 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: motion-support
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.2.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Thomas Kadauke
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2013-12-28 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2015-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: motion-require
17
- requirement: &id001 !ruby/object:Gem::Requirement
18
- none: false
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
22
19
  version: 0.0.6
23
20
  type: :runtime
24
21
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.6
27
+ - !ruby/object:Gem::Dependency
27
28
  name: rake
28
- requirement: &id002 !ruby/object:Gem::Requirement
29
- none: false
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *id002
37
- description: Commonly useful extensions to the standard library for RubyMotion. Ported from ActiveSupport.
38
- email:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Commonly useful extensions to the standard library for RubyMotion. Ported
42
+ from ActiveSupport.
43
+ email:
39
44
  - thomas.kadauke@googlemail.com
40
45
  executables: []
41
-
42
46
  extensions: []
43
-
44
47
  extra_rdoc_files: []
45
-
46
- files:
48
+ files:
47
49
  - .gitignore
50
+ - .travis.yml
48
51
  - .yardopts
49
52
  - Gemfile
50
53
  - HACKS.md
51
- - MIT-LICENSE
54
+ - LICENSE
52
55
  - README.md
53
56
  - Rakefile
54
57
  - app/app_delegate.rb
@@ -129,6 +132,7 @@ files:
129
132
  - motion/core_ext/object/duplicable.rb
130
133
  - motion/core_ext/object/inclusion.rb
131
134
  - motion/core_ext/object/instance_variables.rb
135
+ - motion/core_ext/object/to_json.rb
132
136
  - motion/core_ext/object/to_param.rb
133
137
  - motion/core_ext/object/to_query.rb
134
138
  - motion/core_ext/object/try.rb
@@ -199,6 +203,7 @@ files:
199
203
  - spec/motion-support/core_ext/object/duplicable_spec.rb
200
204
  - spec/motion-support/core_ext/object/inclusion_spec.rb
201
205
  - spec/motion-support/core_ext/object/instance_variable_spec.rb
206
+ - spec/motion-support/core_ext/object/to_json_spec.rb
202
207
  - spec/motion-support/core_ext/object/to_param_spec.rb
203
208
  - spec/motion-support/core_ext/object/to_query_spec.rb
204
209
  - spec/motion-support/core_ext/object/try_spec.rb
@@ -226,38 +231,28 @@ files:
226
231
  - spec/motion-support/number_helper_spec.rb
227
232
  homepage: https://github.com/tkadauke/motion-support
228
233
  licenses: []
229
-
234
+ metadata: {}
230
235
  post_install_message:
231
236
  rdoc_options: []
232
-
233
- require_paths:
237
+ require_paths:
234
238
  - lib
235
- required_ruby_version: !ruby/object:Gem::Requirement
236
- none: false
237
- requirements:
238
- - - ">="
239
- - !ruby/object:Gem::Version
240
- hash: -1590752612782680441
241
- segments:
242
- - 0
243
- version: "0"
244
- required_rubygems_version: !ruby/object:Gem::Requirement
245
- none: false
246
- requirements:
247
- - - ">="
248
- - !ruby/object:Gem::Version
249
- hash: -1590752612782680441
250
- segments:
251
- - 0
252
- version: "0"
239
+ required_ruby_version: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - '>='
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ required_rubygems_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - '>='
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
253
249
  requirements: []
254
-
255
250
  rubyforge_project:
256
- rubygems_version: 1.8.19
251
+ rubygems_version: 2.0.14
257
252
  signing_key:
258
- specification_version: 3
253
+ specification_version: 4
259
254
  summary: Commonly useful extensions to the standard library for RubyMotion
260
- test_files:
255
+ test_files:
261
256
  - spec/motion-support/_helpers/constantize_test_cases.rb
262
257
  - spec/motion-support/_helpers/inflector_test_cases.rb
263
258
  - spec/motion-support/callback_spec.rb
@@ -302,6 +297,7 @@ test_files:
302
297
  - spec/motion-support/core_ext/object/duplicable_spec.rb
303
298
  - spec/motion-support/core_ext/object/inclusion_spec.rb
304
299
  - spec/motion-support/core_ext/object/instance_variable_spec.rb
300
+ - spec/motion-support/core_ext/object/to_json_spec.rb
305
301
  - spec/motion-support/core_ext/object/to_param_spec.rb
306
302
  - spec/motion-support/core_ext/object/to_query_spec.rb
307
303
  - spec/motion-support/core_ext/object/try_spec.rb