simple_interaction 0.0.3 → 0.0.5

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
  SHA1:
3
- metadata.gz: 65e48aa19b408f562e5a17262322879d036211a0
4
- data.tar.gz: 42dab38a13d25b5d4ba5b922ba50f85f894d4842
3
+ metadata.gz: f1373c9de32c59e77fa3bac5d1f86dd66a1b9c6b
4
+ data.tar.gz: 598115f757d7229015e79fa08b63b0a2b7f78d84
5
5
  SHA512:
6
- metadata.gz: 4b80343bfcfe3049868417fdc769c9c31a9275ac309f93c8326c563a6bbf68fd6bb05212c660b418345d8e2a42d57eadbe5829e65e8cfe16ac2fcb6496d8283d
7
- data.tar.gz: f928690ddc5094fb7dc8360c42e5cc8aca014ce1a39d07849b43c716bf924ae30d2266aaf41806806b511755673579fa0519d68c995baa1c50f0797035a75632
6
+ metadata.gz: 1f88bd9e54db5e2a541cf7aafcf7589054274b15088e86b818bbb3159ce23de201ab9d0979072d2ce7e6b059cb87c3099897529097e1111eb3ed73d568e37dc9
7
+ data.tar.gz: af454ff6d113e2b7d0d76f5195fc47fb4222fbbe026baf7d219f3106185b41549b6db629fdd6e2bcfc914d6159fe0863062447d0b1ca99d041f25ecf620418b2
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ bin/
10
11
  *.bundle
11
12
  *.so
12
13
  *.o
data/README.md CHANGED
@@ -4,12 +4,22 @@ Interactions are meant to keep controllers and models or any other business logi
4
4
  Keep intention of class clear when using interactions, for example:
5
5
  To create a user, a class should be name Users::Create.
6
6
 
7
+ If you are using this in a Rails application you might want to use
8
+
9
+ ```ruby
10
+ gem 'simple_interaction-rails, github: "boza/simple_interaction-rails"'
11
+ ```
12
+
13
+ this comes with a generator so you don't have to create individual files :)
14
+
15
+
16
+
7
17
  ## Installation
8
18
 
9
19
  Add this line to your application's Gemfile:
10
20
 
11
21
  ```ruby
12
- gem 'interaction'
22
+ gem 'simple_interaction'
13
23
  ```
14
24
 
15
25
  And then execute:
@@ -18,13 +28,13 @@ And then execute:
18
28
 
19
29
  Or install it yourself as:
20
30
 
21
- $ gem install interaction
31
+ $ gem install simple_interaction
22
32
 
23
33
  ## Usage
24
34
 
