light_operations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d66e8c5f9729b8c5d261eab885164317a64619f
4
+ data.tar.gz: aec76cd8d6088dfbcafee5e1eb607d58cd30d901
5
+ SHA512:
6
+ metadata.gz: 51a815033d9623f4e3b70087e6a9b85e70a1a982dcafadd92676b415037fd604daddffa02a6f132121591b7f54a6d7f1406288e6eb311395341275f7fb783242
7
+ data.tar.gz: ee0e5dc1efa1f059421fc5fca252e78da16e5a07345decac4c7fd091f4cb69d2d95e0388f9422f024e12069737f09e32eb66124f603a6102e8dc8d18a7633e69
data/.gitignore ADDED
@@ -0,0 +1,23 @@
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
23
+ .rubocop_todo.yml
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --format progress
4
+ --no-profile
data/.rubocop.yml ADDED
@@ -0,0 +1,47 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-02-23 06:17:38 +0100 using RuboCop version 0.27.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 3
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ # Offense count: 2
13
+ # Cop supports --auto-correct.
14
+ # Configuration parameters: PreferredDelimiters.
15
+ Style/PercentLiteralDelimiters:
16
+ Enabled: false
17
+
18
+ # Offense count: 2
19
+ Style/RegexpLiteral:
20
+ MaxSlashes: 0
21
+
22
+ # Offense count: 1
23
+ # Cop supports --auto-correct.
24
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
25
+ Style/TrailingBlankLines:
26
+ Enabled: false
27
+
28
+ # Offense count: 1
29
+ # Cop supports --auto-correct.
30
+ # Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, Whitelist.
31
+ Style/TrivialAccessors:
32
+ Enabled: false
33
+
34
+ # Offense count: 2
35
+ # Cop supports --auto-correct.
36
+ Style/UnneededPercentQ:
37
+ Enabled: false
38
+
39
+ Documentation:
40
+ Enabled: false
41
+
42
+ Metrics/LineLength:
43
+ Max: 120
44
+
45
+ AllCops:
46
+ Exclude:
47
+ - '**/Guardfile'
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ script: bundle exec rake
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swift_operations.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
3
+ watch(%r{^spec/(.+).rb$}) { |m| "spec/#{m[1]}.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ watch('Gemfile')
6
+ end
7
+
8
+ guard :rubocop, all_on_start: false, cli: ['--format', 'clang'] do
9
+ watch(%r{.+\.rb$})
10
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pawel Niemczyk
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,31 @@
1
+ # LightOperations
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'light_operations'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install light_operations
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/swift_operations/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: 'spec'
@@ -0,0 +1,63 @@
1
+ require 'active_support'
2
+
3
+ module LightOperations
4
+ class Core
5
+ include ::ActiveSupport::Rescuable
6
+ include ::ActiveSupport
7
+ MissingDependency = Class.new(StandardError)
8
+
9
+ attr_reader :dependencies, :params, :subject
10
+
11
+ def initialize(params = {}, dependencies = {})
12
+ @params, @dependencies = params, dependencies
13
+ end
14
+
15
+ # do not override this method
16
+ def run
17
+ @subject = execute
18
+ self
19
+ rescue => exception
20
+ rescue_with_handler(exception) || raise
21
+ ensure
22
+ self
23
+ end
24
+
25
+ def on_success(&block)
26
+ block.call(subject) if success?
27
+ self
28
+ end
29
+
30
+ def on_fail(&block)
31
+ block.call(subject, errors) unless success?
32
+ self
33
+ end
34
+
35
+ def errors
36
+ @errors ||= (subject.respond_to?(:errors) ? subject.errors : [])
37
+ end
38
+
39
+ def fail?
40
+ !success?
41
+ end
42
+
43
+ def success?
44
+ errors.respond_to?(:empty?) ? errors.empty? : !errors
45
+ end
46
+
47
+ protected
48
+
49
+ def execute
50
+ fail 'Not implemented yet'
51
+ end
52
+
53
+ def fail!(errors = [])
54
+ @errors = errors
55
+ end
56
+
57
+ def dependency(name)
58
+ dependencies.fetch(name)
59
+ rescue KeyError => e
60
+ raise MissingDependency, e.message
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module LightOperations
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'light_operations/version'
2
+ require 'light_operations/core'
3
+
4
+ module LightOperations
5
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'light_operations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'light_operations'
8
+ spec.version = LightOperations::VERSION
9
+ spec.authors = ['Pawel Niemczyk']
10
+ spec.email = ['pniemczyk.info@.gmail.com']
11
+ spec.summary = %q{Light operations}
12
+ spec.description = %q{Light operations for success or fail result}
13
+ spec.homepage = 'https://github.com/pniemczyk/light_operations'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency 'activesupport', '~> 4.2'
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.4'
24
+ spec.add_development_dependency 'rspec', '~> 3.1'
25
+ spec.add_development_dependency 'guard', '~> 2.12'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
27
+ spec.add_development_dependency 'guard-rubocop', '~> 1.2'
28
+ spec.add_development_dependency 'coveralls', '~> 0.7'
29
+ spec.add_development_dependency 'awesome_print', '~> 1.6'
30
+ end
@@ -0,0 +1,158 @@
1
+ require 'spec_helper'
2
+
3
+ describe LightOperations::Core do
4
+ let(:login_service) { double('login_service') }
5
+ let(:params) { { login: 'pawel', password: 'abc' } }
6
+ let(:dependencies) { { login_service: login_service } }
7
+
8
+ def subject_factory(&block)
9
+ Class.new(described_class).tap do |klass|
10
+ klass.class_eval(&block)
11
+ end.new(params, dependencies)
12
+ end
13
+
14
+ subject { described_class.new(params, dependencies) }
15
+
16
+ it 'raise error when #execute is not implemented' do
17
+ expect { subject.run }.to raise_error('Not implemented yet')
18
+ end
19
+
20
+ context 'use cases' do
21
+
22
+ # dependency using
23
+
24
+ context 'dependency usage' do
25
+ subject do
26
+ subject_factory do
27
+ def execute
28
+ dependency(:login_service)
29
+ end
30
+ end
31
+ end
32
+
33
+ it 'is allowed when is initialized correctly' do
34
+ expect { subject.run }.not_to raise_error
35
+ end
36
+
37
+ context 'raise_error' do
38
+ let(:dependencies) { {} }
39
+ it 'when dependency missing' do
40
+ expect { subject.run }.to raise_error(LightOperations::Core::MissingDependency)
41
+ end
42
+ end
43
+ end
44
+
45
+ # error handling
46
+
47
+ context '.rescue_from specific error' do
48
+ context 'by block' do
49
+ subject do
50
+ subject_factory do
51
+ rescue_from StandardError do |_exception|
52
+ fail 'execute block instead original error'
53
+ end
54
+
55
+ def execute
56
+ fail StandardError, 'What now'
57
+ end
58
+ end
59
+ end
60
+
61
+ it 'call' do
62
+ expect { subject.run }.to raise_error('execute block instead original error')
63
+ end
64
+ end
65
+
66
+ context 'by defined method' do
67
+ subject do
68
+ subject_factory do
69
+ rescue_from StandardError, with: :rescue_me
70
+
71
+ def rescue_me
72
+ fail 'execute rescue_me method instead original error'
73
+ end
74
+
75
+ def execute
76
+ fail StandardError, 'What now'
77
+ end
78
+ end
79
+ end
80
+
81
+ it 'execute' do
82
+ expect { subject.run }.to raise_error('execute rescue_me method instead original error')
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'when operation is successful' do
88
+ subject do
89
+ subject_factory do
90
+ def execute
91
+ :success
92
+ end
93
+ end
94
+ end
95
+
96
+ it '#on_success' do
97
+ subject.run.on_success do |result|
98
+ expect(result).to eq(:success)
99
+ end
100
+ end
101
+
102
+ it '#on_fail' do
103
+ block_to_exec = -> () {}
104
+ expect(block_to_exec).not_to receive(:call)
105
+ subject.run.on_fail(&block_to_exec)
106
+ end
107
+
108
+ it '#errors' do
109
+ expect(subject.run.errors).to eq([])
110
+ end
111
+
112
+ it '#fail?' do
113
+ expect(subject.run.fail?).to eq(false)
114
+ end
115
+
116
+ it '#success?' do
117
+ expect(subject.run.success?).to eq(true)
118
+ end
119
+ end
120
+
121
+ context 'when operation is fail' do
122
+ subject do
123
+ subject_factory do
124
+ def execute
125
+ fail!([email: :unknown])
126
+ :fail
127
+ end
128
+ end
129
+ end
130
+
131
+ it '#on_success' do
132
+ block_to_exec = -> () {}
133
+ expect(block_to_exec).not_to receive(:call)
134
+ subject.run.on_success(&block_to_exec)
135
+ end
136
+
137
+ it '#on_fail' do
138
+
139
+ subject.run.on_fail do |result, errors|
140
+ expect(result).to eq(:fail)
141
+ expect(errors).to eq([email: :unknown])
142
+ end
143
+ end
144
+
145
+ it '#errors' do
146
+ expect(subject.run.errors).to eq([email: :unknown])
147
+ end
148
+
149
+ it '#fail?' do
150
+ expect(subject.run.fail?).to eq(true)
151
+ end
152
+
153
+ it '#success?' do
154
+ expect(subject.run.success?).to eq(false)
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,30 @@
1
+ require 'bundler/setup'
2
+ require 'awesome_print'
3
+ Bundler.setup
4
+
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+ require 'light_operations'
9
+
10
+ RSpec.configure do |config|
11
+ config.filter_run :focus
12
+ config.run_all_when_everything_filtered = true
13
+
14
+ config.default_formatter = 'doc' if config.files_to_run.one?
15
+
16
+ config.profile_examples = 10
17
+ config.order = :random
18
+
19
+ Kernel.srand config.seed
20
+
21
+ config.expect_with :rspec do |expectations|
22
+ expectations.syntax = :expect
23
+ end
24
+
25
+ config.mock_with :rspec do |mocks|
26
+ mocks.syntax = :expect
27
+ mocks.verify_partial_doubles = true
28
+ end
29
+ end
30
+ require 'guard/rubocop'
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light_operations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pawel Niemczyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.7'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: awesome_print
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.6'
139
+ description: Light operations for success or fail result
140
+ email:
141
+ - pniemczyk.info@.gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - Guardfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - lib/light_operations.rb
156
+ - lib/light_operations/core.rb
157
+ - lib/light_operations/version.rb
158
+ - light_operations.gemspec
159
+ - spec/lib/core_spec.rb
160
+ - spec/spec_helper.rb
161
+ homepage: https://github.com/pniemczyk/light_operations
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.4.3
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Light operations
185
+ test_files:
186
+ - spec/lib/core_spec.rb
187
+ - spec/spec_helper.rb