simple_action 1.0.4 → 1.0.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
- ODZjYTY2ODQ4MjRiODdjOWZiNGZkZTI3ODFiZjMzNjc2ODg2MzI5YQ==
4
+ MWU2NmZhYWI0MDM3NTJkNzM2NjI4NzVjYmEyZDFkODViYzE5NzJjOQ==
5
5
  data.tar.gz: !binary |-
6
- MDJhNmE2ZTg4Yzc0MTYwYzI1YWNkYTkxY2RlOGQ5Y2UyYjNmNzY4MA==
6
+ MTBjYTA2NThhZDUwN2QyN2ZiMWFlNWIzM2RjYWRkMDFjMzUxODIzZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGQ4ZGQ0NGRhMzNmODNiY2Y5ODJjOTc3Yzc0ZWYxZjFmYjUwZDdkODllNmI4
10
- NzI3MmYwYjgwMmI4ZjE1NmMxZDIwOWVhMzBjYzZiYjlkMGYxY2ExMzM5ZjFm
11
- MWM1OGJkNzQyMzY2YmE1YWI3YTllZjVhMGFiY2FjNWU2ZjlhODY=
9
+ ZWY0ZGNiYzI3MDcxOTI1OGI5NDZiMzA5M2Y2MGE1YjdmY2YxYzk0NTdjMjc5
10
+ YWE3YWYxMTI0N2M3ZjIyZDI2N2M4N2RmMzk2M2JiOWFiNTI0NDJhNjYzNDI2
11
+ NzBiZmFhOGU0OWZiN2U1ZjdlMjYyZjFmOWYxNTc4OWMwYzhlMzQ=
12
12
  data.tar.gz: !binary |-
13
- NDlhNjQyODFkZjIyYmFlYTAwNjE3NGRmMTg1MTMxNzE1OTRlZjUwN2MyMzRm
14
- OTQ3NGQ5NGY4NDYzNGRkMjM3OGM3YzQ5Y2FjMmQwM2MyMWM4N2IzZGY1MmQz
15
- NzJmMTJlMGRlNDNjMWViYzhmMGZhMGI3MzdlMDZiZWYyZmMwYTU=
13
+ N2E2ZTcwZmM2MzM2MjYyMGJjMGU5M2Y5ODk3NDM5NGFjNTg0Y2ViY2QyZjdm
14
+ YmRjNmU4NzBkNTZjMDEwMjA3MmI5NjdjMTVhNzdhZDE1ZDQ1MzdiYzA0YmQx
15
+ MDc0OGFiNmJhM2JhZTU0YTkzY2E1OTc1MDk2ZDIwNjg4ZDI4MDY=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_action (1.0.3)
4
+ simple_action (1.0.4)
5
5
  simple_params (>= 1.0.2, < 2.0)
6
6
 
7
7
  GEM
@@ -4,25 +4,25 @@ require_relative 'concerns/transactable'
4
4
  require_relative 'concerns/delegates_to_params'
5
5
 
6
6
  module SimpleAction
7
- class Service
7
+ class Service
8
8
  extend AcceptsParams
9
9
  extend Transactable
10
+ include ActiveModel::Conversion
11
+ extend ActiveModel::Naming
10
12
  include DelegatesToParams
11
13
 
12
14
  class << self
13
- def model_name
14
- ActiveModel::Name.new(self)
15
- end
16
-
17
15
  def run(params = {})
18
16
  instance = self.new(params)
17
+ instance.mark_as_ran
19
18
  result = transaction do
20
19
  if instance.valid?
21
20
  outcome = instance.execute
22
21
  instance.errors.empty? ? outcome : nil
23
22
  end
24
23
  end
25
- Response.new(instance, result)
24
+ instance.set_result(result)
25
+ instance
26
26
  end
27
27
 
28
28
  def run!(params = {})
@@ -39,6 +39,8 @@ module SimpleAction
39
39
  @raw_params = params
40
40
  @params = self.class.params_class.new(params)
41
41
  @initial_params_valid = nil
42
+ @result = nil
43
+ @has_run = false
42
44
  end