25
35
  ```
26
36
  class Klass
27
- include Interaction
37
+ include SimpleInteraction
28
38
 
29
39
  fail_with 'ErrorClass'
30
40
  requires :param1, :param2
data/interaction.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = '~> 2.0'
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.7"
22
23
  spec.add_development_dependency "rake", "~> 10.0"
@@ -0,0 +1,60 @@
1
+ module SimpleInteraction
2
+ module ClassMethods
3
+
4
+ attr_accessor :requirements
5
+
6
+ # required arguments to run interaction `requires :param`
7
+ def requires(*attrs)
8
+ set_requirements(attrs)
9
+ end
10
+
11
+ # interaction requirements
12
+ def requirements
13
+ @requirements ||= []
14
+ end
15
+
16
+ # specifies the error class that will be raise if interaction is run with !
17
+ # if nothing is set InteractionError will be the default error class
18
+ def fail_with(klass)
19
+ @error_class = self.const_set(klass, Class.new(InteractionError))
20
+ end
21
+
22
+ def error_class
23
+ @error_class ||= InteractionError
24
+ end
25
+
26
+ # checks if requirements are met from the requires params
27
+ # creates an instance of the interaction
28
+ # calls run on the instance
29
+ def run(**options)
30
+ @options = options
31
+ fail RequirementsNotMet.new("#{self} requires the following parameters #{requirements}") unless requirements_met?
32
+ new(@options).tap do |interaction|
33
+ interaction.__send__(:run)
34
+ end
35
+ end
36
+
37
+ # runs interaction raises if any error or returns the interaction result
38
+ def run!(**options)
39
+ interaction = run(options)
40
+ raise error_class.new(interaction.error) unless interaction.success?
41
+ interaction.result
42
+ end
43
+
44
+ private
45
+
46
+ def requirements_met?
47
+ return true if requirements.empty?
48
+ return false unless @options.kind_of?(Hash)
49
+ requirements.each do |accessor|
50
+ break false unless @options.has_key?(accessor)
51
+ end
52
+ end
53
+
54
+ def set_requirements(attrs)
55
+ attr_accessor *attrs
56
+ requirements.unshift(*attrs)
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ module SimpleInteraction
2
+
3
+ #main interaction error class
4
+ class InteractionError< StandardError;end
5
+
6
+ # this exception is raised when any arguments on `requires` method is missing
7
+ class RequirementsNotMet < InteractionError;end
8
+
9
+ # this exception will be raised if no run method is implemented on the interaction class
10
+ class NotImplemented < InteractionError;end
11
+
12
+ end
@@ -0,0 +1,33 @@
1
+ module SimpleInteraction
2
+ module InstanceMethods
3
+
4
+ # error message if interaction was not successful
5
+ attr_accessor :error
6
+
7
+ # result of interaction, set when interaction was successful in otherwise is nil
8
+ attr_accessor :result
9
+
10
+ def success?
11
+ error.nil?
12
+ end
13
+
14
+ private
15
+
16
+ # sets the attr_accessor for each requirement of the intereaction
17
+ def initialize(**opts)
18
+ opts.select { |option, _| self.class.requirements.include?(option) }.each do |accessor, value|
19
+ __send__("#{accessor}=", value)
20
+ end
21
+ end
22
+
23
+ # run method should be overwritten on every class where interaction is included
24
+ def run
25
+ fail NotImplemented.new("`run` instance method must be implemented in class #{self.class}") unless defined?(super)
26
+ interaction_run = super
27
+ if success?
28
+ @result = interaction_run
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleInteraction
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,8 +1,11 @@
1
1
  require "simple_interaction/version"
2
+ require "simple_interaction/exceptions"
3
+ require "simple_interaction/instance_methods"
4
+ require "simple_interaction/class_methods"
2
5
 
3
6
  module SimpleInteraction
4
7
 
5
- # = Interactions
8
+ # = Simple Interaction
6
9
  #
7
10
  # Interactions are meant to keep controllers and models slim (YAY).
8
11
  # Keep intention of class clear when using interactions
@@ -29,94 +32,10 @@ module SimpleInteraction
29
32
  # end
30
33
  #
31
34
  # Klass.run(param1: param1, param2: param2)
32
-
33
- class InteractionError< StandardError;end
34
- class RequirementsNotMet < InteractionError;end
35
- class NotImplemented < InteractionError;end
36
-
37
- module ClassMethods
38
-
39
- attr_accessor :accessors
40
-
41
- def accessors
42
- @accessors ||= []
43
- end
44
-
45
- def error_class
46
- @error_class ||= InteractionError
47
- end
48
-
49
- # class method to make sure parameters are required for the interaction to work.
50
- def requires(*attrs)
51
- set_accessors(attrs)
52
- end
53
-
54
- def fail_with(klass)
55
- @error_class = self.const_set(klass, Class.new(InteractionError))
56
- end
57
-
58
- # checks if requirements are met from the requires params
59
- # creates an instance of the interaction
60
- # calls run on the instance
61
- def run(**options)
62
- @options = options
63
- fail RequirementsNotMet.new("#{self} requires the following parameters #{accessors}") unless requirements_met?
64
- new(@options).__send__(:run)
65
- end
66
-
67
- def run!(**options)
68
- interaction = run(options)
69
- raise error_class.new(interaction.error) unless interaction.success?
70
- interaction.result
71
- end
72
-
73
- private
74
-
75
- def requirements_met?
76
- return true if accessors.empty?
77
- return false unless @options.kind_of?(Hash)
78
- accessors.each do |accessor|
79
- break false unless @options.has_key?(accessor)
80
- end
81
- end
82
-
83
- def set_accessors(attrs)
84
- attr_accessor *attrs
85
- accessors.unshift(*attrs)
86
- end
87
-
88
- end
89
-
90
- module InstanceMethods
91
-
92
- attr_accessor :error, :result, :error_class
93
-
94
- def success?
95
- error.nil?
96
- end
97
-
98
- private
99
-
100
- # initializes instance and sets the instance variable for each attr_accessor from the required parameters
101
- def initialize(**opts)
102
-
103
- opts.select { |option, _| self.class.accessors.include?(option) }.each do |accessor, value|
104
- __send__("#{accessor}=", value)
105
- end
106
- end
107
-
108
- # run method should be overwritten on every class where interaction is included
109
- def run
110
- fail NotImplemented.new("`run` instance method must be implemented in class #{self.class}") unless defined?(super)
111
- interaction_run = super
112
- @result = interaction_run if success?
113
- self
114
- end
115
-
116
- end
117
35
 
118
36
  def self.included(receiver)
119
37
  receiver.extend ClassMethods
120
38
  receiver.send :prepend, InstanceMethods
121
39
  end
40
+
122
41
  end
@@ -45,7 +45,8 @@ module SimpleInteraction
45
45
  end
46
46
 
47
47
  def test_success
48
- assert Interactor.run(param: 'awesome interaction').success?, 'Interactor didn\'t finish successfuly'
48
+ interaction = Interactor.run(param: 'awesome interaction')
49
+ assert interaction.success?, "Interactor didn't finish successfuly #{interaction.error}"
49
50
  end
50
51
 
51
52
  def test_run_with_bang
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_interaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Boza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-17 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: bundler
@@ -52,6 +52,9 @@ files:
52
52
  - Rakefile
53
53
  - interaction.gemspec
54
54
  - lib/simple_interaction.rb
55
+ - lib/simple_interaction/class_methods.rb
56
+ - lib/simple_interaction/exceptions.rb
57
+ - lib/simple_interaction/instance_methods.rb
55
58
  - lib/simple_interaction/version.rb
56
59
  - test/interaction_test.rb
57
60
  homepage: ''
@@ -64,9 +67,9 @@ require_paths:
64
67
  - lib
65
68
  required_ruby_version: !ruby/object:Gem::Requirement
66
69
  requirements:
67
- - - ">="
70
+ - - "~>"
68
71
  - !ruby/object:Gem::Version
69
- version: '0'
72
+ version: '2.0'
70
73
  required_rubygems_version: !ruby/object:Gem::Requirement
71
74
  requirements:
72
75
  - - ">="