droid_services 0.0.1

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.
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ module DroidServices
4
+ describe Decorator do
5
+ let(:decorable) { mock(foo:'bar') }
6
+ subject { DroidServices::Decorator.new(decorable) }
7
+
8
+ its(:resource) { should eq(decorable) }
9
+
10
+ it 'should translate missing methods to decorable' do
11
+ subject.foo.should eq('bar')
12
+ end
13
+
14
+ it 'should not translate missing methods' do
15
+ expect {
16
+ subject.bar
17
+ }.to raise_error(NoMethodError)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ module DroidServices
4
+ describe DroidServices::Extensions::HasCallbacks do
5
+ include DroidServices::Extensions::HasCallbacks
6
+
7
+ describe '#invoke_callback' do
8
+ context 'when callback handler class is not defined' do
9
+ let(:name) { mock }
10
+ let(:resource) { mock }
11
+ before { self.class.stub!(:resource_callbacks_handler_class) }
12
+ it { invoke_callback(name, resource).should be_nil }
13
+ end
14
+
15
+ context 'when callback handler class is defined' do
16
+ let(:name) { "foo" }
17
+ let(:resource) { mock }
18
+ let(:handle_class) { double.as_null_object }
19
+ before { self.class.stub!(:resource_callbacks_handler_class).and_return(handle_class) }
20
+
21
+ it 'initializes handle class instance' do
22
+ handle_class.should_receive(:new).with(resource)
23
+ invoke_callback(name, resource)
24
+ end
25
+
26
+ it 'calls corresponding method on instance' do
27
+ instance = double.as_null_object
28
+ handle_class.stub!(:new).and_return(instance)
29
+ instance.should_receive(:foo)
30
+ invoke_callback(name, resource)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#callbacks_handler' do
36
+ it 'should define callbacks handler class' do
37
+ self.class.callbacks_handler('Foo')
38
+ self.class.resource_callbacks_handler_class.should eq('Foo')
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ class Foo;
4
+ attr_accessor :a
5
+ def initialize(arg)
6
+ @a = arg
7
+ end
8
+
9
+ def ==(other)
10
+ self.a == other.a
11
+ end
12
+ end
13
+
14
+ module DroidServices
15
+ describe DroidServices::Extensions::HasDecorator do
16
+ include DroidServices::Extensions::HasDecorator
17
+
18
+ describe 'class attributes' do
19
+ subject { self.class }
20
+ it { should respond_to(:decorator_class_name) }
21
+ end
22
+
23
+ describe 'class methods' do
24
+ describe '#decorator' do
25
+ before { self.class.decorator(:foo) }
26
+ subject { self }
27
+
28
+ its(:decorator_class_name) { should eq('foo') }
29
+ end
30
+ end
31
+
32
+ describe 'instance methods' do
33
+ subject { self }
34
+
35
+ describe '#decorator_class' do
36
+ before { self.class.decorator(:foo) }
37
+ its(:decorator_class) { should eq(Foo) }
38
+ end
39
+
40
+ describe '#decorable?' do
41
+ context 'when `decorator_class_name` present' do
42
+ before { self.class.decorator(:foo) }
43
+ its(:decorable?) { should be_true }
44
+ end
45
+
46
+ context 'when `decorator_class_name` empty' do
47
+ its(:decorable?) { should be_false }
48
+ end
49
+ end
50
+
51
+ describe '#decorated_collection' do
52
+ context 'when `decorable?`' do
53
+ before { subject.stub!(:decorable?).and_return(true) }
54
+ it 'should return decorated result' do
55
+ subject.should_receive(:build_decorated_collection)
56
+ subject.decorated_collection
57
+ end
58
+ end
59
+ context 'when not `decorable?`' do
60
+ before { subject.stub!(:decorable?).and_return(false) }
61
+ it 'should return raw result' do
62
+ subject.should_not_receive(:build_decorated_collection)
63
+ subject.should_receive(:build_collection)
64
+ subject.decorated_collection
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#build_decorated_collection' do
70
+ before do
71
+ self.class.decorator(:foo)
72
+ subject.stub!(:prepare_collection_for_decorate).and_return([:bar])
73
+ end
74
+
75
+ it 'returns array of decorator instances' do
76
+ subject.send(:build_decorated_collection).should eq([Foo.new(:bar)])
77
+ end
78
+ end
79
+
80
+ describe '#prepare_collection_for_decorate' do
81
+ subject { self.send(:prepare_collection_for_decorate) }
82
+
83
+ context 'when #build_collection returns array' do
84
+ before { self.stub!(:build_collection).and_return([:bar]) }
85
+ it { should eq([:bar]) }
86
+ end
87
+
88
+ context 'when #build_collection returns any object except array' do
89
+ before { self.stub!(:build_collection).and_return(:bar) }
90
+ it { should eq([:bar]) }
91
+ end
92
+
93
+ context 'when #build_collection returns any object which responds to #all method' do
94
+ let(:obj) { mock(:all => [:bar] ) }
95
+ before { self.stub!(:build_collection).and_return(obj) }
96
+
97
+ it { should eq([:bar]) }
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module DroidServices
4
+ describe DroidServices::Extensions::HasLogger do
5
+ include DroidServices::Extensions::HasLogger
6
+
7
+ before { self.class.stub!(:rails_log_path).and_return('./log') }
8
+
9
+ describe '#logger' do
10
+ it { logger.should be_kind_of(Yell::Logger) }
11
+ end
12
+
13
+ describe '#format_message' do
14
+ context 'when String' do
15
+ let(:message) { 'Some test' }
16
+ it { format_message(message).should be_kind_of(String) }
17
+ it { format_message(message).should eq('Some test')}
18
+ end
19
+
20
+ context 'when Hash' do
21
+ let(:message) { {'dog' => 'canine', 'cat' => 'feline', 12 => 'number', user: {nick: 'Guru', 'name' => 'User'}} }
22
+ it { format_message(message).should be_kind_of(String) }
23
+ it { format_message(message).should eq("{\n \"dog\": \"canine\",\n \"cat\": \"feline\",\n \"12\": \"number\",\n \"user\": {\n \"nick\": \"Guru\",\n \"name\": \"User\"\n }\n}") }
24
+ end
25
+
26
+ context 'when Array' do
27
+ let(:message) { ['dog', 'cat', 'number'] }
28
+ it { format_message(message).should be_kind_of(String) }
29
+ it { format_message(message).should eq("[\n \"dog\",\n \"cat\",\n \"number\"\n]") }
30
+ end
31
+
32
+ context 'when some object' do
33
+ let(:message) { mock }
34
+ it 'should inspect object' do
35
+ message.should_receive(:inspect)
36
+ format_message(message)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+
3
+ class Bar; end
4
+ class Foo; end
5
+
6
+ module DroidServices
7
+ describe DroidServices::Extensions::HasResource do
8
+ include DroidServices::Extensions::HasResource
9
+
10
+ describe 'class attributes' do
11
+ subject { self.class }
12
+ it { should respond_to(:resource_name) }
13
+ it { should respond_to(:resource_class_name) }
14
+ end
15
+
16
+ describe 'class methods' do
17
+ describe '#resource' do
18
+ before { self.class.resource(:foo, class_name:'bar', name:'bazz') }
19
+ subject { self }
20
+
21
+ its(:resource_name) { should eq('foo') }
22
+ its(:resource_class_name) { should eq('bar') }
23
+ end
24
+ end
25
+
26
+ describe 'instance methods' do
27
+ subject { self }
28
+
29
+ describe '#resource_name' do
30
+ before { self.class.resource(:foo) }
31
+ its(:resource_name) { should eq('foo') }
32
+ end
33
+
34
+ describe '#collection_name' do
35
+ before { self.class.resource(:foo) }
36
+ its(:collection_name) { should eq('foos') }
37
+ end
38
+
39
+ describe '#resource_class' do
40
+ context 'when `class_name` option is supplied' do
41
+ before { self.class.resource(:foo, class_name:'Bar') }
42
+ its(:resource_class) { should eq(Bar) }
43
+ end
44
+
45
+ context 'when `class_name` option is omited' do
46
+ before { self.class.resource(:foo) }
47
+ its(:resource_class) { should eq(Foo) }
48
+ end
49
+ end
50
+
51
+ describe '#resource_scope' do
52
+ before { subject.stub!(:resource_class).and_return(Foo) }
53
+ its(:resource_scope) { should eq(Foo) }
54
+ end
55
+
56
+ describe '#build_collection' do
57
+ before { subject.stub!(:resource_class).and_return(Foo) }
58
+ its(:build_collection) { should eq(Foo) }
59
+ end
60
+
61
+ describe '#find_resource' do
62
+ let(:resource_scope) { mock }
63
+ before { subject.stub!(:resource_scope).and_return(resource_scope) }
64
+ it 'finds resource by id' do
65
+ allow_message_expectations_on_nil
66
+ resource_scope.should_receive(:find).with(123)
67
+ subject.find_resource(123)
68
+ end
69
+ end
70
+
71
+ describe '#build_resource' do
72
+ context 'when id is nil' do
73
+ let(:resource_scope) { mock }
74
+ before { subject.stub!(:resource_scope).and_return(resource_scope) }
75
+ it 'initializes new object' do
76
+ allow_message_expectations_on_nil
77
+ resource_scope.should_receive(:new).with({foo: 'bar'})
78
+ subject.build_resource(nil, {foo: 'bar'})
79
+ end
80
+ end
81
+ context 'when id is present' do
82
+ let(:resource) { mock.as_null_object }
83
+ it 'finds resource' do
84
+ subject.should_receive(:find_resource).with(123).and_return(resource)
85
+ subject.build_resource(123, {foo: 'bar'})
86
+ end
87
+
88
+ it 'assigns provided attributes' do
89
+ subject.stub!(:find_resource).and_return(resource)
90
+ resource.should_receive(:attributes=).with({foo: 'bar'})
91
+ subject.build_resource(123, {foo: 'bar'})
92
+ end
93
+ end
94
+ end
95
+
96
+ describe '#resource_attributes' do
97
+ let(:dirty_resource_attributes) { mock }
98
+ before { subject.stub!(:dirty_resource_attributes).and_return(dirty_resource_attributes)}
99
+ it 'filters dirty attributes' do
100
+ subject.should_receive(:filter_attributes).with(dirty_resource_attributes)
101
+ subject.resource_attributes
102
+ end
103
+ end
104
+ end
105
+
106
+ describe 'private methods' do
107
+ subject { self }
108
+ describe '#dirty_resource_attributes' do
109
+ context 'when params are present' do
110
+ it 'should return from params by resource_name' do
111
+ subject.stub!(:resource_name).and_return(:foo)
112
+ subject.stub!(:params).and_return({foo: {bar: 'bazz'}})
113
+ subject.send(:dirty_resource_attributes).should eq({bar: 'bazz'})
114
+ end
115
+ end
116
+ context 'when there are no params' do
117
+ it 'should return empty hash' do
118
+ subject.stub!(:resource_name).and_return(:foo)
119
+ subject.stub!(:params).and_return({})
120
+ subject.send(:dirty_resource_attributes).should eq({})
121
+ end
122
+ end
123
+ end
124
+ describe '#default_resource_attributes' do
125
+ it 'should return empty hash by default' do
126
+ subject.send(:default_resource_attributes).should eq({})
127
+ end
128
+ end
129
+
130
+ describe '#filter_attributes' do
131
+ it 'should return supplied attributes by default' do
132
+ subject.send(:filter_attributes, {foo: 'bar'}).should eq({foo: 'bar'})
133
+ end
134
+ end
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ module DroidServices
4
+ describe DroidServices::Extensions::HasResponse do
5
+ include DroidServices::Extensions::HasResponse
6
+
7
+ subject { self }
8
+
9
+ describe '#set_message' do
10
+ it 'should set message' do
11
+ subject.set_message('foo')
12
+ subject.instance_variable_get(:@_message).should eq('foo')
13
+ end
14
+ end
15
+
16
+ describe '#add_error' do
17
+ it 'should add error' do
18
+ subject.add_error('foo')
19
+ subject.instance_variable_get(:@_errors).should include('foo')
20
+ end
21
+ end
22
+
23
+ describe '#add_error_unless' do
24
+ it 'should add error when condition false' do
25
+ subject.add_error_unless('foo', 1==0)
26
+ subject.instance_variable_get(:@_errors).should include('foo')
27
+ end
28
+
29
+ it 'should not add error when condition true' do
30
+ subject.add_error_unless('foo', 1==1)
31
+ subject.instance_variable_get(:@_errors).should be_nil
32
+ end
33
+ end
34
+
35
+ describe '#add_errors_from' do
36
+ let(:messages) { [[:foo, 'bar'], [:bar, 'bazz']]}
37
+ let(:errors) { mock(messages: messages)}
38
+ let(:model) { mock(errors: errors) }
39
+
40
+ it 'should add errors from model error messages' do
41
+ subject.add_errors_from(model)
42
+ subject.instance_variable_get(:@_errors).should eq(['foo: bar', 'bar: bazz'])
43
+ end
44
+ end
45
+
46
+ describe '#assign_resource' do
47
+ it 'should set assignments' do
48
+ subject.assign_resource('foo', 'bar')
49
+ subject.instance_variable_get(:@_assignments).should eq({foo: 'bar'})
50
+ end
51
+ end
52
+
53
+ describe '#respond_with' do
54
+ before { subject.stub!(:resource_name).and_return(:foo) }
55
+ it 'calls DroidServices::Response' do
56
+ DroidServices::Response.should_receive(:new).with(:foo, 'bar').and_return(mock.as_null_object)
57
+ subject.respond_with('bar')
58
+ end
59
+
60
+ it 'should return DroidServices::Response instance' do
61
+ subject.respond_with('bar').should be_a_kind_of(DroidServices::Response)
62
+ end
63
+
64
+ it 'should assign message, errors and additional assignments to response object' do
65
+ response = mock
66
+ DroidServices::Response.should_receive(:new).and_return(response)
67
+ response.should_receive(:message=)
68
+ response.should_receive(:errors=)
69
+ response.should_receive(:assignments=)
70
+ subject.respond_with('bar')
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ module DroidServices
4
+ describe Response do
5
+ subject { DroidServices::Response.new('some_resource', 'some_result')}
6
+
7
+ describe '#with_message' do
8
+ before { subject.with_message('some message') }
9
+ it { should be_kind_of(DroidServices::Response) }
10
+ its(:message) { should be }
11
+ its(:message) { should eq('some message') }
12
+ end
13
+
14
+ describe '#with_errors' do
15
+ before { subject.with_errors(['some', 'errors']) }
16
+ it { should be_kind_of(DroidServices::Response) }
17
+ its(:errors) { should be }
18
+ its(:errors) { should eq(['some', 'errors']) }
19
+ end
20
+
21
+ describe '#success?' do
22
+ context 'when there are no errors' do
23
+ before { subject.with_errors([]) }
24
+ its(:success?) { should be_true }
25
+ end
26
+
27
+ context 'when there are errors' do
28
+ before { subject.with_errors(['some', 'errors']) }
29
+ its(:success?) { should be_false }
30
+ end
31
+ end
32
+
33
+ describe '#failure?' do
34
+ context 'when there are no errors' do
35
+ before { subject.with_errors([]) }
36
+ its(:failure?) { should be_false }
37
+ end
38
+
39
+ context 'when there are errors' do
40
+ before { subject.with_errors(['some', 'errors']) }
41
+ its(:failure?) { should be_true }
42
+ end
43
+ end
44
+
45
+ describe '#notice' do
46
+ before { subject.with_message('some message') }
47
+ context 'when there are no errors' do
48
+ before { subject.with_errors([]) }
49
+ its(:notice) { should be }
50
+ its(:notice) { should eq('some message') }
51
+ end
52
+
53
+ context 'when there are errors' do
54
+ before { subject.with_errors(['some', 'errors']) }
55
+ its(:notice) { should be_nil }
56
+ end
57
+ end
58
+
59
+ describe '#error' do
60
+ context 'when there are no errors' do
61
+ before { subject.with_errors([]) }
62
+ its(:error) { should be_nil }
63
+ end
64
+
65
+ context 'when there are errors' do
66
+ before { subject.with_errors(['some', 'message']) }
67
+ its(:error) { should be }
68
+ its(:error) { should eq('some') }
69
+ end
70
+
71
+ context 'when message is present' do
72
+ before { subject.with_message('some message').with_errors(['some', 'message']) }
73
+ its(:error) { should be }
74
+ its(:error) { should eq('some message') }
75
+ end
76
+ end
77
+
78
+ describe '#has_error?' do
79
+ context 'when there are no errors' do
80
+ before { subject.with_errors([]) }
81
+ specify { subject.has_error?('foo').should be_false }
82
+ end
83
+
84
+ context 'when there are errors' do
85
+ before { subject.with_errors(['some', 'message']) }
86
+ specify { subject.has_error?('some').should be_true }
87
+ specify { subject.has_error?('foo').should be_false }
88
+ end
89
+ end
90
+
91
+ describe '#method_missing' do
92
+ before { subject.assignments = {foo: 'bar'} }
93
+ its(:foo) { should eq('bar') }
94
+ specify { expect{ subject.bar }.to raise_error(NoMethodError) }
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,6 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
6
+ require 'droid_services'
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: droid_services
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Droidlabs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yell
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Droid DroidServices
79
+ email:
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - lib/droid_services/base.rb
85
+ - lib/droid_services/base_resource.rb
86
+ - lib/droid_services/callbacks.rb
87
+ - lib/droid_services/decorator.rb
88
+ - lib/droid_services/extensions/has_callbacks.rb
89
+ - lib/droid_services/extensions/has_decorator.rb
90
+ - lib/droid_services/extensions/has_logger.rb
91
+ - lib/droid_services/extensions/has_resource.rb
92
+ - lib/droid_services/extensions/has_response.rb
93
+ - lib/droid_services/extensions/has_service.rb
94
+ - lib/droid_services/response.rb
95
+ - lib/droid_services/version.rb
96
+ - lib/droid_services.rb
97
+ - LICENSE.txt
98
+ - Rakefile
99
+ - README.md
100
+ - spec/services/base_resource_spec.rb
101
+ - spec/services/base_spec.rb
102
+ - spec/services/callbacks_spec.rb
103
+ - spec/services/decorator_spec.rb
104
+ - spec/services/extensions/has_callbacks_spec.rb
105
+ - spec/services/extensions/has_decorator_spec.rb
106
+ - spec/services/extensions/has_logger_spec.rb
107
+ - spec/services/extensions/has_resource_spec.rb
108
+ - spec/services/extensions/has_response_spec.rb
109
+ - spec/services/response_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage:
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.25
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Droid DroidServices
136
+ test_files:
137
+ - spec/services/base_resource_spec.rb
138
+ - spec/services/base_spec.rb
139
+ - spec/services/callbacks_spec.rb
140
+ - spec/services/decorator_spec.rb
141
+ - spec/services/extensions/has_callbacks_spec.rb
142
+ - spec/services/extensions/has_decorator_spec.rb
143
+ - spec/services/extensions/has_logger_spec.rb
144
+ - spec/services/extensions/has_resource_spec.rb
145
+ - spec/services/extensions/has_response_spec.rb
146
+ - spec/services/response_spec.rb
147
+ - spec/spec_helper.rb