morpheus 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/.autotest +12 -0
  2. data/.rspec +1 -0
  3. data/README.md +77 -0
  4. data/Rakefile +16 -0
  5. data/lib/morpheus.rb +2 -2
  6. data/lib/morpheus/base.rb +1 -1
  7. data/lib/morpheus/client/inflections.rb +1 -1
  8. data/lib/morpheus/configuration.rb +41 -43
  9. data/lib/morpheus/errors.rb +1 -1
  10. data/lib/morpheus/mixins/associations.rb +3 -1
  11. data/lib/morpheus/mixins/attributes.rb +66 -67
  12. data/lib/morpheus/mixins/conversion.rb +9 -13
  13. data/lib/morpheus/mixins/filtering.rb +4 -1
  14. data/lib/morpheus/mixins/finders.rb +4 -1
  15. data/lib/morpheus/mixins/introspection.rb +12 -16
  16. data/lib/morpheus/mixins/persistence.rb +25 -26
  17. data/lib/morpheus/mixins/reflections.rb +4 -1
  18. data/lib/morpheus/mixins/request_handling.rb +4 -1
  19. data/lib/morpheus/mixins/response_parsing.rb +12 -12
  20. data/lib/morpheus/mixins/url_support.rb +4 -1
  21. data/lib/morpheus/request.rb +34 -34
  22. data/lib/morpheus/response.rb +2 -2
  23. data/lib/morpheus/response_parser.rb +9 -4
  24. data/lib/morpheus/version.rb +1 -1
  25. data/morpheus.gemspec +4 -4
  26. data/spec/dummy/app/resources/author.rb +0 -1
  27. data/spec/dummy/app/resources/book.rb +0 -1
  28. data/spec/dummy/app/resources/dog.rb +3 -3
  29. data/spec/dummy/app/resources/meeting.rb +1 -2
  30. data/spec/dummy/config/application.rb +7 -36
  31. data/spec/dummy/config/environments/production.rb +1 -1
  32. data/spec/dummy/config/initializers/morpheus.rb +1 -1
  33. data/spec/dummy/config/locales/en.yml +1 -1
  34. data/spec/dummy/config/routes.rb +0 -56
  35. data/spec/morpheus/associations/association_spec.rb +51 -33
  36. data/spec/morpheus/associations/belongs_to_association_spec.rb +14 -2
  37. data/spec/morpheus/associations/has_many_association_spec.rb +31 -11
  38. data/spec/morpheus/associations/has_one_association_spec.rb +14 -2
  39. data/spec/morpheus/base_spec.rb +104 -100
  40. data/spec/morpheus/client/associations_spec.rb +43 -36
  41. data/spec/morpheus/client/log_subscriber_spec.rb +33 -0
  42. data/spec/morpheus/client/railtie_spec.rb +5 -0
  43. data/spec/morpheus/configuration_spec.rb +92 -113
  44. data/spec/morpheus/mixins/associations_spec.rb +90 -108
  45. data/spec/morpheus/mixins/attributes_spec.rb +71 -77
  46. data/spec/morpheus/mixins/conversion_spec.rb +49 -59
  47. data/spec/morpheus/mixins/filtering_spec.rb +13 -0
  48. data/spec/morpheus/mixins/finders_spec.rb +180 -217
  49. data/spec/morpheus/mixins/introspection_spec.rb +81 -124
  50. data/spec/morpheus/mixins/persistence_spec.rb +140 -133
  51. data/spec/morpheus/mixins/{reflection_spec.rb → reflections_spec.rb} +28 -28
  52. data/spec/morpheus/mixins/request_handling_spec.rb +21 -0
  53. data/spec/morpheus/mixins/response_parsing_spec.rb +10 -2
  54. data/spec/morpheus/mixins/url_support_spec.rb +29 -0
  55. data/spec/morpheus/reflection_spec.rb +21 -0
  56. data/spec/morpheus/relation_spec.rb +34 -58
  57. data/spec/morpheus/request_cache_spec.rb +33 -2
  58. data/spec/morpheus/request_queue_spec.rb +37 -0
  59. data/spec/morpheus/request_spec.rb +102 -1
  60. data/spec/morpheus/response_parser_spec.rb +17 -0
  61. data/spec/morpheus/response_spec.rb +55 -51
  62. data/spec/morpheus/type_caster_spec.rb +128 -118
  63. data/spec/morpheus/url_builder_spec.rb +41 -0
  64. data/spec/shared/active_model_lint_test.rb +2 -2
  65. data/spec/spec_helper.rb +7 -14
  66. data/spec/support/configuration.rb +3 -4
  67. metadata +32 -16
  68. data/README.rdoc +0 -44
  69. data/autotest/discover.rb +0 -7
  70. data/lib/morpheus/mock.rb +0 -66
  71. data/spec/morpheus/mock_spec.rb +0 -133
@@ -1,21 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Morpheus::Reflections, "ClassMethods" do
4
- context "when the resource has a belongs_to association" do
3
+ describe Morpheus::Reflections do
4
+
5
+ context 'when the resource has a belongs_to association' do
5
6
  before(:each) do
6
- @meeting = Meeting.new(:id => 1, :conference_id=>2, :conference => {:id=>2, :name=>'A conference', :notes=>'There be notes'})
7
+ @meeting = Meeting.new(:id => 1, :conference_id => 2, :conference => { :id => 2, :name => 'A conference', :notes => 'There be notes' })
7
8
  end
8
9
 
9
- it "returns the association with the correct type and all nested attributes" do
10
+ it 'returns the association with the correct type and all nested attributes' do
10
11
  @conference = @meeting.conference
11
12
  @conference.should be_a(Conference)
12
13
  @conference.id.should eql(2)
13
14
  @conference.name.should eql('A conference')
14
15
  @conference.notes.should eql('There be notes')
15
16
  end
16
-
17
- it "still allows setting the association the old-fashioned way" do
18
- @conference = Conference.new(:id => 3, :name=>'Different')
17
+
18
+ it 'still allows setting the association the old-fashioned way' do
19
+ @conference = Conference.new(:id => 3, :name => 'Different')
19
20
  @meeting.conference = @conference
20
21
  @meeting.conference_id.should eql(3)
21
22
  @conference = @meeting.conference
@@ -23,9 +24,9 @@ describe Morpheus::Reflections, "ClassMethods" do
23
24
  @conference.id.should eql(3)
24
25
  @conference.name.should eql('Different')
25
26
  end
26
-
27
+
27
28
  it 'allows setting with actual class objects in an initial hash' do
28
- @conference = Conference.new(:id => 4, :name=>'Another')
29
+ @conference = Conference.new(:id => 4, :name => 'Another')
29
30
  @meeting2 = Meeting.new(:id => 2, :conference => @conference)
30
31
  @meeting2.conference_id.should eql(4)
31
32
  @conference = @meeting2.conference
@@ -34,30 +35,30 @@ describe Morpheus::Reflections, "ClassMethods" do
34
35
  @conference.name.should eql('Another')
35
36
  end
36
37
  end
37
-
38
- context "when the resource has a has_one association" do
38
+
39
+ context 'when the resource has a has_one association' do
39
40
  before(:each) do
40
- @meeting = Meeting.new(:id => 1, :speaker => {:id=>22, :name=>'Speaking'})
41
+ @meeting = Meeting.new(:id => 1, :speaker => { :id => 22, :name => 'Speaking' })
41
42
  end
42
43
 
43
- it "returns the association with the correct type and all nested attributes" do
44
+ it 'returns the association with the correct type and all nested attributes' do
44
45
  @speaker = @meeting.speaker
45
46
  @speaker.should be_a(Speaker)
46
47
  @speaker.id.should eql(22)
47
48
  @speaker.name.should eql('Speaking')
48
49
  end
49
-
50
- it "still allows setting the association the old-fashioned way" do
51
- @speaker = Speaker.new({:id=>30, :name=>'Speaking up'})
50
+
51
+ it 'still allows setting the association the old-fashioned way' do
52
+ @speaker = Speaker.new(:id => 30, :name => 'Speaking up')
52
53
  @meeting.speaker = @speaker
53
54
  @speaker = @meeting.speaker
54
55
  @speaker.should be_a(Speaker)
55
56
  @speaker.id.should eql(30)
56
57
  @speaker.name.should eql('Speaking up')
57
58
  end
58
-
59
+
59
60
  it 'allows setting with actual class objects in an initial hash' do
60
- @speaker = Speaker.new({:id=>40, :name=>'Speaking out'})
61
+ @speaker = Speaker.new(:id => 40, :name => 'Speaking out')
61
62
  @meeting2 = Meeting.new(:id => 3, :speaker => @speaker)
62
63
  @speaker = @meeting2.speaker
63
64
  @speaker.should be_a(Speaker)
@@ -65,16 +66,16 @@ describe Morpheus::Reflections, "ClassMethods" do
65
66
  @speaker.name.should eql('Speaking out')
66
67
  end
67
68
  end
68
-
69
- context "when the resource has a has_many association" do
69
+
70
+ context 'when the resource has a has_many association' do
70
71
  before(:each) do
71
- @meeting = Meeting.new(:id => 1, :attendees => [{:id=>1, :name=>'First'}, {:id=>2, :name=>'Second'}, {:id=>7, :name=>'Last'}])
72
+ @meeting = Meeting.new(:id => 1, :attendees => [{ :id => 1, :name => 'First' }, { :id => 2, :name => 'Second' }, { :id => 7, :name => 'Last' }])
72
73
  end
73
74
 
74
- it "returns the association with the correct type and all nested attributes" do
75
+ it 'returns the association with the correct type and all nested attributes' do
75
76
  @attendees = @meeting.attendees
76
77
  @attendees.size.should eql(3)
77
- @attendees.each {|a| a.should be_a(Attendee) }
78
+ @attendees.each { |a| a.should be_a(Attendee) }
78
79
  @attendees.first.id.should eql(1)
79
80
  @attendees.first.name.should eql('First')
80
81
  @attendees.second.id.should eql(2)
@@ -82,19 +83,18 @@ describe Morpheus::Reflections, "ClassMethods" do
82
83
  @attendees.last.id.should eql(7)
83
84
  @attendees.last.name.should eql('Last')
84
85
  end
85
-
86
+
86
87
  it 'allows setting with actual class objects in an initial hash' do
87
- @attendees = [Attendee.new(:id=>4,:name=>'Forth'), Attendee.new(:id=>5,:name=>'A fifth')]
88
+ @attendees = [Attendee.new(:id => 4,:name => 'Forth'), Attendee.new(:id => 5, :name => 'A fifth')]
88
89
  @meeting2 = Meeting.new(:id => 3, :attendees => @attendees)
89
90
  @attendees = @meeting2.attendees
90
91
  @attendees.size.should eql(2)
91
- @attendees.each {|a| a.should be_a(Attendee) }
92
+ @attendees.each { |a| a.should be_a(Attendee) }
92
93
  @attendees.first.id.should eql(4)
93
94
  @attendees.first.name.should eql('Forth')
94
95
  @attendees.second.id.should eql(5)
95
96
  @attendees.second.name.should eql('A fifth')
96
97
  end
97
98
  end
98
-
99
-
99
+
100
100
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morpheus::RequestHandling do
4
+
5
+ describe '.get' do
6
+ pending
7
+ end
8
+
9
+ describe '.post' do
10
+ pending
11
+ end
12
+
13
+ describe '.put' do
14
+ pending
15
+ end
16
+
17
+ describe '.delete' do
18
+ pending
19
+ end
20
+
21
+ end
@@ -1,5 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Morpheus::ResponseParsing, "#build_from_response" do
4
- pending
3
+ describe Morpheus::ResponseParsing do
4
+
5
+ describe '.response_from_request' do
6
+ pending
7
+ end
8
+
9
+ describe '#build_from_response' do
10
+ pending
11
+ end
12
+
5
13
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morpheus::UrlSupport do
4
+
5
+ describe '.url_name' do
6
+ pending
7
+ end
8
+
9
+ describe '.plural_url_name' do
10
+ pending
11
+ end
12
+
13
+ describe '.set_base_url' do
14
+ pending
15
+ end
16
+
17
+ describe '.attributes_root' do
18
+ pending
19
+ end
20
+
21
+ describe '.set_attributes_root' do
22
+ pending
23
+ end
24
+
25
+ describe 'set_base_model_name' do
26
+ pending
27
+ end
28
+
29
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morpheus::Reflection do
4
+
5
+ describe '#initialize' do
6
+ pending
7
+ end
8
+
9
+ describe '#klass' do
10
+ pending
11
+ end
12
+
13
+ describe '#class_name' do
14
+ pending
15
+ end
16
+
17
+ describe '#build_association' do
18
+ pending
19
+ end
20
+
21
+ end
@@ -1,71 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Morpheus::Relation, ".where" do
4
-
5
- before(:each) do
6
- @dogs = [
7
- Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
8
- Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
9
- Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
10
- ]
11
- end
12
-
13
- it "makes a request to find all of the given records, with the where parameters" do
14
- Dog.should_receive(:get).with("/dogs", { :name => "Daisy", :breed => "English Bulldog" }).and_return(@dogs)
15
- Dog.where(:name => "Daisy", :breed => "English Bulldog").all
3
+ describe Morpheus::Relation do
4
+
5
+ describe '#where' do
6
+ it 'makes a request to find all of the given records, with the where parameters' do
7
+ Dog.should_receive(:get).with('/dogs', { :name => 'Daisy', :breed => 'English Bulldog' })
8
+ Dog.where(:name => 'Daisy', :breed => 'English Bulldog').all
9
+ end
16
10
  end
17
-
18
- end
19
11
 
20
- describe Morpheus::Relation, ".limit" do
21
-
22
- before(:each) do
23
- @dogs = [
24
- Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
25
- Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
26
- Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
27
- ]
12
+ describe '#limit' do
13
+ it 'makes a request to find all of the given records, with the limit parameter' do
14
+ Dog.should_receive(:get).with('/dogs', { :limit => 2 })
15
+ Dog.limit(2).all
16
+ end
28
17
  end
29
-
30
- it "makes a request to find all of the given records, with the limit parameter" do
31
- Dog.should_receive(:get).with("/dogs", { :limit => 2 }).and_return(@dogs)
32
- Dog.limit(2).all
18
+
19
+ describe '#page' do
20
+ it 'makes a request to find all of the given records, with the chained parameters' do
21
+ Dog.results_per_page = 10
22
+ Dog.should_receive(:get).with('/dogs', { :limit => 10, :offset => 20 })
23
+ Dog.page(3).all
24
+ end
33
25
  end
34
-
35
- end
36
26
 
37
- describe Morpheus::Relation, ".page" do
38
-
39
- before(:each) do
40
- @dogs = [
41
- Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
42
- Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
43
- Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
44
- ]
27
+ describe '#all' do
28
+ pending
45
29
  end
46
-
47
- it "makes a request to find all of the given records, with the chained parameters" do
48
- Dog.results_per_page = 10
49
- Dog.should_receive(:get).with("/dogs", { :limit => 10, :offset => 20 }).and_return(@dogs)
50
- Dog.page(3).all
30
+
31
+ describe '#to_a' do
32
+ pending
51
33
  end
52
-
53
- end
54
34
 
55
- describe Morpheus::Relation, "chaining relations" do
56
-
57
- before(:each) do
58
- @dogs = [
59
- Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
60
- Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
61
- Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
62
- ]
35
+ describe '#to_json' do
36
+ pending
63
37
  end
64
-
65
- it "makes a request to find all of the given records, with the chained parameters" do
66
- Dog.results_per_page = 10
67
- Dog.should_receive(:get).with("/dogs", { :name => "Daisy", :breed => "English Bulldog", :limit => 2, :offset => 20 }).and_return(@dogs)
68
- Dog.where(:name => "Daisy", :breed => "English Bulldog").page(3).limit(2).all
38
+
39
+ describe 'chaining relations' do
40
+ it 'makes a request to find all of the given records, with the chained parameters' do
41
+ Dog.results_per_page = 10
42
+ Dog.should_receive(:get).with('/dogs', { :name => 'Daisy', :breed => 'English Bulldog', :limit => 2, :offset => 20 })
43
+ Dog.where(:name => 'Daisy', :breed => 'English Bulldog').page(3).limit(2).all
44
+ end
69
45
  end
70
-
46
+
71
47
  end