43
45
 
44
46
  def params
@@ -57,6 +59,27 @@ module SimpleAction
57
59
  raise ImplementationError, "subclasses must implement 'execute' method."
58
60
  end
59
61
 
62
+ def persisted?
63
+ false
64
+ end
65
+
66
+ def success?
67
+ valid? && @has_run
68
+ end
69
+
70
+ def result
71
+ @result
72
+ end
73
+ alias_method :value, :result
74
+
75
+ def set_result(result = nil)
76
+ @result = result
77
+ end
78
+
79
+ def mark_as_ran
80
+ @has_run = true
81
+ end
82
+
60
83
  private
61
84
  def initial_params_valid?
62
85
  if @initial_params_valid.nil?
@@ -1,3 +1,3 @@
1
1
  module SimpleAction
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
data/lib/simple_action.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'simple_action/version'
2
2
  require 'simple_action/error'
3
3
  require 'simple_action/params'
4
- require 'simple_action/response'
5
4
  require 'simple_action/service'
6
5
  require 'simple_action/concerns/accepts_params'
7
6
  require 'simple_action/concerns/transactable'
data/spec/service_spec.rb CHANGED
@@ -77,6 +77,20 @@ describe SimpleAction::Service do
77
77
  end
78
78
 
79
79
  describe "instance methods", instance_methods: true do
80
+ describe "#to_key", to_key: true do
81
+ it "has correct keys" do
82
+ params = ServiceSpecClass.new(name: "Tom", age: 12)
83
+ params.to_key.should be_nil
84
+ end
85
+ end
86
+
87
+ describe "#persisted?", persisted: true do
88
+ it "is not persisted" do
89
+ params = ServiceSpecClass.new(name: "Tom", age: 12)
90
+ params.persisted?.should eq(false)
91
+ end
92
+ end
93
+
80
94
  describe "#params", params: true do
81
95
  it "assigns params" do
82
96
  instance = ServiceSpecClass.new(name: "Nic Cage", age: "40")
@@ -127,5 +141,61 @@ describe SimpleAction::Service do
127
141
  end
128
142
  end
129
143
  end
144
+
145
+ describe "#success?", success: true do
146
+ context "without having run" do
147
+ it "is false" do
148
+ ServiceSpecClass.new(name: "Tom", age: 12).should_not be_success
149
+ end
150
+ end
151
+
152
+ context "with invalid params" do
153
+ it "is false" do
154
+ ServiceSpecClass.run(age: 12).should_not be_success
155
+ end
156
+ end
157
+
158
+ context "with valid params" do
159
+ it "is true" do
160
+ ServiceSpecClass.run(name: "Tom", age: 12).should be_success
161
+ end
162
+ end
163
+ end
164
+
165
+ describe "#result", result: true do
166
+ context "with no result provided" do
167
+ it "is nil" do
168
+ service = ServiceSpecClass.new(name: "Tom", age: 12)
169
+ service.result.should be_nil
170
+ end
171
+ end
172
+
173
+ context "with result set" do
174
+ it "is 53" do
175
+ service = ServiceSpecClass.new(name: "Tom", age: 12)
176
+ service.result.should be_nil
177
+ service.set_result(53)
178
+ service.result.should eq(53)
179
+ end
180
+ end
181
+ end
182
+
183
+ describe "#value", value: true do
184
+ context "with no result provided" do
185
+ it "is nil" do
186
+ service = ServiceSpecClass.new(name: "Tom", age: 12)
187
+ service.value.should be_nil
188
+ end
189
+ end
190
+
191
+ context "with result set" do
192
+ it "is 53" do
193
+ service = ServiceSpecClass.new(name: "Tom", age: 12)
194
+ service.result.should be_nil
195
+ service.set_result(53)
196
+ service.value.should eq(53)
197
+ end
198
+ end
199
+ end
130
200
  end
131
201
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_action
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-21 00:00:00.000000000 Z
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple_params
@@ -107,7 +107,6 @@ files:
107
107
  - lib/simple_action/concerns/transactable.rb
108
108
  - lib/simple_action/error.rb
