operate 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d93185f6ff1d022b5511ba6f6fbd2e815e27eaf1
4
+ data.tar.gz: d0ba09ef7bb5723dc5e0302847dd0c635c28191d
5
+ SHA512:
6
+ metadata.gz: ee4756b459c0db03be97b7e581ad4e7d27fbc9202bc5e661f393f55f806f66cedd489ea9d25b4ec89ad91b5f0f56c6cdc48bcfd4d627e04c0c5b8fcc7ccee943
7
+ data.tar.gz: 8daae04166fe135d2f4e70b3d37946bcbb24291834a91371b87d44cc55e999f0f5b87d43379ad115da0840e583376f4a5825405072b29179e3c87edc1ccb8f2d
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -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 tomichj@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 operate.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Justin Tomich
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.
@@ -0,0 +1,127 @@
1
+ # Operate
2
+
3
+ Operate is a gem to help create [service objects].
4
+
5
+ Use Operate to __remove all business logic from your controller and model__, subsuming it in an Operate-based
6
+ class that represents your processes. Examples might be: a user addition, a post addition, or adding a comment.
7
+
8
+ Service objects can out factor behavior that would bloat models or controllers, and is a useful step to patterns
9
+ like Strategy and Command.
10
+
11
+ Operate is in the very earliest stages of development. Additional features will be added. The core API
12
+ exposed via `Operate::Command` is solid and no changes are in the words.
13
+
14
+
15
+ ## Dependencies
16
+
17
+ Operate requires:
18
+ * `ActiveRecord` 4.2 or greater
19
+ * `ActiveSupport` 4.2 or greater
20
+
21
+ It's not required, but a form object library like [Reform] is recommended. Reform is used in the examples below.
22
+
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'operate'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install operate
39
+
40
+
41
+ ## Usage
42
+
43
+ Just `include Operate::Command` in your class. Operate's api provides:
44
+
45
+ Methods used in your service class:
46
+ * `self#call(*args, &block)` will initialize your service class with *args and invoke #call
47
+ * `#broadcast(:event, *args)` that will broadcast an event to a subscriber
48
+ * `#transaction(&block)` that wraps a block inside a transaction
49
+
50
+ Methods used by clients of your service class:
51
+ * `#on(*events, &block)` that subscribe to an event or events, and provide a block to handle that event
52
+
53
+
54
+ An example service:
55
+
56
+ ```ruby
57
+ class UserAddition
58
+ include Operate::Command
59
+
60
+ def initialize(form, params)
61
+ @form = form
62
+ @params = params
63
+ end
64
+
65
+ def call
66
+ return broadcast(:invalid) unless @form.validate(@params)
67
+
68
+ transaction do
69
+ create_user
70
+ audit_trail
71
+ welcome_user
72
+ end
73
+
74
+ broadcast(:ok)
75
+ end
76
+
77
+ def create_user
78
+ # ...
79
+ end
80
+
81
+ def audit_trail
82
+ # ...
83
+ end
84
+
85
+ def welcome_user
86
+ # ...
87
+ end
88
+ end
89
+ ```
90
+
91
+ And your controller:
92
+
93
+ ```ruby
94
+ class UserController < ApplicationController
95
+ def create
96
+ @form = UserForm.new(params) # a simple Reform form object
97
+ UserAddition.call(@form) do
98
+ on(:ok) { redirect_to dashboard_path }
99
+ on(:invalid) { render :new }
100
+ end
101
+ end
102
+ end
103
+ ```
104
+
105
+
106
+ ## Credit
107
+
108
+ The core of Operate is based on [rectify] and [wisper], and would not exist without these fine projects.
109
+ Both rectify and wisper are excellent gems, they just provide more functionality than I require, and with
110
+ some philosophical differences in execution (rectify requires you extend their base class, operate provides mixins).
111
+
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tomichj/operate.
116
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are
117
+ expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
118
+
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
123
+
124
+ [service objects]: https://gist.github.com/blaix/5764401
125
+ [Reform]: http://trailblazer.to/gems/reform/index.html
126
+ [rectify]: https://github.com/andypike/rectify
127
+ [wisper]: https://github.com/krisleech/wisper
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "operate"
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__)
@@ -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,9 @@
1
+ require 'active_support'
2
+ require 'active_model'
3
+ require 'active_record'
4
+
5
+ require 'operate/version'
6
+ require 'operate/pubsub/events'
7
+ require 'operate/pubsub/publisher'
8
+ require 'operate/pubsub/registration'
9
+ require 'operate/command'
@@ -0,0 +1,47 @@
1
+ module Operate
2
+
3
+ #
4
+ # A command-pattern implementation for controller actions, etc.
5
+ #
6
+ # `register` handlers with on().
7
+ # `broadcast` results with broadcast().
8
+ # `transaction` wraps ActiveRecord transactions.
9
+ #
10
+ module Command
11
+ include Operate::Pubsub::Publisher
12
+ extend ActiveSupport::Concern
13
+
14
+ module ClassMethods
15
+
16
+ #
17
+ # Call will initialize the class with *args and invoke `call` with no parameters.
18
+ #
19
+ def call(*args, &block)
20
+ command = new(*args)
21
+ command.evaluate(&block) if block_given?
22
+ command.call
23
+ end
24
+ end
25
+
26
+ def transaction(&block)
27
+ ActiveRecord::Base.transaction(&block) if block_given?
28
+ end
29
+
30
+ def evaluate(&block)
31
+ @caller = eval('self', block.binding)
32
+ instance_eval(&block)
33
+ end
34
+
35
+ def method_missing(method_name, *args, &block)
36
+ if @caller.respond_to?(method_name, true)
37
+ @caller.send(method_name, *args, &block)
38
+ else
39
+ super
40
+ end
41
+ end
42
+
43
+ def respond_to_missing?(method_name, include_private = false)
44
+ @caller.respond_to?(method_name, include_private)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ module Operate
2
+ module Pubsub #:nodoc:
3
+ # Describes allowed events
4
+ #
5
+ # Duck-types the argument to quack like array of strings
6
+ # when responding to the {#include?} method call.
7
+ class Events
8
+ # Initialize with a list of events.
9
+ #
10
+ # @param [NilClass, String, Symbol, Array, Regexp] list
11
+ def initialize(list)
12
+ @list = list
13
+ end
14
+
15
+ # Check if given event is included in the 'list' of events.
16
+ #
17
+ # @param [#to_s] event
18
+ #
19
+ # @return [Boolean]
20
+ def include?(event)
21
+ appropriate_method.call(event.to_s)
22
+ end
23
+
24
+ private
25
+
26
+ # Different event types and their corresponding matching method.
27
+ def methods
28
+ {
29
+ NilClass => ->(_event) { true },
30
+ String => ->(event) { list == event },
31
+ Symbol => ->(event) { list.to_s == event },
32
+ Enumerable => ->(event) { list.map(&:to_s).include? event },
33
+ Regexp => ->(event) { list.match(event) || false }
34
+ }
35
+ end
36
+
37
+ attr_reader :list
38
+
39
+ def appropriate_method
40
+ @appropriate_method ||= methods[recognized_type]
41
+ end
42
+
43
+ def recognized_type
44
+ methods.keys.detect(&list.method(:is_a?)) || type_not_recognized
45
+ end
46
+
47
+ def type_not_recognized
48
+ raise(ArgumentError, "#{list.class} not supported for `on` argument")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_support/notifications'
2
+
3
+ module Operate
4
+ module Pubsub
5
+
6
+ #
7
+ # A Command uses Publisher to register event handlers and broadcast to them.
8
+ #
9
+ module Publisher
10
+
11
+ # Subscribe a block
12
+ #
13
+ # @example
14
+ # my_publisher.on(:order_created) { |args| ... }
15
+ #
16
+ # @return [self]
17
+ def on(*events, &block)
18
+ raise ArgumentError, 'must give at least one event' if events.empty?
19
+ raise ArgumentError, 'must pass a block' unless block
20
+ registrations << Operate::Pubsub::Registration.new(block, on: events)
21
+ self
22
+ end
23
+
24
+ # Broadcasts an event
25
+ #
26
+ # @example
27
+ # def call
28
+ # # ...
29
+ # broadcast(:finished)
30
+ # end
31
+ #
32
+ # @return [self]
33
+ def broadcast(event, *args)
34
+ registrations.each do |registration|
35
+ registration.broadcast(clean_event(event), *args)
36
+ end
37
+ self
38
+ end
39
+
40
+ private
41
+
42
+ def registrations
43
+ @registrations ||= Set.new
44
+ end
45
+
46
+ def clean_event(event)
47
+ event.to_s.gsub('-', '_')
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ module Operate
2
+ module Pubsub
3
+ #
4
+ # Registers a listener for an event(s).
5
+ #
6
+ class Registration
7
+ attr_reader :on, :listener
8
+
9
+ def initialize(listener, options)
10
+ @listener = listener
11
+ @on = Operate::Pubsub::Events.new options[:on]
12
+ end
13
+
14
+ def broadcast(event, *args)
15
+ listener.call(*args) if should_broadcast?(event)
16
+ end
17
+
18
+ private
19
+
20
+ def should_broadcast?(event)
21
+ on.include? event
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Operate
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'operate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'operate'
8
+ spec.version = Operate::VERSION
9
+ spec.authors = ['Justin Tomich']
10
+ spec.email = ['tomichj@gmail.com']
11
+
12
+ spec.summary = 'An operation object.'
13
+ spec.description = ''
14
+ spec.homepage = 'https://github.com/tomichj/operate'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency 'activesupport', '>= 4.2.0'
33
+ spec.add_dependency 'activerecord', '>= 4.2.0'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.14'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: operate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Tomich
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-27 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.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: ''
84
+ email:
85
+ - tomichj@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/operate.rb
101
+ - lib/operate/command.rb
102
+ - lib/operate/pubsub/events.rb
103
+ - lib/operate/pubsub/publisher.rb
104
+ - lib/operate/pubsub/registration.rb
105
+ - lib/operate/version.rb
106
+ - operate.gemspec
107
+ homepage: https://github.com/tomichj/operate
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.11
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: An operation object.
131
+ test_files: []