motion-resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +8 -0
  3. data/README.md +53 -0
  4. data/Rakefile +14 -0
  5. data/app/app_delegate.rb +7 -0
  6. data/examples/Colr/.gitignore +13 -0
  7. data/examples/Colr/Rakefile +10 -0
  8. data/examples/Colr/app/app_delegate.rb +10 -0
  9. data/examples/Colr/app/color.rb +15 -0
  10. data/examples/Colr/app/color_view_controller.rb +46 -0
  11. data/examples/Colr/app/initializer.rb +2 -0
  12. data/examples/Colr/app/main_navigation_controller.rb +6 -0
  13. data/examples/Colr/app/tag.rb +5 -0
  14. data/examples/Colr/app/tags_view_controller.rb +23 -0
  15. data/lib/motion-resource.rb +5 -0
  16. data/lib/motion-resource/associations.rb +94 -0
  17. data/lib/motion-resource/attributes.rb +37 -0
  18. data/lib/motion-resource/base.rb +51 -0
  19. data/lib/motion-resource/crud.rb +33 -0
  20. data/lib/motion-resource/find.rb +67 -0
  21. data/lib/motion-resource/requests.rb +64 -0
  22. data/lib/motion-resource/string.rb +23 -0
  23. data/lib/motion-resource/urls.rb +29 -0
  24. data/lib/motion-resource/version.rb +3 -0
  25. data/motion-resource.gemspec +20 -0
  26. data/spec/env.rb +41 -0
  27. data/spec/motion-resource/associations/belongs_to_spec.rb +116 -0
  28. data/spec/motion-resource/associations/has_many_spec.rb +128 -0
  29. data/spec/motion-resource/associations/has_one_spec.rb +69 -0
  30. data/spec/motion-resource/associations/scope_spec.rb +21 -0
  31. data/spec/motion-resource/attributes_spec.rb +64 -0
  32. data/spec/motion-resource/base_spec.rb +54 -0
  33. data/spec/motion-resource/crud_spec.rb +141 -0
  34. data/spec/motion-resource/find_spec.rb +90 -0
  35. data/spec/motion-resource/requests_spec.rb +119 -0
  36. data/spec/motion-resource/string_spec.rb +26 -0
  37. data/spec/motion-resource/urls_spec.rb +52 -0
  38. metadata +140 -0
