civil_service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3505ccbce978bbeeea7a494316cd161eee31460207b6b3b2c322315089bd1442
4
+ data.tar.gz: cf203b21b33f1f3b5e87a010bc3c2b8006703ca1cf1b53b1328b107264b21890
5
+ SHA512:
6
+ metadata.gz: 8d99b796b01ecaa22eacf3b4bea961037749c927b798d7f2b153a7114192b6a40b8d1833e014abc2c736a929d2c5250f6ea8e4f2c94445c1df4df97a1fb41ae4
7
+ data.tar.gz: 73885e7a905e506e0f7c65324b370af89ed21f5df44755b40b7b44e414a5ffc75e635b6ab93e767fd910b107a18f66ac6ba20cd8ed3a34e40dca7b99fcc68068
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at natbudin@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
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 civil_service.gemspec
6
+ gemspec
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ civil_service (1.0.0)
5
+ activemodel (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.5)
11
+ activesupport (= 5.1.5)
12
+ activesupport (5.1.5)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ concurrent-ruby (1.0.5)
18
+ i18n (0.9.5)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.11.3)
21
+ rake (10.5.0)
22
+ thread_safe (0.3.6)
23
+ tzinfo (1.2.5)
24
+ thread_safe (~> 0.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.16)
31
+ civil_service!
32
+ minitest (~> 5.0)
33
+ rake (~> 10.0)
34
+
35
+ BUNDLED WITH
36
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 New England Interactive Literature
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # CivilService
2
+
3
+ [![Build Status](https://travis-ci.org/neinteractiveliterature/civil_service.svg?branch=master)](https://travis-ci.org/neinteractiveliterature/civil_service)
4
+
5
+ CivilService is a tiny framework for [service objects in Rails apps](https://hackernoon.com/service-objects-in-ruby-on-rails-and-you-79ca8a1c946e). With CivilService, you can use ActiveModel validations to do pre-flight checks before the service runs, and create your own result object classes to capture the results of complex operations.
6
+
7
+ CivilService was extracted from [Intercode](https://github.com/neinteractiveliterature/intercode), a web app for convention organizers and participants.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'civil_service'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install civil_service
24
+
25
+ ## What CivilService does
26
+
27
+ CivilService::Service is really a pretty tiny class. It does, however, have some opinions that create a potentially-useful abstraction for app developers:
28
+
29
+ * When called, services always return a result object that responds to (at least) `#success?`, `#failure?`, and `#errors`. This lets your code paths that call services be consistent and simple. (If you want to return more information as a result of running the service, it's easy to define a custom result class for your service.)
30
+ * Services include `ActiveModel::Validations` so they can easily do pre-flight checks. That means you can call `my_service.valid?` and `my_service.errors` just like you can for a model, and it also means that the service will fail if it's not valid.
31
+
32
+ ## Basic example
33
+
34
+ Here's a simple service that changes a user's password in a hypothetical Rails app, and sends a
35
+ notification email about it:
36
+
37
+ ```ruby
38
+ class PasswordChangeService < CivilService::Service
39
+ validate :ensure_valid_password
40
+
41
+ attr_reader :user, :new_password
42
+
43
+ def initialize(user:, new_password:)
44
+ @user = user
45
+ @new_password = new_password
46
+ end
47
+
48
+ private
49
+
50
+ def inner_call
51
+ user.update!(password: new_password)
52
+ UserMailer.password_changed(user).deliver_later
53
+ success
54
+ end
55
+
56
+ def ensure_valid
57
+ return if new_password.length >= 8
58
+ errors.add(:base, "Passwords must be at least 8 characters long")
59
+ end
60
+ end
61
+ ```
62
+
63
+ You might call this from a controller action like this:
64
+
65
+ ```ruby
66
+ class UsersController < ApplicationController
67
+ def change_password
68
+ service = PasswordChangeService.new(user: current_user, new_password: params[:password])
69
+ result = service.call
70
+
71
+ if result.success?
72
+ redirect_to root_url, notice: "Your password has been changed."
73
+ else
74
+ flash[:alert] = result.errors.full_messages.join(', ')
75
+ end
76
+ end
77
+ end
78
+ ```
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ 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).
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/neinteractiveliterature/civil_service.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "civil_service"
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,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "civil_service/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "civil_service"
8
+ spec.version = CivilService::VERSION
9
+ spec.authors = ["Nat Budin"]
10
+ spec.email = ["nbudin@patientslikeme.com"]
11
+
12
+ spec.summary = %q{A tiny service object framework for Rails apps}
13
+ spec.description = %q{CivilService provides a base class for your service objects. With CivilService, you can use ActiveModel validations to do pre-flight checks before the service runs, and create your own result object classes to capture the results of complex operations.}
14
+ spec.homepage = "https://github.com/neinteractiveliterature/civil_service"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activemodel", ">= 3.0.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
@@ -0,0 +1,5 @@
1
+ require "civil_service/version"
2
+ require "active_model"
3
+ require "civil_service/result"
4
+ require "civil_service/service_failure"
5
+ require "civil_service/service"
@@ -0,0 +1,20 @@
1
+ class CivilService::Result
2
+ include ActiveModel::Model
3
+ attr_accessor :success, :errors
4
+
5
+ def self.success(attributes = {})
6
+ new(attributes.merge(success: true))
7
+ end
8
+
9
+ def self.failure(attributes = {})
10
+ new(attributes.merge(success: false))
11
+ end
12
+
13
+ def success?
14
+ @success
15
+ end
16
+
17
+ def failure?
18
+ !success?
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ class CivilService::Service
2
+ include ActiveModel::Validations
3
+
4
+ attr_writer :logger
5
+
6
+ class << self
7
+ attr_accessor :validate_manually
8
+ attr_writer :result_class
9
+
10
+ def result_class
11
+ @result_class || CivilService::Result
12
+ end
13
+ end
14
+
15
+ def call
16
+ unless self.class.validate_manually
17
+ return failure(errors) unless valid?
18
+ end
19
+
20
+ inner_call
21
+ end
22
+
23
+ def call!
24
+ unless self.class.validate_manually
25
+ raise CivilService::ServiceFailure.new(self, failure(errors)) unless valid?
26
+ end
27
+
28
+ result = inner_call
29
+ raise CivilService::ServiceFailure.new(self, result) if result.failure?
30
+ result
31
+ end
32
+
33
+ def logger
34
+ @logger || Rails.logger
35
+ end
36
+
37
+ private
38
+
39
+ def success(attributes = {})
40
+ self.class.result_class.success(attributes)
41
+ end
42
+
43
+ def failure(errors, attributes = {})
44
+ self.class.result_class.failure(attributes.merge(errors: errors))
45
+ end
46
+
47
+ def inner_call
48
+ raise 'Service classes are expected to implement #inner_call'
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ class CivilService::ServiceFailure < StandardError
2
+ attr_reader :service, :result
3
+
4
+ def initialize(service, result)
5
+ @service = service
6
+ @result = result
7
+ super("#{service.class.name} failed: #{result.errors.full_messages.join(', ')}")
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module CivilService
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: civil_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nat Budin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: CivilService provides a base class for your service objects. With CivilService,
70
+ you can use ActiveModel validations to do pre-flight checks before the service runs,
71
+ and create your own result object classes to capture the results of complex operations.
72
+ email:
73
+ - nbudin@patientslikeme.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - civil_service.gemspec
89
+ - lib/civil_service.rb
90
+ - lib/civil_service/result.rb
91
+ - lib/civil_service/service.rb
92
+ - lib/civil_service/service_failure.rb
93
+ - lib/civil_service/version.rb
94
+ homepage: https://github.com/neinteractiveliterature/civil_service
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: A tiny service object framework for Rails apps
117
+ test_files: []