responsibility 0.1.0

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: f8b5b58fe0c75ff392eeee922e72ebd697b5e210
4
+ data.tar.gz: cbf0dcaa8696297f15191e0e28304b7221df5afb
5
+ SHA512:
6
+ metadata.gz: 81e650119d4e5f0b442e1a006cae04f43eed2bafd9afd84264c356f63ac7330f8841000be780bcc8bb32986711cadbc5ee72c6eb99f9ac00c1b12790dae40eda
7
+ data.tar.gz: 93cdb5f3448ac67f25b55f1bbf2d9bed19983ca8b9e9445442871c2f6d035db8393c148bafb04bbc4646118150f0d020322bb66eccf311ad1fcbcdb39e533e48
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0.pre.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in responsibility.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ responsibility (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (9.1.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ pry (0.11.1)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ pry-byebug (3.5.0)
17
+ byebug (~> 9.1)
18
+ pry (~> 0.10)
19
+ rake (10.5.0)
20
+ rspec (3.6.0)
21
+ rspec-core (~> 3.6.0)
22
+ rspec-expectations (~> 3.6.0)
23
+ rspec-mocks (~> 3.6.0)
24
+ rspec-core (3.6.0)
25
+ rspec-support (~> 3.6.0)
26
+ rspec-expectations (3.6.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.6.0)
29
+ rspec-mocks (3.6.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.6.0)
32
+ rspec-support (3.6.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.16.a)
39
+ pry-byebug (~> 3.5)
40
+ rake (~> 10.0)
41
+ responsibility!
42
+ rspec (~> 3.0)
43
+
44
+ BUNDLED WITH
45
+ 1.16.0.pre.3
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Responsibility
2
+
3
+ A Responsibility is a class that provides a single piece of functionality,
4
+ as per the Single Responsibility Principle
5
+ [https://en.wikipedia.org/wiki/Single_responsibility_principle]. This class
6
+ provides a single method called "perform", with optional "before" and
7
+ "after" hooks. It also keeps track of any errors set during execution.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'responsibility'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install responsibility
24
+
25
+ ## Usage
26
+
27
+ A simple example could be something like:
28
+
29
+ ```ruby
30
+ class UserSignupService
31
+ include Responsibility
32
+
33
+ def before
34
+ unless user.present? && user.valid?
35
+ errors << "User was not provided or is not valid"
36
+ end
37
+ end
38
+
39
+ def perform
40
+ user.save
41
+ end
42
+
43
+ def after
44
+ UserConfirmationEmailService.perform(email: user.email)
45
+ end
46
+ end
47
+
48
+ user = User.new(
49
+ email: "cerickbrower@gmail.com",
50
+ password: "Wubba Lubba Dub Dub!"
51
+ )
52
+ result = UserSignupService.perform(user: user)
53
+
54
+ ```
55
+
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/erickbrower/responsibility.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ sh "irb -rubygems -I lib -r responsibility.rb"
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "responsibility"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require "responsibility/version"
2
+ require "responsibility/initializer"
3
+ require "responsibility/instance_methods"
4
+ require "responsibility/class_methods"
5
+
6
+ module Responsibility
7
+ def self.included(base)
8
+ base.send(:prepend, Initializer)
9
+ base.include(InstanceMethods)
10
+ base.extend(ClassMethods)
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ module Responsibility
2
+ module ClassMethods
3
+ def before(obj)
4
+ if obj.respond_to?(:before)
5
+ result = obj.before
6
+ if obj.errors.any?
7
+ obj.instance_variable_set("@success", false)
8
+ else
9
+ obj.instance_variable_set("@success", !!result)
10
+ end
11
+ end
12
+ end
13
+
14
+ def after(obj)
15
+ if obj.respond_to?(:after)
16
+ result = obj.after
17
+ if obj.errors.any?
18
+ obj.instance_variable_set("@success", false)
19
+ else
20
+ obj.instance_variable_set("@success", !!result)
21
+ end
22
+ end
23
+ end
24
+
25
+ def perform(*args)
26
+ args = {} if args.is_a?(Array)
27
+ service = new(args)
28
+ before(service)
29
+ if service.success?
30
+ service.perform
31
+ end
32
+ if service.errors.any?
33
+ service.instance_variable_set("@success", false)
34
+ end
35
+ if service.success?
36
+ after(service)
37
+ end
38
+ service
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module Responsibility
2
+ module Initializer
3
+ def initialize(args)
4
+ args[:errors] = []
5
+ args.each do |key, val|
6
+ self.class.send(:attr_accessor, key)
7
+ instance_variable_set("@#{key}", val)
8
+ end
9
+ self.class.send(:attr_reader, :success)
10
+ instance_variable_set("@success", true)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module Responsibility
2
+ module InstanceMethods
3
+ def success?
4
+ success == true
5
+ end
6
+
7
+ def failure?
8
+ success == false
9
+ end
10
+
11
+ def fail!
12
+ success = false
13
+ end
14
+
15
+ def succeed!
16
+ success = true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Responsibility
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "responsibility/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "responsibility"
8
+ spec.version = Responsibility::VERSION
9
+ spec.authors = ["Erick Brower"]
10
+ spec.email = ["cerickbrower@gmail.com"]
11
+
12
+ spec.summary = %q{Provides an interface for creating single purpose objects}
13
+ spec.description = <<~HEREDOC
14
+ A Responsibility is a class that provides a single piece of functionality,
15
+ as per the Single Responsibility Principle
16
+ [https://en.wikipedia.org/wiki/Single_responsibility_principle]. This class
17
+ provides a single method called "perform", with optional "before" and
18
+ "after" hooks.
19
+ HEREDOC
20
+ spec.homepage = "http://erickbrower.com"
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16.a"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "pry-byebug", "~> 3.5"
33
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responsibility
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Erick Brower
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-14 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.16.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.a
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ description: |
70
+ A Responsibility is a class that provides a single piece of functionality,
71
+ as per the Single Responsibility Principle
72
+ [https://en.wikipedia.org/wiki/Single_responsibility_principle]. This class
73
+ provides a single method called "perform", with optional "before" and
74
+ "after" hooks.
75
+ email:
76
+ - cerickbrower@gmail.com
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - ".gitignore"
82
+ - ".rspec"
83
+ - ".travis.yml"
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - README.md
87
+ - Rakefile
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/responsibility.rb
91
+ - lib/responsibility/class_methods.rb
92
+ - lib/responsibility/initializer.rb
93
+ - lib/responsibility/instance_methods.rb
94
+ - lib/responsibility/version.rb
95
+ - responsibility.gemspec
96
+ homepage: http://erickbrower.com
97
+ licenses: []
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.13
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Provides an interface for creating single purpose objects
119
+ test_files: []