@@ -0,0 +1,69 @@
1
+ describe "has_one" do
2
+ extend WebStub::SpecHelpers
3
+
4
+ it "should define reader" do
5
+ User.new.should.respond_to :profile
6
+ end
7
+
8
+ it "should define writer" do
9
+ User.new.should.respond_to :profile=
10
+ end
11
+
12
+ it "should define reset method" do
13
+ User.new.should.respond_to :reset_profile
14
+ end
15
+
16
+ it "should by default return nil" do
17
+ User.new.profile.should.be.nil
18
+ end
19
+
20
+ it "should reset" do
21
+ user = User.new
22
+ user.profile = Profile.new
23
+ user.reset_profile
24
+ user.profile.should.be.nil
25
+ end
26
+
27
+ describe "writer" do
28
+ it "should create model when assigned with hash" do
29
+ user = User.new
30
+ user.profile = { :id => 1, :name => 'John', :email => 'doe@example.com' }
31
+ user.profile.should.is_a Profile
32
+ end
33
+
34
+ it "should set attributes when assigned with hash" do
35
+ user = User.new
36
+ user.profile = { :id => 1, :name => 'John', :email => 'doe@example.com' }
37
+ user.profile.name.should == 'John'
38
+ user.profile.email.should == 'doe@example.com'
39
+ end
40
+
41
+ it "should use identity map when assigned with hash" do
42
+ profile = Profile.instantiate(:id => 10)
43
+ user = User.new
44
+ user.profile = { :id => 10 }
45
+ user.profile.should == profile
46
+ end
47
+
48
+ it "should act like a regular setter when assigned with object" do
49
+ profile = Profile.new
50
+ user = User.new
51
+ user.profile = profile
52
+ user.profile.should == profile
53
+ end
54
+ end
55
+
56
+ describe "piggybacking" do
57
+ it "should set association when returned with model" do
58
+ stub_request(:get, "http://example.com/users/1.json").to_return(json: { id: 1, profile: { id: 2, name: 'John' } })
59
+ User.find(1) do |result|
60
+ @result = result
61
+ resume
62
+ end
63
+
64
+ wait_max 1.0 do
65
+ @result.profile.name.should == 'John'
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,21 @@
1
+ describe "scope" do
2
+ extend WebStub::SpecHelpers
3
+
4
+ it "should define a custom url" do
5
+ Comment.should.respond_to :recent_url
6
+ end
7
+
8
+ it "should fetch collection" do
9
+ stub_request(:get, "http://example.com/comments/recent.json").to_return(json: [{ id: 1, text: 'Whats up?' }])
10
+
11
+ Comment.recent do |results|
12
+ @results = results
13
+ resume
14
+ end
15
+
16
+ wait_max 1.0 do
17
+ @results.size.should == 1
18
+ @results.first.text.should == 'Whats up?'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,64 @@
1
+ describe "attributes" do
2
+ it "should define reader per attribute" do
3
+ Shape.new.should.respond_to :contents
4
+ end
5
+
6
+ it "should define writer per attribute" do
7
+ Shape.new.should.respond_to :contents=
8
+ end
9
+
10
+ it "should duplicate array in setter" do
11
+ contents = ['hello']
12
+ shape = Shape.new
13
+ shape.contents = contents
14
+ shape.contents.object_id.should != contents.object_id
15
+ end
16
+
17
+ it "should duplicate hash in setter" do
18
+ contents = { :foo => 'bar' }
19
+ shape = Shape.new
20
+ shape.contents = contents
21
+ shape.contents.should.not.be.identical_to contents
22
+ end
23
+
24
+ it "should behave regularly for other values in setter" do
25
+ shape = Shape.new
26
+ shape.contents = "hello"
27
+ shape.contents.should == "hello"
28
+ end
29
+
30
+ it "should store attribute names in accessor" do
31
+ Shape.attributes.should.include :contents
32
+ end
33
+
34
+ it "should inherit attribute names from parent class" do
35
+ Rectangle.attributes.should.include :contents
36
+ Rectangle.attributes.should.include :size
37
+ end
38
+
39
+ it "should get hash of all attributes from instance" do
40
+ shape = Shape.new
41
+ shape.contents = "hello"
42
+ shape.attributes[:contents].should == "hello"
43
+ end
44
+
45
+ describe "update_attributes" do
46
+ it "should set attr_accessors" do
47
+ time = Time.now
48
+ shape = Shape.new
49
+ shape.update_attributes(:created_at => time)
50
+ shape.created_at.should == time
51
+ end
52
+
53
+ it "should set attributes" do
54
+ shape = Shape.new
55
+ shape.update_attributes(:position => "10,10")
56
+ shape.position.should == "10,10"
57
+ end
58
+
59
+ it "should not crash when updating an unknown attribute" do
60
+ shape = Shape.new
61
+ lambda { shape.update_attributes(:foo => 'bar') }.should.not.raise
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,54 @@
1
+ describe "base" do
2
+ it "should define the id method" do
3
+ Post.new.should.respond_to :id
4
+ end
5
+
6
+ it "should set new_record to true when calling new" do
7
+ Post.new.should.be.new_record
8
+ end
9
+
10
+ it "should set attributes when given a hash in new" do
11
+ post = Post.new(:text => 'hello')
12
+ post.text.should == 'hello'
13
+ end
14
+
15
+ describe "instantiate" do
16
+ it "should raise an exception if no ID is given" do
17
+ lambda { Shape.instantiate({}) }.should.raise(ArgumentError)
18
+ end
19
+
20
+ it "should instantiate concrete type if given in json" do
21
+ shape = Shape.instantiate(:id => 1, :type => 'Rectangle')
22
+ shape.should.is_a Rectangle
23
+ end
24
+
25
+ it "should instantiate base type if concrete type does not exist" do
26
+ shape = Shape.instantiate(:id => 2, :type => 'FuzzyCircle')
27
+ shape.should.is_a Shape
28
+ end
29
+
30
+ it "should instantiate base type if no type given in json" do
31
+ shape = Shape.instantiate(:id => 3)
32
+ shape.should.is_a Shape
33
+ shape.should.not.is_a Rectangle
34
+ end
35
+
36
+ it "should set new_record to false" do
37
+ shape = Shape.instantiate(:id => 4)
38
+ shape.should.not.be.new_record
39
+ end
40
+
41
+ it "should remember instance" do
42
+ Shape.recall(5).should.be.nil
43
+ shape = Shape.instantiate(:id => 5)
44
+ Shape.recall(5).should.not.be.nil
45
+ end
46
+
47
+ it "should update existing instance" do
48
+ shape1 = Shape.instantiate(:id => 6, :contents => 'nothing')
49
+ shape2 = Shape.instantiate(:id => 6, :contents => 'something')
50
+ shape1.should.be.identical_to shape2
51
+ shape1.contents.should == 'something'
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,141 @@
1
+ describe "crud" do
2
+ extend WebStub::SpecHelpers
3
+
4
+ describe "create" do
5
+ it "should create on save if record is new" do
6
+ stub_request(:post, "http://example.com/comments.json").to_return(json: { id: 1 })
7
+ comment = Comment.new
8
+ comment.save do |result|
9
+ @result = result
10
+ resume
11
+ end
12
+
13
+ wait_max 1.0 do
14
+ @result.should.not.be.nil
15
+ end
16
+ end
17
+
18
+ it "should create with json in response" do
19
+ stub_request(:post, "http://example.com/comments.json").to_return(json: { id: 1 })
20
+ comment = Comment.new
21
+ comment.create do |result|
22
+ @result = result
23
+ resume
24
+ end
25
+
26
+ wait_max 1.0 do
27
+ @result.should.not.be.nil
28
+ end
29
+ end
30
+
31
+ it "should create without json in response" do
32
+ stub_request(:post, "http://example.com/comments.json").to_return(body: "")
33
+ comment = Comment.new
34
+ comment.create do |result|
35
+ @result = result
36
+ resume
37
+ end
38
+
39
+ wait_max 1.0 do
40
+ @result.should.be.nil
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "update" do
46
+ it "should update on save if record already exists" do
47
+ stub_request(:put, "http://example.com/comments/10.json").to_return(json: { id: 10 })
48
+ comment = Comment.instantiate(:id => 10)
49
+ comment.save do |result|
50
+ @result = result
51
+ resume
52
+ end
53
+
54
+ wait_max 1.0 do
55
+ @result.should.not.be.nil
56
+ end
57
+ end
58
+
59
+ it "should update with json in response" do
60
+ stub_request(:put, "http://example.com/comments/10.json").to_return(json: { id: 10 })
61
+ comment = Comment.instantiate(:id => 10)
62
+ comment.update do |result|
63
+ @result = result
64
+ resume
65
+ end
66
+
67
+ wait_max 1.0 do
68
+ @result.should.not.be.nil
69
+ end
70
+ end
71
+
72
+ it "should update without json in response" do
73
+ stub_request(:put, "http://example.com/comments/10.json").to_return(body: "")
74
+ comment = Comment.instantiate(:id => 10)
75
+ comment.update do |result|
76
+ @result = result
77
+ resume
78
+ end
79
+
80
+ wait_max 1.0 do
81
+ @result.should.be.nil
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "destroy" do
87
+ it "should destroy with json in response" do
88
+ stub_request(:delete, "http://example.com/comments/10.json").to_return(json: { id: 10 })
89
+ comment = Comment.instantiate(:id => 10)
90
+ comment.destroy do |result|
91
+ @result = result
92
+ resume
93
+ end
94
+
95
+ wait_max 1.0 do
96
+ @result.should.not.be.nil
97
+ end
98
+ end
99
+
100
+ it "should destroy without json in response" do
101
+ stub_request(:delete, "http://example.com/comments/10.json").to_return(body: "")
102
+ comment = Comment.instantiate(:id => 10)
103
+ comment.destroy do |result|
104
+ @result = result
105
+ resume
106
+ end
107
+
108
+ wait_max 1.0 do
109
+ @result.should.be.nil
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "reload" do
115
+ it "should reload with json in response" do
116
+ stub_request(:get, "http://example.com/comments/10.json").to_return(json: { id: 10 })
117
+ comment = Comment.instantiate(:id => 10)
118
+ comment.reload do |result|
119
+ @result = result
120
+ resume
121
+ end
122
+
123
+ wait_max 1.0 do
124
+ @result.should.not.be.nil
125
+ end
126
+ end
127
+
128
+ it "should reload without json in response" do
129
+ stub_request(:get, "http://example.com/comments/10.json").to_return(body: "")
130
+ comment = Comment.instantiate(:id => 10)
131
+ comment.reload do |result|
132
+ @result = result
133
+ resume
134
+ end
135
+
136
+ wait_max 1.0 do
137
+ @result.should.be.nil
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,90 @@
1
+ describe "find" do
2
+ extend WebStub::SpecHelpers
3
+
4
+ it "should find single record" do
5
+ stub_request(:get, "http://example.com/comments/10.json").to_return(json: { id: 10 })
6
+ Comment.find(10) do |result|
7
+ @result = result
8
+ resume
9
+ end
10
+
11
+ wait_max 1.0 do
12
+ @result.should.is_a Comment
13
+ end
14
+ end
15
+
16
+ it "should find collection" do
17
+ stub_request(:get, "http://example.com/comments.json").to_return(json: [{ id: 10 }])
18
+ Comment.find_all do |result|
19
+ @result = result
20
+ resume
21
+ end
22
+
23
+ wait_max 1.0 do
24
+ @result.should.is_a Array
25
+ @result.first.should.is_a Comment
26
+ end
27
+ end
28
+
29
+ it "should fetch member with custom URL" do
30
+ stub_request(:get, "http://example.com/foo.json").to_return(json: { id: 10 })
31
+ Comment.fetch_member("foo") do |result|
32
+ @result = result
33
+ resume
34
+ end
35
+
36
+ wait_max 1.0 do
37
+ @result.should.is_a Comment
38
+ end
39
+ end
40
+
41
+ it "should give nil object if fetching single member fails" do
42
+ stub_request(:get, "http://example.com/foo.json").to_return(status_code: 404)
43
+ Comment.fetch_member("foo") do |result|
44
+ @result = result
45
+ resume
46
+ end
47
+
48
+ wait_max 1.0 do
49
+ @result.should.be.nil
50
+ end
51
+ end
52
+
53
+ it "should fetch collection with custom URL" do
54
+ stub_request(:get, "http://example.com/bar.json").to_return(json: [{ id: 10 }])
55
+ Comment.fetch_collection("bar") do |result|
56
+ @result = result
57
+ resume
58
+ end
59
+
60
+ wait_max 1.0 do
61
+ @result.should.is_a Array
62
+ @result.first.should.is_a Comment
63
+ end
64
+ end
65
+
66
+ it "should allow hash JSON response with model name as root" do
67
+ stub_request(:get, "http://example.com/bar.json").to_return(json: { comments: [{ id: 10 }] })
68
+ Comment.fetch_collection("bar") do |result|
69
+ @result = result
70
+ resume
71
+ end
72
+
73
+ wait_max 1.0 do
74
+ @result.should.is_a Array
75
+ @result.first.should.is_a Comment
76
+ end
77
+ end
78
+
79
+ it "should give nil object if fetching collection fails" do
80
+ stub_request(:get, "http://example.com/bar.json").to_return(status_code: 404)
81
+ Comment.fetch_collection("bar") do |result|
82
+ @result = result
83
+ resume
84
+ end
85
+
86
+ wait_max 1.0 do
87
+ @result.should.be.nil
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,119 @@
1
+ describe "requests" do
2
+ extend WebStub::SpecHelpers
3
+
4
+ it "should define GET method on instance and class" do
5
+ Comment.new.should.respond_to :get
6
+ Comment.should.respond_to :get
7
+ end
8
+
9
+ it "should define POST method on instance and class" do
10
+ Comment.new.should.respond_to :post
11
+ Comment.should.respond_to :post
12
+ end
13
+
14
+ it "should define PUT method on instance and class" do
15
+ Comment.new.should.respond_to :put
16
+ Comment.should.respond_to :put
17
+ end
18
+
19
+ it "should define DELETE method on instance and class" do
20
+ Comment.new.should.respond_to :delete
21
+ Comment.should.respond_to :delete
22
+ end
23
+
24
+ it "should add query string" do
25
+ stub_request(:get, "http://example.com/comments/10.json?foo=bar").to_return(json: { id: 10 })
26
+ Comment.new.get("comments/10", :query => { :foo => 'bar' }) do |response, json|
27
+ @result = json
28
+ resume
29
+ end
30
+
31
+ wait_max 1.0 do
32
+ @result.should.not.be.nil
33
+ end
34
+ end
35
+
36
+ it "should parse JSON in response" do
37
+ stub_request(:get, "http://example.com/comments/10.json").to_return(json: { id: 10, foo: 'bar' })
38
+ Comment.new.get("comments/10") do |response, json|
39
+ @result = json
40
+ resume
41
+ end
42
+
43
+ wait_max 1.0 do
44
+ @result.should == { "id" => 10, "foo" => "bar" }
45
+ end
46
+ end
47
+
48
+ it "should yield empty hash if response is blank" do
49
+ stub_request(:get, "http://example.com/comments/10.json").to_return(body: "")
50
+ Comment.new.get("comments/10") do |response, json|
51
+ @result = json
52
+ resume
53
+ end
54
+
55
+ wait_max 1.0 do
56
+ @result.should == {}
57
+ end
58
+ end
59
+
60
+ it "should yield nil if response is not ok" do
61
+ stub_request(:get, "http://example.com/comments/10.json").to_return(status_code: 404)
62
+ Comment.new.get("comments/10") do |response, json|
63
+ @result = json
64
+ resume
65
+ end
66
+
67
+ wait_max 1.0 do
68
+ @result.should.be.nil
69
+ end
70
+ end
71
+
72
+ it "should get attributes" do
73
+ stub_request(:get, "http://example.com/comments/10.json").to_return(json: { id: 10, text: "Hello" })
74
+ Comment.new.get("comments/10") do |response, json|
75
+ @result = json
76
+ resume
77
+ end
78
+
79
+ wait_max 1.0 do
80
+ @result["text"].should == "Hello"
81
+ end
82
+ end
83
+
84
+ it "should post" do
85
+ stub_request(:post, "http://example.com/comments.json").to_return(json: { id: 10 })
86
+ Comment.post("comments") do |response, json|
87
+ @result = json
88
+ resume
89
+ end
90
+
91
+ wait_max 1.0 do
92
+ @result.should.not.be.nil
93
+ end
94
+ end
95
+
96
+ it "should put" do
97
+ stub_request(:put, "http://example.com/comments/10.json").to_return(json: { id: 10 })
98
+ Comment.new.put("comments/10") do |response, json|
99
+ @result = json
100
+ resume
101
+ end
102
+
103
+ wait_max 1.0 do
104
+ @result.should.not.be.nil
105
+ end
106
+ end
107
+
108
+ it "should delete" do
109
+ stub_request(:delete, "http://example.com/comments/10.json").to_return(json: { id: 10 })
110
+ Comment.new.delete("comments/10") do |response, json|
111
+ @result = json
112
+ resume
113
+ end
114
+
115
+ wait_max 1.0 do
116
+ @result.should.not.be.nil
117
+ end
118
+ end
119
+ end