109
109
  - lib/simple_action/params.rb
110
- - lib/simple_action/response.rb
111
110
  - lib/simple_action/service.rb
112
111
  - lib/simple_action/version.rb
113
112
  - simple_action.gemspec
@@ -116,7 +115,6 @@ files:
116
115
  - spec/fixtures/params_spec_class.rb
117
116
  - spec/fixtures/service_spec_class.rb
118
117
  - spec/params_spec.rb
119
- - spec/response_spec.rb
120
118
  - spec/service_spec.rb
121
119
  - spec/spec_helper.rb
122
120
  homepage: https://github.com/brycesenz/simple_action
@@ -149,6 +147,5 @@ test_files:
149
147
  - spec/fixtures/params_spec_class.rb
150
148
  - spec/fixtures/service_spec_class.rb
151
149
  - spec/params_spec.rb
152
- - spec/response_spec.rb
153
150
  - spec/service_spec.rb
154
151
  - spec/spec_helper.rb
@@ -1,26 +0,0 @@
1
- module SimpleAction
2
- class Response
3
- def initialize(service_object, result = nil)
4
- @service_object = service_object
5
- @result = result
6
- valid?
7
- end
8
-
9
- def valid?
10
- @service_object.valid?
11
- end
12
-
13
- def errors
14
- @service_object.errors
15
- end
16
-
17
- def success?
18
- valid?
19
- end
20
-
21
- def result
22
- @result
23
- end
24
- alias_method :value, :result
25
- end
26
- end
@@ -1,104 +0,0 @@
1
- require 'spec_helper'
2
- require 'fixtures/dummy_service_object_class'
3
-
4
- describe SimpleAction::Response do
5
- describe "#valid?" do
6
- context "with valid object" do
7
- let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
8
- let(:response) { described_class.new(object) }
9
-
10
- it "is valid" do
11
- response.should be_valid
12
- end
13
- end
14
-
15
- context "with invalid object" do
16
- let(:object) { DummyServiceObjectClass.new(name: nil) }
17
- let(:response) { described_class.new(object) }
18
-
19
- it "is not valid" do
20
- response.should_not be_valid
21
- end
22
- end
23
- end
24
-
25
- describe "#errors" do
26
- context "with valid object" do
27
- let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
28
- let(:response) { described_class.new(object) }
29
-
30
- it "has no errors" do
31
- response.errors.should be_empty
32
- end
33
- end
34
-
35
- context "with invalid object" do
36
- let(:object) { DummyServiceObjectClass.new(name: nil) }
37
- let(:response) { described_class.new(object) }
38
-
39
- it "has errors" do
40
- response.errors[:name].should eq(["can't be blank"])
41
- end
42
- end
43
- end
44
-
45
- describe "#success?" do
46
- context "with valid object" do
47
- let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
48
- let(:response) { described_class.new(object) }
49
-
50
- it "is success" do
51
- response.should be_success
52
- end
53
- end
54
-
55
- context "with invalid object" do
56
- let(:object) { DummyServiceObjectClass.new(name: nil) }
57
- let(:response) { described_class.new(object) }
58
-
59
- it "is not success" do
60
- response.should_not be_success
61
- end
62
- end
63
- end
64
-
65
- describe "#result" do
66
- let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
67
-
68
- context "with no result provided" do
69
- let(:response) { described_class.new(object) }
70
-
71
- it "is nil" do
72
- response.result.should be_nil
73
- end
74
- end
75
-
76
- context "with response provided" do
77
- let(:response) { described_class.new(object, 53) }
78
-
79
- it "is 53" do
80
- response.result.should eq(53)
81
- end
82
- end
83
- end
84
-
85
- describe "#value" do
86
- let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
87
-
88
- context "with no result provided" do
89
- let(:response) { described_class.new(object) }
90
-
91
- it "is nil" do
92
- response.value.should be_nil
93
- end
94
- end
95
-
96
- context "with response provided" do
97
- let(:response) { described_class.new(object, 53) }
98
-
99
- it "is 53" do
100
- response.value.should eq(53)
101
- end
102
- end
103
- end
104
- end