operatic 0.2.0

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
+ SHA256:
3
+ metadata.gz: f5f46398654b0cda4099550dc86d85e82833fa9671722f901dfeb09a0c9ef6e5
4
+ data.tar.gz: c5098edb779caedbefc2ddf39861ffd6e005315f5928da73b5eaaa04046c68fc
5
+ SHA512:
6
+ metadata.gz: 93c89546ab8bc0a51fd1e58e059a5ec5da145443fdffadc4072e8af304fad195d1034f31b37a446870fe7e230292d7a40bf0168cfc1cc536ceab4e7b251907fb
7
+ data.tar.gz: 8a14b3e513e2416c55fc768612f0445697da7dabd331594d651c88f8da42c866db3bef42933d22ba44b3ef29b37dc5a6aa77b00af93360b7738c1563d32280c3
@@ -0,0 +1,31 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ rspec:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby:
11
+ - '2.5.x'
12
+ - '2.6.x'
13
+
14
+ name: Ruby ${{ matrix.ruby }} RSpec
15
+
16
+ steps:
17
+ - uses: actions/checkout@v1
18
+
19
+ - name: Set up Ruby
20
+ uses: actions/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby }}
23
+
24
+ - name: Install latest bundler
25
+ run: gem install bundler
26
+
27
+ - name: Install operatic dependencies
28
+ run: bundle install --jobs 4
29
+
30
+ - name: Run RSpec
31
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
12
+
13
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at spideryoung@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in operatic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ben Pickles
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Operatic
2
+
3
+ [![GitHub Actions status](https://github.com/benpickles/operatic/workflows/Ruby/badge.svg)](https://github.com/benpickles/operatic)
4
+
5
+ ## Installation
6
+
7
+ Add Operatic to your application's Gemfile and run `bundle install`.
8
+
9
+ ```ruby
10
+ gem 'operatic'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ An Operatic class encapsulates an operation and communicates the status of the operation via its result object. As well as being either a `#success?` or a `#failure?` further data can be attached via `#success!`, `#failure!` or convenience accessors.
16
+
17
+ ```ruby
18
+ class SayHello
19
+ include Operatic
20
+
21
+ # Readers for attributes passed via `.call`.
22
+ attr_reader :name
23
+
24
+ # Declare convenience accessors on the result.
25
+ result :message
26
+
27
+ def call
28
+ # Exit the method and mark the result as a failure.
29
+ return failure! unless name
30
+
31
+ # Mark the result as a success and attach further data.
32
+ success!(message: "Hello #{name}")
33
+ end
34
+ end
35
+
36
+ result = SayHello.call(name: 'Dave')
37
+ result.success? # => true
38
+ result.message # => "Hello Dave"
39
+ result.to_hash # => {:message=>"Hello Dave"}
40
+
41
+ result = SayHello.call
42
+ result.failure? # => true
43
+ result.success? # => false
44
+ result.message # => nil
45
+ result.to_hash # => {}
46
+ ```
47
+
48
+ A Rails controller might use Operatic like this:
49
+
50
+ ```ruby
51
+ class HellosController < ApplicationController
52
+ def create
53
+ result = SayHello.call(name: params[:name])
54
+
55
+ if result.success?
56
+ render plain: result.message
57
+ else
58
+ render :new
59
+ end
60
+ end
61
+ end
62
+ ```
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
67
+
68
+ ## Code of Conduct
69
+
70
+ Everyone interacting in the Operatic project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/benpickles/operatic/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'operatic'
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
data/lib/operatic.rb ADDED
@@ -0,0 +1,116 @@
1
+ require 'operatic/errors'
2
+ require 'operatic/result'
3
+ require 'operatic/version'
4
+
5
+ module Operatic
6
+ # @!visibility private
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ # The main way of calling an operation.
13
+ #
14
+ # The class is instantiated with supplied +attrs+ and calls {Operatic#call}
15
+ # returning a frozen {Result} instance.
16
+ #
17
+ # @param attrs [Hash<Symbol, anything>] an optional hash of key/values to
18
+ # to the result. The class must have corresponding +attr_reader+s
19
+ #
20
+ # @return a [Result] which is fro
21
+ def call(attrs = nil)
22
+ new(attrs)
23
+ .tap(&:call)
24
+ .result
25
+ .freeze
26
+ end
27
+
28
+ # Calls {#call} but raises {FailureError} if the returned {Result} is a
29
+ # {Result#failure?} - useful for things like background jobs, rake tasks,
30
+ # test setups, etc.
31
+ #
32
+ # @return [Result]
33
+ def call!(attrs = nil)
34
+ call(attrs).tap { |result|
35
+ raise FailureError if result.failure?
36
+ }
37
+ end
38
+
39
+ # Define a {Result} subclass with named accessors specific to the class via
40
+ # {Result.generate}.
41
+ #
42
+ # @example
43
+ # class SayHello
44
+ # include Operatic
45
+ #
46
+ # attr_reader :name
47
+ #
48
+ # result :message
49
+ #
50
+ # def call
51
+ # success!(message: "Hello #{name}")
52
+ # end
53
+ # end
54
+ #
55
+ # result = SayHello.call(name: 'Dave')
56
+ # result.success? # => true
57
+ # result.message # => "Hello Dave"
58
+ def result(*args)
59
+ @result_class = Result.generate(*args)
60
+ end
61
+
62
+ def result_class
63
+ @result_class || Result
64
+ end
65
+ end
66
+
67
+ # An instance of {Result} or a subclass generated by {ClassMethods#result}.
68
+ #
69
+ # @return [Result]
70
+ attr_reader :result
71
+
72
+ def initialize(attrs = nil)
73
+ @result = self.class.result_class.new
74
+
75
+ attrs.each do |key, value|
76
+ instance_variable_set("@#{key}", value)
77
+ end if attrs
78
+ end
79
+
80
+ # Override this method with your implementation. Use {#success!} or
81
+ # {#failure!} methods to communicate the {#result}'s status and to attach
82
+ # data it. Define convenience result accessors with {ClassMethods#result}.
83
+ #
84
+ # @example
85
+ # class SayHello
86
+ # include Operatic
87
+ #
88
+ # attr_reader :name
89
+ #
90
+ # def call
91
+ # return failure! unless name
92
+ # success!(message: "Hello #{name}")
93
+ # end
94
+ # end
95
+ #
96
+ # result = SayHello.call(name: 'Dave')
97
+ # result.success? # => true
98
+ # result.to_hash # => {:message=>"Hello Dave"}
99
+ #
100
+ # result = SayHello.call
101
+ # result.failure? # => true
102
+ # result.success? # => false
103
+ # result.to_hash # => {}
104
+ def call
105
+ end
106
+
107
+ # Convenience shortcut to the operation's {Result#failure!}.
108
+ def failure!(data = nil)
109
+ result.failure!(data)
110
+ end
111
+
112
+ # Convenience shortcut to the operation's {Result#success!}.
113
+ def success!(data = nil)
114
+ result.success!(data)
115
+ end
116
+ end
@@ -0,0 +1,4 @@
1
+ module Operatic
2
+ OperaticError = Class.new(StandardError)
3
+ FailureError = Class.new(OperaticError)
4
+ end
@@ -0,0 +1,87 @@
1
+ module Operatic
2
+ class Result
3
+ # Generate a subclass of {Result} with named +attrs+ accessors. This
4
+ # wouldn't normally be called directly, see {ClassMethods#result} for
5
+ # example usage.
6
+ #
7
+ # @param attrs [Array<Symbol>] a list of accessors to the result's data.
8
+ def self.generate(*attrs)
9
+ Class.new(self) do
10
+ attrs.each do |name|
11
+ define_method name do
12
+ @data[name]
13
+ end
14
+
15
+ define_method "#{name}=" do |value|
16
+ @data[name] = value
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @data = {}
24
+ @success = true
25
+ end
26
+
27
+ # Mark the result as a failure, optionally attach data, and freeze the
28
+ # object so it cannot be modified further.
29
+ #
30
+ # *Note*: After calling this method calling {#success!} or {#failure!}
31
+ # again will raise a +FrozenError+.
32
+ #
33
+ # @param data [Hash<Symbol, anything>] an optional hash of data to attach
34
+ # to the result.
35
+ def failure!(data = nil)
36
+ set_data(data) if data
37
+ @success = false
38
+ freeze
39
+ end
40
+
41
+ def failure?
42
+ !@success
43
+ end
44
+
45
+ def freeze
46
+ @data.freeze
47
+ super
48
+ end
49
+
50
+ # Mark the result as a success, optionally attach data, and freeze the
51
+ # object so it cannot be modified further.
52
+ #
53
+ # Calling this is not strictly necessary as a result defaults to being a
54
+ # success, but it's a convenient means of attaching data and of indicating
55
+ # intent in the consuming code.
56
+ #
57
+ # *Note*: After calling this method calling {#success!} or {#failure!}
58
+ # again will raise a +FrozenError+.
59
+ #
60
+ # @param data [Hash<Symbol, anything>] an optional hash of data to attach
61
+ # to the result.
62
+ def success!(data = nil)
63
+ set_data(data) if data
64
+ @success = true
65
+ freeze
66
+ end
67
+
68
+ def success?
69
+ @success
70
+ end
71
+
72
+ # Returns the full hash of data attached to the result via {#success!},
73
+ # {#failure!}, or convenience accessors added with {.generate}.
74
+ #
75
+ # @return [Hash]
76
+ def to_hash
77
+ @data
78
+ end
79
+
80
+ private
81
+ def set_data(data)
82
+ data.each do |key, value|
83
+ @data[key] = value
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Operatic
2
+ VERSION = '0.2.0'
3
+ end
data/operatic.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'operatic/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'operatic'
7
+ spec.version = Operatic::VERSION
8
+ spec.authors = ['Ben Pickles']
9
+ spec.email = ['spideryoung@gmail.com']
10
+
11
+ spec.summary = 'Operations'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/benpickles/operatic'
14
+ spec.license = 'MIT'
15
+
16
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ # spec.metadata['source_code_uri'] = 'TODO: Put your gem's public repo URL here.'
20
+ # spec.metadata['changelog_uri'] = 'TODO: Put your gem's CHANGELOG.md URL here.'
21
+
22
+ spec.required_ruby_version = '>= 2.5.0'
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: operatic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Pickles
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-23 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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
+ description: ''
56
+ email:
57
+ - spideryoung@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/ruby.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/operatic.rb
73
+ - lib/operatic/errors.rb
74
+ - lib/operatic/result.rb
75
+ - lib/operatic/version.rb
76
+ - operatic.gemspec
77
+ homepage: https://github.com/benpickles/operatic
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/benpickles/operatic
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.5.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Operations
101
+ test_files: []