actioninteractor 0.0.14 → 0.0.15

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f08c637f81de888650ae3d393f950ab1175da21b5e6ef8372fb09e79cc74ed6c
4
- data.tar.gz: 7f3e79960f36ec8798de280126c81bb01b0db3e4a40a46425b3474ef6b3bf3d2
3
+ metadata.gz: 1cf59e621fd60a8cfb99220e97d08b679bc639a38d161e4c3254720969f761c6
4
+ data.tar.gz: bdbd3020978080ea42f44a17424c9f0ae71787ebd8fe37cbbe9e2b00569ea70a
5
5
  SHA512:
6
- metadata.gz: 97a5902178899624a1ce4f53f384b298d241bde185495076489097fc46a254531eef05dfe92475193a496bebdd66b8f7fe11847220719e14e39ee029354e4cf1
7
- data.tar.gz: b212e659fb2a716e8f1fda31b61de2fc2700941da1ef9e07b88cc94c0bdf00042487ed335929ef3950dccdd2773c80aaeeec863ae207078d4a128cd8d4504258
6
+ metadata.gz: d55546b260465dedecc262964d10001f2e12964af2d6bc2d35a4546eaa6aed91131c109590a86b68b901b7fdcb64d3b180a77c4df712e139ce839fd8ebe16691
7
+ data.tar.gz: 01c4533072dd4020a359257e516b35fac8f80b316eadd2000e63de3b350aa81ae0d2f616357365fdecfadde5331bdad37cdc39a62b948448b39c0de4cd70323b
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ActionInteractor
4
4
  class Base
5
- attr_reader :params, :result
5
+ attr_reader :params, :results
6
6
 
7
7
  def initialize(params)
8
8
  @params = params
@@ -40,7 +40,7 @@ module ActionInteractor
40
40
  end
41
41
 
42
42
  def reset!
43
- @result = {}
43
+ @results = Results.new
44
44
  @_success = false
45
45
  @_finished = false
46
46
  end
@@ -54,21 +54,12 @@ module ActionInteractor
54
54
  @_success = true
55
55
  @_finished = true
56
56
  end
57
- alias_method :finish!, :success!
58
57
 
59
58
  def fail!
60
59
  @_success = false
61
60
  @_finished = true
62
61
  end
63
62
 
64
- def add_result(key, value)
65
- @result[key] = value
66
- end
67
-
68
- def results(items = {})
69
- @result = items
70
- end
71
-
72
63
  class << self
73
64
  def execute(params)
74
65
  new(params).tap(&:execute)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module ActionInteractor
6
+ class Results
7
+ include Enumerable
8
+ extend Forwardable
9
+
10
+ attr_reader :_results
11
+
12
+ def_delegators :@_results, :clear, :keys, :values, :[], :delete
13
+
14
+ def initialize(*)
15
+ @_results = {}
16
+ end
17
+
18
+ def add(attribute, detail)
19
+ _results[attribute.to_sym] = detail
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "action_interactor/base"
3
+ $:.unshift File.dirname(__FILE__)
4
+
5
+ module ActionInteractor
6
+ autoload :Base, "action_interactor/base"
7
+ autoload :Results, "action_interactor/results"
8
+ end
@@ -1,5 +1,5 @@
1
1
  require "test/unit"
2
- require "actioninteractor"
2
+ require_relative "../lib/actioninteractor"
3
3
 
4
4
  class BaseTest < Test::Unit::TestCase
5
5
  test ".execute does not raise error" do
@@ -1,5 +1,5 @@
1
1
  require "test/unit"
2
- require_relative "../lib/action_interactor/base.rb"
2
+ require_relative "../lib/actioninteractor"
3
3
 
4
4
  class User
5
5
  attr_accessor :name
@@ -12,16 +12,17 @@ end
12
12
  class RegistrationInteractor < ActionInteractor::Base
13
13
  def execute
14
14
  return fail! unless params[:name]
15
- add_result(:user, User.new(name: params[:name]))
16
- finish!
15
+ results.add(:user, User.new(name: params[:name]))
16
+ success!
17
17
  end
18
18
  end
19
19
 
20
20
  class NotificationInteractor < ActionInteractor::Base
21
21
  def execute
22
22
  return fail! unless params[:name] || params[:email]
23
- results(name: params[:name], email: params[:email])
24
- finish!
23
+ results.add(:name, params[:name])
24
+ results.add(:email, params[:email])
25
+ success!
25
26
  end
26
27
  end
27
28
 
@@ -41,13 +42,13 @@ class InheritanceTest < Test::Unit::TestCase
41
42
  test "the result contains a user" do
42
43
  params = { name: 'John'}
43
44
  interactor = RegistrationInteractor.execute(params)
44
- assert_instance_of(User, interactor.result[:user])
45
+ assert_instance_of(User, interactor.results[:user])
45
46
  end
46
47
 
47
48
  test "the result user name is John" do
48
49
  params = { name: 'John'}
49
50
  interactor = RegistrationInteractor.execute(params)
50
- user = interactor.result[:user]
51
+ user = interactor.results[:user]
51
52
  assert_equal(user.name, 'John')
52
53
  end
53
54
 
@@ -78,9 +79,9 @@ class InheritanceTest < Test::Unit::TestCase
78
79
  test "the result user name is Taro and email is taro@example.com" do
79
80
  params = { name: 'Taro', email: 'taro@example.com'}
80
81
  interactor = NotificationInteractor.execute(params)
81
- name = interactor.result[:name]
82
+ name = interactor.results[:name]
82
83
  assert_equal(name, 'Taro')
83
- email = interactor.result[:email]
84
+ email = interactor.results[:email]
84
85
  assert_equal(email, 'taro@example.com')
85
86
  end
86
87
  end
@@ -0,0 +1,14 @@
1
+ require "test/unit"
2
+ require_relative "../lib/actioninteractor"
3
+
4
+ class ResultsTest < Test::Unit::TestCase
5
+ test "initialized correctly" do
6
+ assert_nothing_raised { ActionInteractor::Results.new }
7
+ end
8
+
9
+ test "add result for the key" do
10
+ results = ActionInteractor::Results.new
11
+ results.add(:foo, "bar")
12
+ assert_equal(results[:foo], "bar")
13
+ end
14
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actioninteractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Hashimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-28 00:00:00.000000000 Z
11
+ date: 2018-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,9 +48,11 @@ files:
48
48
  - Rakefile
49
49
  - actioninteractor.gemspec
50
50
  - lib/action_interactor/base.rb
51
+ - lib/action_interactor/results.rb
51
52
  - lib/actioninteractor.rb
52
53
  - test/base_test.rb
53
54
  - test/inheritance_test.rb
55
+ - test/results_test.rb
54
56
  homepage: https://github.com/ryohashimoto/lightrails
55
57
  licenses:
56
58
  - MIT
@@ -79,3 +81,4 @@ summary: Action Interactor provides a simple interface for performing operations
79
81
  test_files:
80
82
  - test/base_test.rb
81
83
  - test/inheritance_test.rb
84
+ - test/results_test.rb