responsive_service 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5c99af05e50eb7ce1fdc4fc46ac0ae85791584a
4
- data.tar.gz: 8377670a0580fa671948ae5683771178f99a60ef
3
+ metadata.gz: d201035ae2b1931491a0cdefb73867927a105026
4
+ data.tar.gz: e18b6ce1c12afc5b857985d6d172837970d57d56
5
5
  SHA512:
6
- metadata.gz: f755a9ddbc13d16b9d08d566c926dae033a3c3352093ee876131b6fb4845ab956e22ee5bbc1c7331ce6363528978aef9c37139e474663f94d02cbb37fd99355b
7
- data.tar.gz: fce27956087d7c51d1a326a5d501c28931ede073ebd077f2c6756ae13d2147c818a02696da1a4bb0f552a6b36955a0fecd5ea2f09722408f620efa58b1b34981
6
+ metadata.gz: 4c24e42a63f3fe65f9600afb95b700e261aa301c1a1ca7d6463d880cf348b79fec0f4eaee4bdbbec1d9094ece7471c630bd5b20059d2d7ad85a54d686dd950b8
7
+ data.tar.gz: 725f27ea43686a14e3bd3ab6eb9655e595a90b29f605458543b8b7f261ad7b4597d3872b35c49bc29141be431aaaaabd8734712e4ffa12a2b2c9700184e41475
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ module ResponsiveService
2
+ class Response
3
+ attr_reader :type, :message, :context, :valid_states
4
+
5
+ def initialize(type, message=nil, context=nil, valid_states=nil)
6
+ @valid_states = Array(valid_states || self.class.class_valid_states)
7
+ raise "Invalid type of response: #{type}" unless (@valid_states.empty? || @valid_states.include?(type))
8
+ @type = type
9
+ @context = context
10
+ @message = message
11
+ end
12
+
13
+ def self.valid_states(*args)
14
+ @class_valid_states = Array(args)
15
+ end
16
+
17
+ def self.class_valid_states
18
+ @class_valid_states
19
+ end
20
+
21
+ def method_missing(method, *args, &block)
22
+ if valid_states.empty? || valid_states.include?(method)
23
+ yield if method == type
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+
30
+ Responder = Response
31
+ end
@@ -1,13 +1,16 @@
1
1
  module ResponsiveService
2
2
  class ResponsiveService
3
- attr_reader :responder_factory
3
+ attr_reader :response_factory
4
+
5
+ # Deprecated
6
+ alias_method :responder_factory, :response_factory
4
7
 
5
8
  def initialize(dependencies={})
6
- @responder_factory = dependencies.fetch(:responder_factory, Responder)
9
+ @response_factory = dependencies[:response_factory] || dependencies.fetch(:responder_factory) { Response }
7
10
  end
8
11
 
9
12
  def call(&block)
10
- yield responder_factory.new(:unimplemented, "A ResponsiveService should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponsiveService::Responder object.\n")
13
+ yield response_factory.new(:unimplemented, "A ResponsiveService should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponsiveService::Response object.\n")
11
14
  end
12
15
  end
13
16
  end
@@ -1,3 +1,3 @@
1
1
  module ResponsiveService
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require "responsive_service/version"
2
2
  require "responsive_service/responsive_service"
3
- require "responsive_service/responder"
3
+ require "responsive_service/response"
4
4
 
5
5
  module ResponsiveService
6
6
  end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ module ResponsiveService
4
+ describe Response do
5
+ subject(:response) { Response.new(type) }
6
+ let(:type) { :success }
7
+ before { Response.instance_variable_set :@class_valid_states, nil }
8
+
9
+ it 'yields when sent :success with a type of :success' do
10
+ expect { |b| response.success(&b) }.to yield_control
11
+ end
12
+
13
+ it 'does not yield when sent :failure with a type of :success' do
14
+ expect { |b| response.failure(&b) }.not_to yield_control
15
+ end
16
+
17
+ context 'when initialized with a message' do
18
+ subject(:response) { Response.new(type, message) }
19
+ let(:message) { double(:message) }
20
+
21
+ specify { expect(response.message).to eq message }
22
+
23
+ context 'when additionally initialized with a context' do
24
+ subject(:response) { Response.new(type, message, context) }
25
+ let(:context) { double(:context) }
26
+
27
+ specify { expect(response.context).to eq context }
28
+
29
+ context 'when additionally initiaized with a set of valid states' do
30
+ subject(:response) { Response.new(type, message, context, valid_states) }
31
+ let(:valid_states) { [:success, :failure] }
32
+
33
+ it 'yields when sent :success with a type of :success' do
34
+ expect { |b| response.success(&b) }.to yield_control
35
+ end
36
+
37
+ it 'does not yield when sent :failure with a type of :success' do
38
+ expect { |b| response.failure(&b) }.not_to yield_control
39
+ end
40
+
41
+ it 'throws method missing exception if sent a non-valid state' do
42
+ expect { |b| response.foo(&b) }.to raise_exception
43
+ end
44
+
45
+ it 'reflects the valid_states' do
46
+ expect(response.valid_states).to eq [:success, :failure]
47
+ end
48
+
49
+ context 'when initialized with an invalid type' do
50
+ let(:type) { :foo }
51
+
52
+ it 'raises an exception' do
53
+ expect { response }.to raise_exception('Invalid type of response: foo')
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'backwards compatibility' do
61
+ it 'sets Responder as an alias for Response' do
62
+ expect(Responder).to eq Response
63
+ end
64
+ end
65
+
66
+ describe '.valid_states' do
67
+ context 'when set to an array of states' do
68
+ before { Response.valid_states(:success, :failure) }
69
+
70
+ context 'when the response type is :success' do
71
+ subject(:response) { Response.new(:success) }
72
+
73
+ it 'yields when sent :success' do
74
+ expect { |b| response.success(&b) }.to yield_control
75
+ end
76
+
77
+ it 'does not yield when sent :failure' do
78
+ expect { |b| response.failure(&b) }.not_to yield_control
79
+ end
80
+
81
+ it 'throws method missing exception if sent a non-valid state' do
82
+ expect { |b| response.foo(&b) }.to raise_exception
83
+ end
84
+ end
85
+
86
+ context 'when initialized with an invalid type' do
87
+ subject(:response) { Response.new(:foo) }
88
+
89
+ it 'raises an exception' do
90
+ expect { response }.to raise_exception('Invalid type of response: foo')
91
+ end
92
+ end
93
+
94
+ describe '#valid_states' do
95
+ subject(:response) { Response.new(:success) }
96
+
97
+ it 'reflects the valid_states' do
98
+ expect(response.valid_states).to eq [:success, :failure]
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -3,14 +3,21 @@ require 'spec_helper'
3
3
  module ResponsiveService
