interactor-copy_context 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79f620e40edaf5d49a865cbb33b4c17122ac54d6
4
+ data.tar.gz: a3229c54f831ecf7a4dbed367fb341dcbb1d035e
5
+ SHA512:
6
+ metadata.gz: 78bb812e0a1d2fc348f53c7ba7a2c0a2d3bb60753f534788911d8a4602e4baa18c593f16fa1f86a0cfeb1a539b39e800d782eaf832adcb77a28431a1ea9cd8be
7
+ data.tar.gz: ac551502fb93eca7b42b1765580f7668af02e923d56090f99c3eec79dc031c611e2c9ead2d6908b96ab4694a1d882dfbfe2e2965302e5842717cdd2952e87072
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in interactor-copy_context.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Fire-Dragon-DoL
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Interactor CopyContext
2
+
3
+ This gem allows you to easily extend [interactor](https://github.com/collectiveidea/interactor) so that in
4
+ Ruby on Rails you can eventually copy the `context` on your controller as `@instace_variables`, accessing them
5
+ easily. Here is an example:
6
+
7
+ ```ruby
8
+ class MakeCake
9
+ include Interactor
10
+
11
+ def perform
12
+ # dosomething
13
+ end
14
+ end
15
+
16
+ class CakesController < ApplicationController
17
+
18
+ def create
19
+ MakeCake.perform_on(self, ingredients: [:egg, :milk, :something])
20
+
21
+ render text: @ingredients.first.to_s # => "egg"
22
+ end
23
+
24
+ end
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'interactor-copy_context', '~> 0.0.1'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ ```bash
38
+ $ bundle
39
+ ```
40
+
41
+ Or install it yourself as:
42
+
43
+ ```bash
44
+ $ gem install interactor-copy_context
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Usage is very simple, use the method `perform_on` which is just a wrapper for `perform` (accepts same params).
50
+ The only important thing is **first parameter is the object which you want to inject context into**. So for
51
+ example, `self` in a controller is the controller itself, very helpful because data is ready for your views in
52
+ that case. The method is available both on Interactor and organizer and on instance and class too.
53
+
54
+ ```ruby
55
+ DummyInteractor.perform_on(self, dummy1: 'blah')
56
+ DummyInteractor.new(dummy1: 'blah').perform_on(self)
57
+ ```
58
+
59
+ Both methods inject same context, so they are equivalent. Same for organizer methods (they behave like
60
+ interactors):
61
+
62
+ ```ruby
63
+ DummyOrganizer.perform_on(self)
64
+ DummyOrganizer.new.perform_on(self)
65
+ ```
66
+
67
+ ## About performance
68
+ It's a copy of an entire hash (not a deep copy, only pointers). I don't think context will ever grow over 100
69
+ elements, so it should be fine.
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/[my-github-username]/interactor-copy_context/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'interactor/copy_context/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "interactor-copy_context"
8
+ spec.version = Interactor::CopyContext::VERSION
9
+ spec.authors = ["Fire-Dragon-DoL"]
10
+ spec.email = ["francesco.belladonna@gmail.com"]
11
+ spec.summary = %q{Copy interactor context on specified object's instance variables}
12
+ spec.description = %q{Copy interactor context on specified object's instance variables to allow easier interaction with returned values}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rspec", "~> 2.14.0"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rake"
25
+
26
+ spec.add_dependency "interactor", "~> 2.1"
27
+ end
@@ -0,0 +1,5 @@
1
+ module Interactor
2
+ module CopyContext
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require 'interactor/copy_context/version'
2
+ require 'interactor/ext/interactor'
3
+ require 'interactor/ext/interactor/organizer'
@@ -0,0 +1,24 @@
1
+ module Interactor
2
+
3
+ module ClassMethods
4
+
5
+ def perform_on(obj, *arguments)
6
+ perform(*arguments).tap do |instance|
7
+ instance.context.each do |key, value|
8
+ obj.send(:instance_variable_set, :"@#{ key }", value)
9
+ end
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ # InstanceMethods
16
+
17
+ def perform_on(obj)
18
+ perform
19
+ context.each do |key, value|
20
+ obj.send(:instance_variable_set, :"@#{ key }", value)
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe DummyInteractor do
4
+ let(:dummy1) { 'testmedummy' }
5
+ let(:dummy_object) do
6
+ Class.new do
7
+ attr_accessor :dummy1
8
+ end.new
9
+ end
10
+
11
+ it { expect(DummyInteractor).to respond_to(:perform_on) }
12
+ it { should respond_to(:perform_on) }
13
+
14
+ it "sets @dummy1 on dummy_object" do
15
+ DummyInteractor.perform_on(dummy_object, dummy1: dummy1)
16
+
17
+ expect(dummy_object.dummy1).to eq dummy1
18
+ end
19
+
20
+ context "when called on instance" do
21
+
22
+ it "sets @dummy1 on dummy_object" do
23
+ interactor = DummyInteractor.new(dummy1: dummy1)
24
+ interactor.perform_on(dummy_object)
25
+
26
+ expect(dummy_object.dummy1).to eq dummy1
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe DummyOrganizer do
4
+ let(:dummy1) { Dummy1Interactor::DUMMYVAR }
5
+ let(:dummy2) { Dummy2Interactor::DUMMYVAR }
6
+ let(:dummy_object) do
7
+ Class.new do
8
+ attr_accessor :dummy1
9
+ attr_accessor :dummy2
10
+ end.new
11
+ end
12
+
13
+ it { expect(DummyOrganizer).to respond_to(:perform_on) }
14
+ it { should respond_to(:perform_on) }
15
+
16
+ it "sets @dummy1 on dummy_object" do
17
+ DummyOrganizer.perform_on(dummy_object)
18
+
19
+ expect(dummy_object.dummy1).to eq dummy1
20
+ end
21
+
22
+ it "sets @dummy2 on dummy_object" do
23
+ DummyOrganizer.perform_on(dummy_object)
24
+
25
+ expect(dummy_object.dummy2).to eq dummy2
26
+ end
27
+
28
+ context "when called on instance" do
29
+
30
+ it "sets @dummy1 on dummy_object" do
31
+ interactor = DummyOrganizer.new
32
+ interactor.perform_on(dummy_object)
33
+
34
+ expect(dummy_object.dummy1).to eq dummy1
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'interactor'
2
+ require 'interactor/copy_context'
3
+
4
+ support_path = Pathname.new(File.expand_path('..', __FILE__))
5
+ support_path = support_path.join('support', '**', '*.rb')
6
+ Dir[support_path].each { |f| require f }
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ config.filter_run_excluding broken: true
13
+
14
+ config.expect_with :rspec do |c|
15
+ c.syntax = :expect
16
+ end
17
+
18
+ # Run specs in random order to surface order dependencies. If you find an
19
+ # order dependency and want to debug it, you can fix the order by providing
20
+ # the seed, which is printed after each run.
21
+ # --seed 1234
22
+ config.order = 'random'
23
+ end
@@ -0,0 +1,12 @@
1
+ class DummyInteractor
2
+ include Interactor
3
+
4
+ def perform
5
+ if context[:should_fail]
6
+ context.fail!
7
+ else
8
+ context[:custom_var_1] = 'testme'
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,35 @@
1
+ class Dummy1Interactor
2
+ include Interactor
3
+
4
+ DUMMYVAR = 'testmedummy1'
5
+
6
+ def perform
7
+ if context[:should_fail]
8
+ context.fail!
9
+ else
10
+ context[:dummy1] = DUMMYVAR
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ class Dummy2Interactor
17
+ include Interactor
18
+
19
+ DUMMYVAR = 'testmedummy2'
20
+
21
+ def perform
22
+ if context[:should_fail]
23
+ context.fail!
24
+ else
25
+ context[:dummy2] = DUMMYVAR
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ class DummyOrganizer
32
+ include Interactor::Organizer
33
+
34
+ organize Dummy1Interactor, Dummy2Interactor
35
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interactor-copy_context
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fire-Dragon-DoL
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: interactor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ description: Copy interactor context on specified object's instance variables to allow
84
+ easier interaction with returned values
85
+ email:
86
+ - francesco.belladonna@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".ruby-version"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - interactor-copy_context.gemspec
99
+ - lib/interactor/copy_context.rb
100
+ - lib/interactor/copy_context/version.rb
101
+ - lib/interactor/ext/interactor.rb
102
+ - spec/features/interactor_spec.rb
103
+ - spec/features/organizer_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/dummy_interactor.rb
106
+ - spec/support/dummy_organizer.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Copy interactor context on specified object's instance variables
131
+ test_files:
132
+ - spec/features/interactor_spec.rb
133
+ - spec/features/organizer_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/dummy_interactor.rb
136
+ - spec/support/dummy_organizer.rb