civil 0.1.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
+ SHA1:
3
+ metadata.gz: 3d2daf7eb5fdf64d4cab227079bf992c8008c5be
4
+ data.tar.gz: 0a6f4fa9e751ca1ed84fb43e0c2c0ffd31f2fd0b
5
+ SHA512:
6
+ metadata.gz: 9f1be6f7162cc833f4af0997bceac0817997fe9f86497341f16d008f234a6e69c87039e96dbe4c965815ee08f200b66225eaa3ecaadb5387236252fda8f02d99
7
+ data.tar.gz: b03a199806ecb0824cb2c191d2ede1e50778a01f16a554911aa0fc592558b61f4ac89d786c419c4ad07dec1d0410d0134637f1437de4eb21b6c938e17ad5ce03
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /tmp/
11
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ civil
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.3
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
4
+ before_install: gem install bundler -v 1.10.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## 0.1.0 - 2016-11-30
10
+ ### Added
11
+ - Initial commit, imported files that I had originally written for [StandardService](https://github.com/TheTroveApp/standard-service)
12
+ - Civil::Result and Civil::Service classes
13
+ - Specs for Civil::Result and Civil::Service
14
+ - Various bits of tooling for RVM, Rake, RSpec, etc.
15
+ - Documentation and examples in README.md
16
+ - This changelog :-)
17
+
18
+ [Unreleased]: https://github.com/earksiinni/civil/compare/v0.1.0...HEAD
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in civil.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ersin Akinci
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,126 @@
1
+ # Civil
2
+
3
+ An opinionated Ruby framework for standardizing services, their inputs and their outputs. [![Build Status](https://travis-ci.org/earksiinni/civil.svg?branch=master)](https://travis-ci.org/earksiinni/civil)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'civil'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install civil
20
+
21
+ ## Getting started
22
+
23
+ Write your service:
24
+
25
+ ```ruby
26
+ class MyService
27
+ include Civil::Service
28
+
29
+ # Use attribute readers for input parameters
30
+ attr_reader :foo
31
+ attr_accessor :buffer
32
+
33
+ service do
34
+ do_something
35
+ do_something_else
36
+ end
37
+
38
+ def do_something
39
+ self.buffer = foo + 3
40
+ end
41
+
42
+ def do_something_else
43
+ self.buffer = buffer * 2
44
+ end
45
+ end
46
+ ```
47
+
48
+ Invoke your service:
49
+
50
+ ```ruby
51
+ # Pass input parameters in hash
52
+ result = MyService.call foo: 10
53
+
54
+ result.data # 26; result.data == the last return value from inside the service block
55
+ result.conditions # {}; add conditions to indicate non-happy path results (see below)
56
+ result.meta # {}
57
+ result.ideal? # true; because there are no conditions
58
+ result.deviant? # false
59
+ ```
60
+
61
+ ## Usage
62
+
63
+ ### Conditions
64
+
65
+ Civil distinguishes between "errors" and "conditions":
66
+
67
+ - An error results from an unexpected behavior that has nothing to do with your business logic. For example, a user attempting to access an unauthorized resource.
68
+ - A condition results from an expected behavior that deviates from the happy path in your business logic. For example, a user attempting to upload an avatar image that's too big.
69
+
70
+ Civil currently doesn't handle errors. To add a condition, use `#add_condition`:
71
+
72
+ ```ruby
73
+ class SaveAvatarImage
74
+ ...
75
+
76
+ def check_image_size
77
+ add_condition :image_too_big, "The uploaded image must be < 1 MB in size" if image_size_too_big?
78
+ end
79
+ end
80
+ ```
81
+
82
+ Your condition will be available in your result:
83
+
84
+ ```ruby
85
+ result = SaveAvatarImage.call image: image
86
+
87
+ result.conditions # { image_too_big: "The uploaded image must be < 1 MB in size" }
88
+ result.ideal? # false
89
+ result.deviant? # true
90
+ ```
91
+
92
+ Results from service calls that have no conditions are said to be "ideal" whereas those with conditions are said to be "deviant." Two helper methods, `ideal?` and `deviant?`, provide shortcuts for checking for the presence of conditions. Note that we intentionally avoid the commonly used terms "success" and "failure" as these relate to errors, not conditions.
93
+
94
+ ### Metadata
95
+
96
+ Metadata can be returned with any ideal or deviant result by using `add_meta`:
97
+
98
+ ```ruby
99
+ class MetadataExample
100
+ ...
101
+
102
+ def process_doodad
103
+ add_meta :length, 12.5
104
+ end
105
+ end
106
+
107
+ result = MetadataExample.call doodad: doodad
108
+
109
+ result.meta # { length: 12.5 }
110
+ ```
111
+
112
+ ## Development
113
+
114
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+
116
+ 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).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/earksiinni/civil. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
121
+
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
126
+
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 "civil"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/civil.gemspec ADDED
@@ -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 'civil/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "civil"
8
+ spec.version = Civil::VERSION
9
+ spec.authors = ["Ersin Akinci"]
10
+ spec.email = ["ersin.akinci@gmail.com"]
11
+
12
+ spec.summary = %q{Standardize your services, their inputs and their outputs}
13
+ spec.description = %q{Services help keep your business logic clean, but what helps keep your services clean? Civil provides a framework for writing services that all have a standardized structure, inputs and outputs.}
14
+ spec.homepage = "https://github.com/earksiinni/civil"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.5"
25
+ end
@@ -0,0 +1,27 @@
1
+ module Civil
2
+ class Result
3
+ attr_reader :data, :conditions, :meta
4
+
5
+ def initialize(data, conditions = {}, meta = {})
6
+ @data = data
7
+ @conditions = conditions
8
+ @meta = meta
9
+ end
10
+
11
+ def ideal?
12
+ conditions.empty?
13
+ end
14
+
15
+ def deviant?
16
+ !ideal?
17
+ end
18
+
19
+ def to_s
20
+ data.to_s
21
+ end
22
+
23
+ def to_json
24
+ data.to_json
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ module Civil
2
+ module Service
3
+ def self.included(klass)
4
+ class << klass
5
+ # Civil::Service.service
6
+ # Wrapper method, used within service class
7
+ #
8
+ # service do
9
+ # execute_step_1
10
+ # execute_step_2
11
+ # ...
12
+ # end
13
+ def service(&block)
14
+ define_method '_run', &block
15
+ end
16
+
17
+ # Civil::Service.call
18
+ # Initiate service execution and return standardized result
19
+ #
20
+ # result = MyService.call(foo: 1, bar: 'a')
21
+ #
22
+ # if result.ideal?
23
+ # ...
24
+ # else
25
+ # ...
26
+ # end
27
+ def call(params = {})
28
+ service = new(params)
29
+ data = service._run
30
+
31
+ Civil::Result.new(data, service._conditions, service._meta)
32
+ end
33
+ end
34
+ end
35
+
36
+ def initialize(params = {})
37
+ params.to_h.each do |(name, value)|
38
+ instance_variable_set("@#{name}", value)
39
+ end
40
+ end
41
+
42
+ def add_condition(type, desc = "")
43
+ _conditions[type.to_sym] = desc.to_s
44
+ end
45
+
46
+ def add_meta(type, desc = "")
47
+ _meta[type.to_sym] = desc.to_s
48
+ end
49
+
50
+ def _conditions
51
+ @_conditions ||= {}
52
+ end
53
+
54
+ def _meta
55
+ @_meta ||= {}
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Civil
2
+ VERSION = "0.1.0"
3
+ end
data/lib/civil.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "civil/version"
2
+ require "civil/service"
3
+ require "civil/result"
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: civil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ersin Akinci
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-30 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description: Services help keep your business logic clean, but what helps keep your
56
+ services clean? Civil provides a framework for writing services that all have a
57
+ standardized structure, inputs and outputs.
58
+ email:
59
+ - ersin.akinci@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".ruby-gemset"
67
+ - ".ruby-version"
68
+ - ".travis.yml"
69
+ - CHANGELOG.md
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - civil.gemspec
78
+ - lib/civil.rb
79
+ - lib/civil/result.rb
80
+ - lib/civil/service.rb
81
+ - lib/civil/version.rb
82
+ homepage: https://github.com/earksiinni/civil
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Standardize your services, their inputs and their outputs
106
+ test_files: []