4
4
  describe ResponsiveService do
5
5
  subject(:service) { ResponsiveService.new(dependencies) }
6
- let(:dependencies) { { responder_factory: responder_factory } }
7
- let(:responder_factory) { double :responder_factory, new: nil }
8
- let(:responder) { double :responder }
6
+ let(:dependencies) { { response_factory: response_factory } }
7
+ let(:response_factory) { double :response_factory, new: nil }
8
+ let(:response) { double :response }
9
9
 
10
10
  describe '#call' do
11
- before { responder_factory.stub(:new).with(:unimplemented, "A ResponsiveService should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponsiveService::Responder object.\n").and_return(responder) }
12
- it 'yields with a responder indicating instructions' do
13
- expect { |b| service.call(&b) }.to yield_with_args(responder)
11
+ before { response_factory.stub(:new).with(:unimplemented, "A ResponsiveService should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponsiveService::Response object.\n").and_return(response) }
12
+ it 'yields with a response indicating instructions' do
13
+ expect { |b| service.call(&b) }.to yield_with_args(response)
14
+ end
15
+ end
16
+
17
+ # Deprecated
18
+ describe '#responder_factory' do
19
+ it 'is just a pointer to response_factory' do
20
+ expect(service.responder_factory).to eq service.response_factory
14
21
  end
15
22
  end
16
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responsive_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexpeachey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-01 00:00:00.000000000 Z
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,11 +80,11 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/responsive_service.rb
83
- - lib/responsive_service/responder.rb
83
+ - lib/responsive_service/response.rb
84
84
  - lib/responsive_service/responsive_service.rb
85
85
  - lib/responsive_service/version.rb
86
86
  - responsive_service.gemspec
87
- - spec/lib/responsive_service/responder_spec.rb
87
+ - spec/lib/responsive_service/response_spec.rb
88
88
  - spec/lib/responsive_service/responsive_service_spec.rb
89
89
  - spec/spec_helper.rb
90
90
  homepage: http://github.com/alexpeachey/responsive_service
@@ -112,6 +112,6 @@ signing_key:
112
112
  specification_version: 4
113
113
  summary: Easy to use responsive service pattern
114
114
  test_files:
115
- - spec/lib/responsive_service/responder_spec.rb
115
+ - spec/lib/responsive_service/response_spec.rb
116
116
  - spec/lib/responsive_service/responsive_service_spec.rb
117
117
  - spec/spec_helper.rb
@@ -1,16 +0,0 @@
1
- module ResponsiveService
2
- class Responder
3
-
4
- attr_reader :type, :message, :context
5
-
6
- def initialize(type, message=nil, context=nil)
7
- @type = type
8
- @context = context
9
- @message = message
10
- end
11
-
12
- def method_missing(method, *args, &block)
13
- yield if method == type
14
- end
15
- end
16
- end
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ResponsiveService
4
- describe Responder do
5
- subject(:responder) { Responder.new(type) }
6
- let(:type) { :success }
7
-
8
- it 'yields when sent :success with a type of :success' do
9
- expect { |b| responder.success(&b) }.to yield_control
10
- end
11
-
12
- it 'does not yield when sent :failure with a type of :success' do
13
- expect { |b| responder.failure(&b) }.to_not yield_control
14
- end
15
-
16
- context 'when initialized with a message' do
17
- subject(:responder) { Responder.new(type, message) }
18
- let(:message) { double(:message) }
19
-
20
- specify { expect(responder.message).to eq message }
21
-
22
- context 'when additionally initialized with a context' do
23
- subject(:responder) { Responder.new(type, message, context) }
24
- let(:context) { double(:context) }
25
-
26
- specify { expect(responder.context).to eq context }
27
- end
28
- end
29
- end
30
- end