simple_command 0.0.3

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: c874dea14c49cfea5cf2d5f8dafcbc8352199a55
4
+ data.tar.gz: caedd0f176a3f0091728dabd200212100c4462b0
5
+ SHA512:
6
+ metadata.gz: cbf388daa3f6482e3916afd9f2d982d22b50927e4af7e60e6b4b88d88f94020e872c1ad206bbe039e732936e3d43a065608f32d0624e6153dd172852f4f2eb68
7
+ data.tar.gz: 07ccf08861301769d364e60dce60a388bea5319988e90ab83b108409ca9a629a9e1eaf280b01a7408a4f2a8cc0a7709bff6dfb0f01940c25692f4af2fec8df43
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_command.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'simplecov'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nebulab S.r.l. (http://nebulab.it)
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,89 @@
1
+ # SimpleCommand
2
+
3
+ A simple, standardized way to build and use _Service Objects_ (aka _Commands_) in Ruby
4
+
5
+ ## Requirements
6
+
7
+ * Ruby 2.0+
8
+
9
+ ## Installation
10
+
11
+ *NOTE:* this gem is not yet published on rubygems, use the github repository.
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'simple_command', github: 'nebulab/simple_command'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install simple_command
26
+
27
+ ## Usage
28
+
29
+ Here's a basic example of a command that authenticates a user
30
+
31
+ ```ruby
32
+ # define a command class
33
+ class AuthenticateUser
34
+ # put SimpleCommand before the class' ancestors chain
35
+ prepend SimpleCommand
36
+
37
+ # optional, initialize the command with some arguments
38
+ def initialize(user, password)
39
+ @user = user
40
+ @password = password
41
+ end
42
+
43
+ # mandatory: define a #perform method. its return value will be available
44
+ # through #result
45
+ def perform
46
+ if user = User.authenticate(@email, @password)
47
+ return user
48
+ else
49
+ add_error(authentication: I18n.t "authenticate_user.failure")
50
+ end
51
+ nil
52
+ end
53
+ end
54
+ ```
55
+
56
+ Then, in your controller:
57
+
58
+ ```ruby
59
+ class SessionsController < ApplicationController
60
+ def create
61
+ # initialize and execute the command
62
+ command = AuthenticateUser.new(session_params[:user], session_params[:password]).perform
63
+
64
+ # check command outcome
65
+ if command.success?
66
+ # command#result will contain the user instance, if found
67
+ session[:user_token] = command.result.secret_token
68
+ redirect_to root_path
69
+ else
70
+ flash.now[:alert] = t(command.errors[:authentication])
71
+ render :new
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def session_params
78
+ params.require(:session).permit(:email, :password)
79
+ end
80
+ end
81
+ ```
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/nebulab/simple_command/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,43 @@
1
+ require "simple_command/version"
2
+ require "simple_command/errors"
3
+
4
+ module SimpleCommand
5
+
6
+ def perform
7
+ if !defined?(super)
8
+ raise NotImplementedError
9
+ end
10
+
11
+ @performed = true
12
+ @result = super
13
+
14
+ self
15
+ end
16
+
17
+ def success?
18
+ performed? && !failure?
19
+ end
20
+
21
+ def failure?
22
+ performed? && errors.any?
23
+ end
24
+
25
+ def result
26
+ @result
27
+ end
28
+
29
+ def errors
30
+ @errors ||= {}
31
+ end
32
+
33
+ private
34
+
35
+ def performed?
36
+ @performed ||= false
37
+ end
38
+
39
+ def add_error(key, value)
40
+ errors[key] ||= []
41
+ errors[key] << value
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleCommand
2
+ class NotImplementedError < ::StandardError ; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleCommand
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_command/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.required_ruby_version = '~> 2.0'
8
+ spec.name = "simple_command"
9
+ spec.version = SimpleCommand::VERSION
10
+ spec.authors = ["Andrea Pavoni"]
11
+ spec.email = ["andrea.pavoni@gmail.com"]
12
+ spec.summary = %q{Easy way to build and manage commands (aka: service objects)}
13
+ spec.description = %q{Easy way to build and manage commands (aka: service objects)}
14
+ spec.homepage = "http://github.com/nebulab/simple_command"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,11 @@
1
+ class FailCommand
2
+ prepend SimpleCommand
3
+
4
+ def initialize(input)
5
+ @input = input
6
+ end
7
+
8
+ def perform
9
+ add_error(:wrong_math, 'Math is not an opinion') if true
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ class MissedPerformCommand
2
+ prepend SimpleCommand
3
+
4
+ def initialize(input)
5
+ @input = input
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ class SuccessCommand
2
+ prepend SimpleCommand
3
+
4
+ def initialize(input)
5
+ @input = input
6
+ end
7
+
8
+ def perform
9
+ @input * 2
10
+ end
11
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleCommand do
4
+ let(:command) { SuccessCommand.new(2) }
5
+ let(:fail_command) { FailCommand.new(2) }
6
+
7
+ describe '#perform' do
8
+ let(:missed_perform_command) { MissedPerformCommand.new(2) }
9
+
10
+ it 'returns the command object' do
11
+ expect(command.perform).to be_a(SuccessCommand)
12
+ end
13
+
14
+ it 'raises an exception if the method is not defined in the command' do
15
+ expect { missed_perform_command.perform }.to raise_error(SimpleCommand::NotImplementedError)
16
+ end
17
+ end
18
+
19
+ describe '#success?' do
20
+ it 'is true by default' do
21
+ expect(command.perform.success?).to be_truthy
22
+ end
23
+
24
+ it 'is false if something went wrong' do
25
+ expect(fail_command.perform.success?).to be_falsy
26
+ end
27
+
28
+ context 'when perform is not called yet' do
29
+ it 'is false by default' do
30
+ expect(command.success?).to be_falsy
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#result' do
36
+ it 'returns the result of command execution' do
37
+ expect(command.perform.result).to eq(4)
38
+ end
39
+
40
+ context 'when perform is not called yet' do
41
+ it 'returns nil' do
42
+ expect(command.result).to be_nil
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#failure?' do
48
+ it 'is false by default' do
49
+ expect(command.perform.failure?).to be_falsy
50
+ end
51
+
52
+ it 'is true if something went wrong' do
53
+ expect(fail_command.perform.failure?).to be_truthy
54
+ end
55
+
56
+ context 'when perform is not called yet' do
57
+ it 'is false by default' do
58
+ expect(command.failure?).to be_falsy
59
+ end
60
+ end
61
+ end
62
+
63
+ describe '#errors' do
64
+ let(:errors) { command.perform.errors }
65
+
66
+ it 'returns an Hash' do
67
+ expect(errors).to be_a(Hash)
68
+ end
69
+
70
+ context 'with no errors' do
71
+ it 'returns an empty Hash' do
72
+ expect(errors).to be_empty
73
+ end
74
+ end
75
+
76
+ context 'with errors' do
77
+ let(:errors) { fail_command.perform.errors }
78
+
79
+ it 'has a key with error message' do
80
+ expect(errors[:wrong_math]).to_not be_nil
81
+ end
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ end
6
+
7
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
8
+
9
+ require 'simple_command'
10
+
11
+ Dir[File.join(File.dirname(__FILE__), 'factories', '**/*.rb')].each { |f| require f }
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Andrea Pavoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ description: 'Easy way to build and manage commands (aka: service objects)'
56
+ email:
57
+ - andrea.pavoni@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/simple_command.rb
70
+ - lib/simple_command/errors.rb
71
+ - lib/simple_command/version.rb
72
+ - simple_command.gemspec
73
+ - spec/factories/fail_command.rb
74
+ - spec/factories/missed_perform_command.rb
75
+ - spec/factories/success_command.rb
76
+ - spec/simple_command_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: http://github.com/nebulab/simple_command
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '2.0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: 'Easy way to build and manage commands (aka: service objects)'
102
+ test_files:
103
+ - spec/factories/fail_command.rb
104
+ - spec/factories/missed_perform_command.rb
105
+ - spec/factories/success_command.rb
106
+ - spec/simple_command_spec.rb
107
+ - spec/spec_helper.rb