polist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d15ac52b24c1883fb0451e9fbe6f9b2d39ae90b
4
+ data.tar.gz: 7f25436cea33059f9eb79f81dc54e34ddade7631
5
+ SHA512:
6
+ metadata.gz: d6d28e8018957ebdab190abba767b3cd08872478337a5a839190335be2f1eb89c9ac8c4e8f6b11d06f292710b5901e21acd294a51167ccf00364484a03824c00
7
+ data.tar.gz: 122c0fe7ac27d0c2153f4a74dcf5f191b9b545534c25dfad6b45910bd17a4f69ee1b003cebbdd6ea4bf72489b37cf5c52d0823dff66094e806692624d652f4a0
@@ -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,206 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ TargetRubyVersion: 2.4
4
+ Exclude:
5
+ - polist.gemspec
6
+
7
+
8
+ # Layout
9
+
10
+ Layout/AlignParameters:
11
+ Enabled: true
12
+
13
+ # Layout/MultilineMethodCallIndentation:
14
+ # Enabled: false
15
+
16
+ # Layout/MultilineOperationIndentation:
17
+ # Enabled: false
18
+
19
+ Layout/SpaceInLambdaLiteral:
20
+ EnforcedStyle: require_space
21
+
22
+ Layout/SpaceInsideBrackets:
23
+ Enabled: false
24
+
25
+ Layout/IndentArray:
26
+ EnforcedStyle: consistent
27
+
28
+ Layout/IndentHash:
29
+ EnforcedStyle: consistent
30
+
31
+ # Lint
32
+
33
+ Lint/AmbiguousBlockAssociation:
34
+ Enabled: false
35
+
36
+ Lint/AmbiguousOperator:
37
+ Enabled: false
38
+
39
+ Lint/NonLocalExitFromIterator:
40
+ Enabled: false
41
+
42
+ Lint/HandleExceptions:
43
+ Enabled: false
44
+
45
+ Lint/RescueType:
46
+ Enabled: true
47
+
48
+ Lint/ShadowingOuterLocalVariable:
49
+ Enabled: false
50
+
51
+ Lint/ScriptPermission:
52
+ Enabled: true
53
+
54
+ # Metrics
55
+
56
+ # Metrics/AbcSize:
57
+ # Enabled: false
58
+
59
+ Metrics/BlockLength:
60
+ Exclude:
61
+ - spec/**/*
62
+
63
+ # Metrics/ClassLength:
64
+ # Enabled: false
65
+
66
+ # Metrics/CyclomaticComplexity:
67
+ # Enabled: false
68
+
69
+ Metrics/LineLength:
70
+ Max: 100
71
+
72
+ Metrics/MethodLength:
73
+ Max: 20
74
+
75
+ Metrics/PerceivedComplexity:
76
+ Enabled: false
77
+
78
+ # Performance
79
+
80
+ Performance/Caller:
81
+ Enabled: true
82
+
83
+ Performance/Casecmp:
84
+ Enabled: false
85
+
86
+ # Security
87
+
88
+ Security/YAMLLoad:
89
+ Enabled: false
90
+
91
+ # Style
92
+
93
+ Style/AccessorMethodName:
94
+ # This cop assumes every method is accessor
95
+ Enabled: false
96
+
97
+ Style/AndOr:
98
+ # `do_something and return`
99
+ Enabled: false
100
+
101
+ Style/ClassAndModuleChildren:
102
+ # Enable it in non-rails projects with EnforcedStyle you prefer
103
+ Enabled: false
104
+
105
+ Style/CommentAnnotation:
106
+ # Also, i think that better to use yard's `@todo` and other in ruby code
107
+ Enabled: false
108
+
109
+ Style/ConditionalAssignment:
110
+ Enabled: false
111
+
112
+ Style/Documentation:
113
+ Enabled: false
114
+
115
+ Style/DoubleNegation:
116
+ # Always return booleans in predicates
117
+ Enabled: false
118
+
119
+ Style/EmptyCaseCondition:
120
+ Enabled: false
121
+
122
+ Style/EmptyElse:
123
+ Enabled: false
124
+
125
+ Style/Encoding:
126
+ Enabled: false
127
+
128
+ Style/FormatStringToken:
129
+ Enabled: true
130
+
131
+ Style/IfUnlessModifier:
132
+ Enabled: true
133
+
134
+ Style/GuardClause:
135
+ Enabled: false
136
+
137
+ Style/HashSyntax:
138
+ Exclude:
139
+ - Rakefile
140
+
141
+ Style/LambdaCall:
142
+ Enabled: false
143
+
144
+ Style/ModuleFunction:
145
+ Enabled: false
146
+
147
+ Style/MultilineTernaryOperator:
148
+ Enabled: false
149
+
150
+ Style/MultipleComparison:
151
+ Enabled: true
152
+
153
+ Style/NumericLiterals:
154
+ Description: 'Use indent in groups of 3 unlesss you meet project-specific rule'
155
+ Enabled: false
156
+
157
+ Style/ParallelAssignment:
158
+ Enabled: false
159
+
160
+ Style/PerlBackrefs:
161
+ Enabled: true
162
+
163
+ Style/PredicateName:
164
+ NamePrefixBlacklist:
165
+ - is_
166
+
167
+ Style/RaiseArgs:
168
+ Description: 'Use `raise, ErrorClass` in all cases but not when you need ErrorClass constructor'
169
+ Enabled: false
170
+
171
+ Style/RegexpLiteral:
172
+ EnforcedStyle: mixed
173
+ Enabled: true
174
+
175
+ Style/RescueModifier:
176
+ Enabled: false
177
+
178
+
179
+ Style/StringLiterals:
180
+ EnforcedStyle: double_quotes
181
+ ConsistentQuotesInMultiline: true
182
+
183
+ Style/SignalException:
184
+ EnforcedStyle: only_raise
185
+
186
+ Style/SingleLineBlockParams:
187
+ Enabled: false
188
+
189
+ Style/StringLiteralsInInterpolation:
190
+ Enabled: false
191
+
192
+ Style/SymbolArray:
193
+ Enabled: false
194
+
195
+ Style/TrailingCommaInArguments:
196
+ EnforcedStyleForMultiline: comma
197
+
198
+ Style/TrailingCommaInLiteral:
199
+ EnforcedStyleForMultiline: comma
200
+
201
+ Style/TrivialAccessors:
202
+ AllowPredicates: true
203
+ Enabled: false
204
+
205
+ Style/YodaCondition:
206
+ Enabled: false
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in polist.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yuri Smirnov
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,83 @@
1
+ # Polist
2
+
3
+ `Polist::Service` is a simple class designed for creating service classes.
4
+
5
+ ### Basic usage example
6
+ ```
7
+ class MyService < Polist::Service
8
+ def call
9
+ if params[:ok]
10
+ success!(code: :cool)
11
+ else
12
+ fail!(code: :not_cool)
13
+ end
14
+ end
15
+ end
16
+
17
+ service = MyService.run(ok: true)
18
+ service.success? #=> true
19
+ service.response #=> { code: :cool }
20
+
21
+ service = MyService.run(ok: false)
22
+ service.success? #=> false
23
+ service.response #=> { code: :not_cool }
24
+ ```
25
+
26
+ The only parameter that is passed to the service is called `params` by default. If you want more params, feel free to define your own initializer and call the service accordingly:
27
+
28
+ ```
29
+ class MyService < Polist::Service
30
+ def initialize(a, b, c)
31
+ # ...
32
+ end
33
+ end
34
+
35
+ MyService.call(1, 2, 3)
36
+ ```
37
+
38
+ Unlike `.run`, `.call` will raise `Polist::Service::Failure` exception on failure:
39
+
40
+ ```
41
+ begin
42
+ MyService.call(ok: false)
43
+ rescue Polist::Service::Failure => error
44
+ error.response #=> { code: :not_cool }
45
+ end
46
+ ```
47
+
48
+ Note that `.run` and `.call` are just shortcuts for `MyService.new(...).run` and `MyService.new(...).call` with the only difference that they always return the service instance instead of the result of `#run` or `#call`. Unlike `#call` though, `#run` is not intended to be owerwritten in subclasses.
49
+
50
+ ### Using Form objects
51
+ Sometimes you want to use some kind of params parsing and/or validation, and you can do that with the help of `Polist::Service::Form` class. It uses [tainbox](https://github.com/enthrops/tainbox) gem under the hood.
52
+
53
+ ```
54
+ class MyService < Polist::Service
55
+ class Form < Polist::Service::Form
56
+ attribute :param1, :String
57
+ attribute :param2, :Integer
58
+ attribute :param3, :String, default: "smth"
59
+ attribute :param4, :String
60
+
61
+ validates :param4, presence: true
62
+ end
63
+
64
+ def call
65
+ p form.valid?
66
+ p [form.param1, form.param2, form.param3]
67
+ end
68
+
69
+ # The commented code is just the default implementation and can be simply overwritten
70
+ # private
71
+
72
+ # def form
73
+ # @form ||= self.class::Form.new(form_attributes.to_snake_keys)
74
+ # end
75
+
76
+ # def form_attributes
77
+ # params
78
+ # end
79
+ end
80
+
81
+ MyService.call(param1: "1", param2: "2") # prints false and then ["1", 2, "smth"]
82
+ ```
83
+ The `#form` method is there just for convinience and by default it uses what `#form_attributes` returns as the attributes for the default form class which is the services' `Form` class. You are free to use as many different form classes as you want in your service.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "polist"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "polist/service"
4
+ require "polist/version"
5
+
6
+ module Polist
7
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model/validations"
4
+ require "plissken"
5
+ require "tainbox"
6
+
7
+ class Polist::Service
8
+ class Failure < StandardError
9
+ attr_accessor :response
10
+
11
+ def initialize(response)
12
+ self.response = response
13
+ super
14
+ end
15
+ end
16
+
17
+ class Form
18
+ include Tainbox
19
+ include ActiveModel::Validations
20
+ end
21
+
22
+ attr_accessor :params
23
+
24
+ def self.call(*args)
25
+ build(*args).tap(&:call)
26
+ end
27
+
28
+ def self.run(*args)
29
+ build(*args).tap(&:run)
30
+ end
31
+
32
+ def self.param(*names)
33
+ names.each do |name|
34
+ define_method(name) { params.fetch(name) }
35
+ end
36
+ end
37
+
38
+ def initialize(params)
39
+ self.params = params
40
+ end
41
+
42
+ def call; end # Should be implemented in subclasses
43
+
44
+ def run
45
+ call
46
+ rescue Failure => error
47
+ @response = error.response
48
+ @failure = true
49
+ end
50
+
51
+ def response
52
+ @response ||= {}
53
+ end
54
+
55
+ def failure?
56
+ !!@failure
57
+ end
58
+
59
+ def success?
60
+ !failure?
61
+ end
62
+
63
+ def validate!
64
+ error!(form.errors.to_h.values.first) unless form.valid?
65
+ end
66
+
67
+ private
68
+
69
+ def form
70
+ @form ||= self.class::Form.new(form_attributes.to_snake_keys)
71
+ end
72
+
73
+ def form_attributes
74
+ params
75
+ end
76
+
77
+ def fail!(response = {})
78
+ raise self.class::Failure.new(response)
79
+ end
80
+
81
+ def error!(message = "")
82
+ fail!(error: message)
83
+ end
84
+
85
+ def success!(response = {})
86
+ @response = response
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polist
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "polist/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "polist"
9
+ spec.version = Polist::VERSION
10
+ spec.authors = ["Yuri Smirnov"]
11
+ spec.email = ["tycooon@yandex.ru"]
12
+
13
+ spec.summary = "A gem for creating simple service classes."
14
+ spec.description = "Polist is a gem for creating simple service classes."
15
+ spec.homepage = "https://github.com/umbrellio/polist"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "tainbox"
22
+ spec.add_runtime_dependency "plissken", ">= 0.3"
23
+ spec.add_runtime_dependency "activemodel", ">= 3.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuri Smirnov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tainbox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: plissken
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Polist is a gem for creating simple service classes.
98
+ email:
99
+ - tycooon@yandex.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/polist.rb
115
+ - lib/polist/service.rb
116
+ - lib/polist/version.rb
117
+ - polist.gemspec
118
+ homepage: https://github.com/umbrellio/polist
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.6.12
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: A gem for creating simple service classes.
142
+ test_files: []
143
+ has_rdoc: