cyrax 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NmE3M2RiMzA0YzZkOWJlNjI1MWZmM2I4ZDVkMjNjMzRiYTI1YjVmMw==
4
+ ZDE0YjZkNTZmYTNmNzI1YzQzODY2Nzg1OWI4MDY3NDZmMzY5N2Y1Yg==
5
5
  data.tar.gz: !binary |-
6
- YzM3YTgzM2Q2OTczMjVjNjgxYmQ3NzFkNTE2NjE5ZDBhNzQ4MDk2YQ==
6
+ MzlhNjI4YThhOTc5MWE3YzhkMjU2NDIzNmQ0YjU1YzExNDY1YjRmOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YTJhMTMyZTJiM2EzOWVjNGQ0NjEzZjUyNTBlYWIzOGU4NDYzNzRiZDM1NmEz
10
- YzMwNTZmMGFlMzE1NTVkZmE4NTc2NDQwNDg3N2M5MGVjZWVhYzQwMTEwNWJl
11
- ODJjODU2OWJhNmEyNjVkZjQzZjEzYmE4MDM1ZDc4YzI2MTNmNGI=
9
+ Njk3YmQyYzdlOWFiZWUxODM0NjNmZTdlOTgzNTVhMDFjNmI3N2I3ODc3MTk1
10
+ ODc2NDJjNzI3ZDg1OWNiMDVjZjlkOGFmMGFhNzA4Y2JlYTEwMjExYjgwODk3
11
+ ODMyYmNhMWIzODk2MTg2YzcyYzQwYTYzMjg0ZjFmYTlmYjgzNDM=
12
12
  data.tar.gz: !binary |-
13
- MjUyMmFmNzJkZDJkYWEwZWMzNGRiZWQwNDI3NmQwYWEyNzkwYjAzMjFlYjc1
14
- MWQ4ZDAyOTkxMzEwNTk1NjZjNDdlZGM3MWQ3MzVmNWY5NWUwMDdlNjUzMjU0
15
- NmRmNjUzOWM5OGZiYzlhM2Y4NzQyYWVmODdlZGEzZjU2ZDQzODE=
13
+ NDZjZTM1ZDNlNWM2NGFlODc4NmE0MzhmMWRiYmJkMDJiZDgzNmU1OTBiZjU3
14
+ ZDg1NGNjMDFkYTJiYjhiN2MzMjVmOTM1OGQ0MTA2NDBiYWVlZDAzNDE2ODQx
15
+ NjM4YzY1NWFlYWEzNDM3NWRjMWM0Y2QzNzBiMmQ3ZTBiNDQ4OGM=
@@ -7,11 +7,12 @@ module Cyrax::Extensions
7
7
  register_extension :has_resource
8
8
  class_attribute :resource_name
9
9
  class_attribute :resource_class_name
10
+ class_attribute :collection_name
10
11
  end
11
12
 
12
13
  def resource_class
13
- if self.class.resource_class_name
14
- self.class.resource_class_name.constantize
14
+ if self.resource_class_name
15
+ self.resource_class_name.constantize
15
16
  else
16
17
  resource_name.classify.constantize
17
18
  end
@@ -21,32 +22,10 @@ module Cyrax::Extensions
21
22
  resource_class
22
23
  end
23
24
 
24
- def collection_name
25
- resource_name.pluralize
26
- end
27
-
28
25
  def resource_attributes
29
26
  filter_attributes(dirty_resource_attributes)
30
27
  end
31
28
 
32
- def build_resource(id, attributes = {})
33
- if id.present?
34
- resource = find_resource(id)
35
- resource.attributes = attributes
36
- resource
37
- else
38
- resource_scope.new(default_resource_attributes.merge(attributes))
39
- end
40
- end
41
-
42
- def build_collection
43
- resource_scope
44
- end
45
-
46
- def find_resource(id)
47
- resource_scope.find(id)
48
- end
49
-
50
29
  def response_name
51
30
  resource_name
52
31
  end
@@ -64,6 +43,7 @@ module Cyrax::Extensions
64
43
  def resource(name, options = {})
65
44
  self.resource_name = name.to_s
66
45
  self.resource_class_name = options[:class_name]
46
+ self.collection_name = name.to_s.pluralize
67
47
  end
68
48
  end
69
49
 
@@ -19,7 +19,7 @@ module Cyrax::Extensions
19
19
  resource = build_resource(nil, custom_attributes||resource_attributes)
20
20
  invoke_callback(:before_create, resource)
21
21
  invoke_callback(:before_save, resource)
22
- if resource.save
22
+ if save_resource(resource)
23
23
  invoke_callback(:after_create, resource)
24
24
  invoke_callback(:after_save, resource)
25
25
  set_message("#{resource_name.titleize} successfully created")
@@ -39,7 +39,7 @@ module Cyrax::Extensions
39
39
  resource = build_resource(params[:id], custom_attributes||resource_attributes)
40
40
  invoke_callback(:before_update, resource)
41
41
  invoke_callback(:before_save, resource)
42
- if resource.save
42
+ if save_resource(resource)
43
43
  invoke_callback(:after_update, resource)
44
44
  invoke_callback(:after_save, resource)
45
45
  set_message("#{resource_name.titleize} successfully updated")
@@ -52,9 +52,35 @@ module Cyrax::Extensions
52
52
  def destroy
53
53
  resource = find_resource(params[:id])
54
54
  invoke_callback(:before_destroy, resource)
55
- resource.destroy
55
+ delete_resource(resource)
56
56
  invoke_callback(:after_destroy, resource)
57
57
  respond_with(resource)
58
58
  end
59
+
60
+ def find_resource(id)
61
+ resource_scope.find(id)
62
+ end
63
+
64
+ def build_resource(id, attributes = {})
65
+ if id.present?
66
+ resource = find_resource(id)
67
+ resource.attributes = attributes
68
+ resource
69
+ else
70
+ resource_scope.new(default_resource_attributes.merge(attributes))
71
+ end
72
+ end
73
+
74
+ def save_resource(resource)
75
+ resource.save
76
+ end
77
+
78
+ def delete_resource(resource)
79
+ resource.destroy
80
+ end
81
+
82
+ def build_collection
83
+ resource_scope
84
+ end
59
85
  end
60
86
  end
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -4,10 +4,6 @@ class Foo; end
4
4
  module Cyrax
5
5
  describe BaseResource do
6
6
  describe 'it should behave like resource' do
7
- context 'class methods' do
8
- subject { Cyrax::BaseResource }
9
- it{ should respond_to(:new) }
10
- end
11
7
  context 'instance methods' do
12
8
  subject { Cyrax::BaseResource.new }
13
9
  it{ should respond_to(:accessor) }
@@ -30,19 +26,19 @@ module Cyrax
30
26
  end
31
27
 
32
28
  subject { Cyrax::BaseResource.new }
33
- let(:resource) { mock.as_null_object }
34
- let(:collection) { [mock] }
29
+ let(:resource) { double.as_null_object }
30
+ let(:collection) { [double] }
35
31
  before do
36
- subject.stub!(:params).and_return({id:123})
37
- subject.stub!(:find_resource).and_return(resource)
38
- subject.stub!(:resource_name).and_return('foo')
32
+ subject.stub(:params).and_return({id:123})
33
+ subject.stub(:find_resource).and_return(resource)
34
+ subject.class.send :resource, :foo
39
35
  end
40
36
 
41
- describe '#collection' do
37
+ describe '#read_all' do
42
38
  it 'responds with decorated collection' do
43
39
  subject.should_receive(:build_collection).and_return(collection)
44
40
  subject.should_receive(:respond_with).with(collection, name: 'foos', present: :collection)
45
- subject.collection
41
+ subject.read_all
46
42
  end
47
43
  end
48
44
 
@@ -91,7 +87,7 @@ module Cyrax
91
87
 
92
88
  describe '#create' do
93
89
  let(:params) { {foo: 'bar'} }
94
- before { subject.stub!(:build_resource).and_return(resource) }
90
+ before { subject.stub(:build_resource).and_return(resource) }
95
91
  it 'responds with resource' do
96
92
  subject.should_receive(:build_resource).with(nil, params)
97
93
  subject.should_receive(:respond_with).with(resource)
@@ -99,7 +95,7 @@ module Cyrax
99
95
  end
100
96
 
101
97
  context 'when resource successfully saved' do
102
- before { resource.stub!(:save).and_return(true) }
98
+ before { resource.stub(:save).and_return(true) }
103
99
 
104
100
  it 'invokes callbacks' do
105
101
  subject.should_receive(:invoke_callback).with(:before_save, resource)
@@ -116,7 +112,7 @@ module Cyrax
116
112
  end
117
113
 
118
114
  context 'when resource could not be saved' do
119
- before { resource.stub!(:save).and_return(false) }
115
+ before { resource.stub(:save).and_return(false) }
120
116
 
121
117
  it 'invokes callbacks' do
122
118
  subject.should_receive(:invoke_callback).with(:before_save, resource)
@@ -135,7 +131,7 @@ module Cyrax
135
131
 
136
132
  describe '#update' do
137
133
  let(:params) { {foo: 'bar'} }
138
- before { subject.stub!(:build_resource).and_return(resource) }
134
+ before { subject.stub(:build_resource).and_return(resource) }
139
135
  it 'responds with resource' do
140
136
  subject.should_receive(:build_resource).with(123, params)
141
137
  subject.should_receive(:respond_with).with(resource)
@@ -143,7 +139,7 @@ module Cyrax
143
139
  end
144
140
 
145
141
  context 'when resource successfully saved' do
146
- before { resource.stub!(:save).and_return(true) }
142
+ before { resource.stub(:save).and_return(true) }
147
143
 
148
144
  it 'invokes callbacks' do
149
145
  subject.should_receive(:invoke_callback).with(:before_save, resource)
@@ -160,7 +156,7 @@ module Cyrax
160
156
  end
161
157
 
162
158
  context 'when resource could not be saved' do
163
- before { resource.stub!(:save).and_return(false) }
159
+ before { resource.stub(:save).and_return(false) }
164
160
 
165
161
  it 'invokes callbacks' do
166
162
  subject.should_receive(:invoke_callback).with(:before_save, resource)
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Cyrax
4
4
  describe Callbacks do
5
- subject { Cyrax::Callbacks.new(mock) }
5
+ subject { Cyrax::Callbacks.new(double) }
6
6
 
7
7
  it { should respond_to(:before_create) }
8
8
  it { should respond_to(:after_create) }
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Cyrax
4
4
  describe Decorator do
5
- let(:decorable) { mock(foo:'bar') }
5
+ let(:decorable) { double(foo:'bar') }
6
6
  subject { Cyrax::Decorator.new(decorable) }
7
7
 
8
8
  its(:resource) { should eq(decorable) }
@@ -10,17 +10,17 @@ module Cyrax
10
10
 
11
11
  describe '#invoke_callback' do
12
12
  context 'when callback handler class is not defined' do
13
- let(:name) { mock }
14
- let(:resource) { mock }
15
- before { subject.class.stub!(:resource_callbacks_handler_class) }
13
+ let(:name) { double }
14
+ let(:resource) { double }
15
+ before { subject.class.stub(:resource_callbacks_handler_class) }
16
16
  it { subject.invoke_callback(name, resource).should be_nil }
17
17
  end
18
18
 
19
19
  context 'when callback handler class is defined' do
20
20
  let(:name) { "foo" }
21
- let(:resource) { mock }
21
+ let(:resource) { double }
22
22
  let(:handle_class) { double.as_null_object }
23
- before { subject.class.stub!(:resource_callbacks_handler_class).and_return(handle_class) }
23
+ before { subject.class.stub(:resource_callbacks_handler_class).and_return(handle_class) }
24
24
 
25
25
  it 'initializes handle class instance' do
26
26
  handle_class.should_receive(:new).with(resource)
@@ -29,7 +29,7 @@ module Cyrax
29
29
 
30
30
  it 'calls corresponding method on instance' do
31
31
  instance = double.as_null_object
32
- handle_class.stub!(:new).and_return(instance)
32
+ handle_class.stub(:new).and_return(instance)
33
33
  instance.should_receive(:foo)
34
34
  subject.invoke_callback(name, resource)
35
35
  end
@@ -56,53 +56,13 @@ module Cyrax
56
56
  end
57
57
 
58
58
  describe '#resource_scope' do
59
- before { subject.stub!(:resource_class).and_return(Foo) }
59
+ before { subject.stub(:resource_class).and_return(Foo) }
60
60
  its(:resource_scope) { should eq(Foo) }
61
61
  end
62
62
 
63
- describe '#build_collection' do
64
- before { subject.stub!(:resource_class).and_return(Foo) }
65
- its(:build_collection) { should eq(Foo) }
66
- end
67
-
68
- describe '#find_resource' do
69
- let(:resource_scope) { mock }
70
- before { subject.stub!(:resource_scope).and_return(resource_scope) }
71
- it 'finds resource by id' do
72
- allow_message_expectations_on_nil
73
- resource_scope.should_receive(:find).with(123)
74
- subject.find_resource(123)
75
- end
76
- end
77
-
78
- describe '#build_resource' do
79
- context 'when id is nil' do
80
- let(:resource_scope) { mock }
81
- before { subject.stub!(:resource_scope).and_return(resource_scope) }
82
- it 'initializes new object' do
83
- allow_message_expectations_on_nil
84
- resource_scope.should_receive(:new).with({foo: 'bar'})
85
- subject.build_resource(nil, {foo: 'bar'})
86
- end
87
- end
88
- context 'when id is present' do
89
- let(:resource) { mock.as_null_object }
90
- it 'finds resource' do
91
- subject.should_receive(:find_resource).with(123).and_return(resource)
92
- subject.build_resource(123, {foo: 'bar'})
93
- end
94
-
95
- it 'assigns provided attributes' do
96
- subject.stub!(:find_resource).and_return(resource)
97
- resource.should_receive(:attributes=).with({foo: 'bar'})
98
- subject.build_resource(123, {foo: 'bar'})
99
- end
100
- end
101
- end
102
-
103
63
  describe '#resource_attributes' do
104
- let(:dirty_resource_attributes) { mock }
105
- before { subject.stub!(:dirty_resource_attributes).and_return(dirty_resource_attributes)}
64
+ let(:dirty_resource_attributes) { double }
65
+ before { subject.stub(:dirty_resource_attributes).and_return(dirty_resource_attributes)}
106
66
  it 'filters dirty attributes' do
107
67
  subject.should_receive(:filter_attributes).with(dirty_resource_attributes)
108
68
  subject.resource_attributes
@@ -114,15 +74,15 @@ module Cyrax
114
74
  describe '#dirty_resource_attributes' do
115
75
  context 'when params are present' do
116
76
  it 'should return from params by resource_name' do
117
- subject.stub!(:resource_name).and_return(:foo)
118
- subject.stub!(:params).and_return({foo: {bar: 'bazz'}})
77
+ subject.stub(:resource_name).and_return(:foo)
78
+ subject.stub(:params).and_return({foo: {bar: 'bazz'}})
119
79
  subject.send(:dirty_resource_attributes).should eq({bar: 'bazz'})
120
80
  end
121
81
  end
122
82
  context 'when there are no params' do
123
83
  it 'should return empty hash' do
124
- subject.stub!(:resource_name).and_return(:foo)
125
- subject.stub!(:params).and_return({})
84
+ subject.stub(:resource_name).and_return(:foo)
85
+ subject.stub(:params).and_return({})
126
86
  subject.send(:dirty_resource_attributes).should eq({})
127
87
  end
128
88
  end
@@ -8,8 +8,8 @@ module Cyrax
8
8
  describe Cyrax::Extensions::HasResponse do
9
9
  subject { BaseWithResponse.new }
10
10
  before do
11
- subject.stub!(:decorable?).and_return(false)
12
- subject.stub!(:decorator_class).and_return(nil)
11
+ subject.stub(:decorable?).and_return(false)
12
+ subject.stub(:decorator_class).and_return(nil)
13
13
  end
14
14
 
15
15
  describe '#set_message' do
@@ -40,8 +40,8 @@ module Cyrax
40
40
 
41
41
  describe '#add_errors_from' do
42
42
  let(:messages) { [[:foo, 'bar'], [:bar, 'bazz']]}
43
- let(:errors) { mock(messages: messages)}
44
- let(:model) { mock(errors: errors) }
43
+ let(:errors) { double(messages: messages)}
44
+ let(:model) { double(errors: errors) }
45
45
 
46
46
  it 'should add errors from model error messages' do
47
47
  subject.add_errors_from(model)
@@ -57,9 +57,9 @@ module Cyrax
57
57
  end
58
58
 
59
59
  describe '#respond_with' do
60
- before { subject.stub!(:response_name).and_return(:foo) }
60
+ before { subject.stub(:response_name).and_return(:foo) }
61
61
  it 'calls Cyrax::Response' do
62
- Cyrax::Response.should_receive(:new).with(:foo, 'bar').and_return(mock.as_null_object)
62
+ Cyrax::Response.should_receive(:new).with(:foo, 'bar').and_return(double.as_null_object)
63
63
  subject.respond_with('bar')
64
64
  end
65
65
 
@@ -68,7 +68,7 @@ module Cyrax
68
68
  end
69
69
 
70
70
  it 'should assign message, errors and additional assignments to response object' do
71
- response = mock
71
+ response = double
72
72
  Cyrax::Response.should_receive(:new).and_return(response)
73
73
  response.should_receive(:message=)
74
74
  response.should_receive(:errors=)
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ class StrongParameters < Hash
4
+ def permit(attrs)
5
+ self.slice(*attrs)
6
+ end
7
+ end
8
+
9
+ class Bar; end
10
+ class Foo; end
11
+
12
+ module Cyrax
13
+ class BaseWithService < Cyrax::Base
14
+ include Cyrax::Extensions::HasService
15
+ end
16
+
17
+ describe Cyrax::Extensions::HasResource do
18
+ subject { BaseWithService.new }
19
+
20
+ describe 'instance methods' do
21
+ describe '#build_collection' do
22
+ before { subject.stub(:resource_scope).and_return(Foo) }
23
+ its(:build_collection) { should eq(Foo) }
24
+ end
25
+
26
+ describe '#find_resource' do
27
+ let(:resource_scope) { double }
28
+ before { subject.stub(:resource_scope).and_return(resource_scope) }
29
+ it 'finds resource by id' do
30
+ allow_message_expectations_on_nil
31
+ resource_scope.should_receive(:find).with(123)
32
+ subject.find_resource(123)
33
+ end
34
+ end
35
+
36
+ describe '#build_resource' do
37
+ context 'when id is nil' do
38
+ let(:resource_scope) { double }
39
+ before { subject.stub(:resource_scope).and_return(resource_scope) }
40
+ before { subject.stub(:default_resource_attributes).and_return({}) }
41
+ it 'initializes new object' do
42
+ allow_message_expectations_on_nil
43
+ resource_scope.should_receive(:new).with({foo: 'bar'})
44
+ subject.build_resource(nil, {foo: 'bar'})
45
+ end
46
+ end
47
+ context 'when id is present' do
48
+ let(:resource) { double.as_null_object }
49
+ it 'finds resource' do
50
+ subject.should_receive(:find_resource).with(123).and_return(resource)
51
+ subject.build_resource(123, {foo: 'bar'})
52
+ end
53
+
54
+ it 'assigns provided attributes' do
55
+ subject.stub(:find_resource).and_return(resource)
56
+ resource.should_receive(:attributes=).with({foo: 'bar'})
57
+ subject.build_resource(123, {foo: 'bar'})
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-23 00:00:00.000000000 Z
11
+ date: 2013-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -97,6 +97,7 @@ files:
97
97
  - spec/cyrax/extensions/has_decorator_spec.rb
98
98
  - spec/cyrax/extensions/has_resource_spec.rb
99
99
  - spec/cyrax/extensions/has_response_spec.rb
100
+ - spec/cyrax/extensions/has_service_spec.rb
100
101
  - spec/cyrax/response_spec.rb
101
102
  - spec/spec_helper.rb
102
103
  homepage:
@@ -132,5 +133,6 @@ test_files:
132
133
  - spec/cyrax/extensions/has_decorator_spec.rb
133
134
  - spec/cyrax/extensions/has_resource_spec.rb
134
135
  - spec/cyrax/extensions/has_response_spec.rb
136
+ - spec/cyrax/extensions/has_service_spec.rb
135
137
  - spec/cyrax/response_spec.rb
136
138
  - spec/spec_helper.rb