@@ -1,5 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Morpheus::RequestCache, ".call" do
4
- pending
3
+ describe Morpheus::RequestCache do
4
+ let(:app) { mock(:call => nil) }
5
+ let(:cache) { Morpheus::RequestCache.new(app) }
6
+
7
+ describe '#initialize' do
8
+ it 'takes an app instance and sets its instance variable' do
9
+ obj = Object.new
10
+ cache = Morpheus::RequestCache.new(obj)
11
+ cache.instance_variable_get('@app').should eql(obj)
12
+ end
13
+ end
14
+
15
+ describe '#call' do
16
+ it 'clears the cache hash' do
17
+ Morpheus::RequestCache.cache.should_receive(:clear)
18
+ cache.call(Object.new)
19
+ end
20
+
21
+ it 'delegates to the app with the env' do
22
+ env = Object.new
23
+ app.should_receive(:call).with(env)
24
+ cache.call(env)
25
+ end
26
+ end
27
+
28
+ describe '.cache' do
29
+ it 'returns the global cache hash' do
30
+ cache_hash = Morpheus::RequestCache.cache
31
+ cache_hash.should be_kind_of(Hash)
32
+ cache_hash.should be_empty
33
+ end
34
+ end
35
+
5
36
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morpheus::RequestQueue do
4
+
5
+ describe '#initialize' do
6
+ pending
7
+ end
8
+
9
+ describe '#call' do
10
+ pending
11
+ end
12
+
13
+ describe '.enqueue' do
14
+ pending
15
+ end
16
+
17
+ describe '.queue' do
18
+ pending
19
+ end
20
+
21
+ describe '.uncached_queue' do
22
+ pending
23
+ end
24
+
25
+ describe '.hydra' do
26
+ pending
27
+ end
28
+
29
+ describe '.run!' do
30
+ pending
31
+ end
32
+
33
+ describe '.has_request?' do
34
+ pending
35
+ end
36
+
37
+ end
@@ -1,5 +1,106 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Morpheus::Request do
4
- pending
4
+ let(:request) do
5
+ Morpheus::Request.new('/path', :method => :get, :params => { :one => 1 })
6
+ end
7
+ let(:response) { Morpheus::Response.new }
8
+
9
+ describe '#initialize' do
10
+ pending
11
+ end
12
+
13
+ describe '#cache_key' do
14
+ it 'returns a hash of the array [method, url, params]' do
15
+ key = [request.method, request.url, request.params].hash
16
+ request.cache_key.should eql(key)
17
+ end
18
+ end
19
+
20
+ describe '#response=' do
21
+ it 'sets the response for this request' do
22
+ request.response = response
23
+ request.response.should eql(response)
24
+ end
25
+
26
+ it 'sets the response in the RequestCache' do
27
+ request.response = response
28
+ Morpheus::RequestCache.cache[request.cache_key].should eql(response)
29
+ end
30
+
31
+ it 'tags the response for caching' do
32
+ response.should_receive(:tag_for_caching!)
33
+ request.response = response
34
+ end
35
+ end
36
+
37
+ describe '#response' do
38
+ it 'checks to see if the request is in the queue' do
39
+ Morpheus::RequestQueue.should_receive(:has_request?).with(request)
40
+ request.response
41
+ end
42
+
43
+ context 'when the request is in the RequestQueue' do
44
+ before do
45
+ Morpheus::RequestQueue.stub(:has_request?).and_return(true)
46
+ end
47
+
48
+ it 'calls .run! on RequestQueue' do
49
+ Morpheus::RequestQueue.should_receive(:run!)
50
+ request.response
51
+ end
52
+ end
53
+
54
+ context 'when the request is not in the RequestQueue' do
55
+ before do
56
+ Morpheus::RequestQueue.stub(:has_request?).and_return(false)
57
+ end
58
+
59
+ it 'does not call .run! on RequestQueue' do
60
+ Morpheus::RequestQueue.should_not_receive(:run!)
61
+ request.response
62
+ end
63
+ end
64
+
65
+ context 'when the RequestCache has a cached response for this request' do
66
+ before do
67
+ Morpheus::RequestCache.cache[request.cache_key] = response
68
+ end
69
+
70
+ it 'returns the response from the cache' do
71
+ request.response.should eql(response)
72
+ end
73
+ end
74
+
75
+ context 'when the RequestCache does not have a cached response for this request' do
76
+ pending
77
+ end
78
+ end
79
+
80
+ describe '.enqueue' do
81
+ let(:method) { :get }
82
+ let(:path) { '/path' }
83
+ let(:params) { { :one => 1 } }
84
+
85
+ context 'when using method, path, and params' do
86
+ it 'creates a new Request object with the path, method, and params' do
87
+ Morpheus::Request.should_receive(:new).with(path, { :method => method, :params => params })
88
+ Morpheus::Request.enqueue(method, path, params)
89
+ end
90
+ end
91
+
92
+ context 'when only using method and path' do
93
+ it 'creates a new Request object with only the path and method' do
94
+ Morpheus::Request.should_receive(:new).with(path, { :method => method })
95
+ Morpheus::Request.enqueue(method, path, {})
96
+ end
97
+ end
98
+
99
+ it 'queues up the request on the RequestQueue' do
100
+ Morpheus::Request.stub(:new).and_return(request)
101
+ Morpheus::RequestQueue.should_receive(:enqueue).with(request)
102
+ Morpheus::Request.enqueue(method, path, params)
103
+ end
104
+ end
105
+
5
106
  end