mini_service 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e819cb6201d5918ccdc7905773d5be0702ffedd68be9f124055213478af8987
4
+ data.tar.gz: d3bd75d54c0e92e3c7c4fa2a41cdcb60228106f627f21a20e5ffa9e8e3d8b11f
5
+ SHA512:
6
+ metadata.gz: 43459572a9839d4ea529b76cb109f665e70b331ef77acf66f51e7040b5fd8de7adfdacad7ddcb338b1a9e0bb7af2302622079f3130792b4e8da6815a44fc2a01
7
+ data.tar.gz: 7a503cb92018de678a4aba44f6a04574ed54081e42d12fcd9c4ede1100e508caaad19ef1e929a4839578ef616bc73a7765e8422aa9837da9a29239fe191a9184
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
+ example.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mini_service.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mini_service (0.1.0)
5
+ activesupport (~> 4.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.11)
11
+ i18n (~> 0.7)
12
+ minitest (~> 5.1)
13
+ thread_safe (~> 0.3, >= 0.3.4)
14
+ tzinfo (~> 1.1)
15
+ concurrent-ruby (1.1.4)
16
+ diff-lcs (1.3)
17
+ i18n (0.9.5)
18
+ concurrent-ruby (~> 1.0)
19
+ irb (1.0.0)
20
+ minitest (5.11.3)
21
+ rake (10.5.0)
22
+ rspec (3.8.0)
23
+ rspec-core (~> 3.8.0)
24
+ rspec-expectations (~> 3.8.0)
25
+ rspec-mocks (~> 3.8.0)
26
+ rspec-core (3.8.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-expectations (3.8.2)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-mocks (3.8.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-support (3.8.0)
35
+ thread_safe (0.3.6)
36
+ tzinfo (1.2.5)
37
+ thread_safe (~> 0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ bundler (~> 2.0)
44
+ irb
45
+ mini_service!
46
+ rake (~> 10.0)
47
+ rspec (~> 3.0)
48
+
49
+ BUNDLED WITH
50
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Rodrgo Andrés Contreras Vilina
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,125 @@
1
+ # MiniService
2
+ MiniServices are a prototype from which to build single-functionality
3
+ minimalistic services, aiming to
4
+ help you correctly modularize and isolate your processes making them easier to
5
+ mantain and less prone to
6
+ errors.
7
+
8
+ ## TL;DR
9
+ Define them like this:
10
+ ```ruby
11
+ class ExampleService < MiniService::Base
12
+ mini_requires %i[a1 a2]
13
+ mini_allows %i[a3 a4]
14
+
15
+ private
16
+
17
+ def perform
18
+ # THIS IS WHAT YOUR SERVICE WILL DO AND RETURN
19
+ end
20
+ end
21
+ ```
22
+
23
+ Call them like this:
24
+ ```ruby
25
+ ExampleService.call(a1: 1, a2: 2) # or
26
+ ExampleService.call(a1: 1, a2: 2, a3: 3, a4: 4)
27
+ # Instance variables will be available for each key-value pair
28
+ # so you can use @a1 inside perform if a1 is given in the args hash
29
+ ```
30
+
31
+ Adding `mini_reqs` will raise an error if you miss `reqd` arguments
32
+
33
+ Adding `mini_lets` will raise an error if you add arguments not `letd` or `reqd`
34
+
35
+ **ALWAYS** pass arguments in a hash.
36
+
37
+ ## Usage
38
+ You can create your own services by inheriting from `MiniService::Base` class.
39
+
40
+ Your `MiniServices` should be used via their `call` method, providing arguments
41
+ in a hash, like
42
+ `ExampleService.call(p1: v1, p2: v2)` or `ExampleService.call(args)` with args
43
+ built elsewhere.
44
+
45
+ This will automatically initialize your service with one `@instance_variable`
46
+ for each `key-value` pair
47
+ you've given.
48
+
49
+ So for example a service like this:
50
+ ```ruby
51
+ class ExampleService < MiniService::Base
52
+ private
53
+
54
+ def perform
55
+ # TODO: put your service logic in here~!!
56
+ end
57
+ end
58
+ ```
59
+
60
+ when called like this: `ExampleService.call(ex: true, ample: 1)`, would have
61
+ both `@ex = true` and
62
+ `@ample = 1` set.
63
+
64
+ The `perform` method you see in the example, is the only method that will be
65
+ run when you `call` a
66
+ `MiniService`; that is, all your logic should be put there, and the final
67
+ result should be `return`ed as the
68
+ service's response.
69
+
70
+ Also, `MiniServices` provide a nice way of declaring which arguments should be
71
+ required and let through, raising errors when this conditions are not met.
72
+ You can declare them using `mini_reqs` and `mini_lets` methods on your class'
73
+ definition, giving them each an array of symbols.
74
+
75
+ ```ruby
76
+ class ExampleService < MiniService::Base
77
+ mini_reqs %i[a1 a2]
78
+ mini_lets %i[a3 a4]
79
+
80
+ private
81
+
82
+ ···
83
+
84
+ end
85
+ ```
86
+
87
+ So it would raise `MissingArguments` or `UnallowedArguments` if you miss `a1`
88
+ or add `a5` respectively
89
+
90
+ ## Installation
91
+ Add this line to your application's Gemfile:
92
+
93
+ ```ruby
94
+ gem 'mini-service'
95
+ ```
96
+
97
+ And then execute:
98
+ ```bash
99
+ $ bundle
100
+ ```
101
+
102
+ Or install it yourself as:
103
+ ```bash
104
+ $ gem install mini-service
105
+ ```
106
+
107
+ ## Contributing
108
+ If you wan't to contribute you can contact me via github or directly fork the
109
+ repository and then make a
110
+ pull request.
111
+ For now, I'll only accept improvements on the code or functionality directly
112
+ mentioned in the Next Changes
113
+ section.
114
+
115
+ ## Next Changes
116
+ Adding a simple generator for the service, that works by using
117
+ `rails generate mini_service YourServiceName`
118
+ to create a template file inside your app/services
119
+ directory with the provided name.
120
+
121
+ Also, testing the whole service.
122
+
123
+ ## License
124
+ The gem is available as open source under the terms of the
125
+ [MIT License](https://opensource.org/licenses/MIT).
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 "mini_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__)
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
@@ -0,0 +1,9 @@
1
+ require 'mini_service/class_methods.rb'
2
+ require 'mini_service/instance_methods'
3
+
4
+ module MiniService
5
+ class Base
6
+ extend MiniService::ClassMethods
7
+ include MiniService::InstanceMethods
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_support'
2
+ module MiniService
3
+ module ClassMethods
4
+ def call(*args)
5
+ raise MiniService::ArgumentsNotHashError unless args[0].is_a?(Hash)
6
+
7
+ new(*args).call
8
+ end
9
+
10
+ def mini_reqs(array)
11
+ mattr_accessor :reqd_args
12
+ self.reqd_args = array
13
+ end
14
+
15
+ def mini_lets(array)
16
+ mattr_accessor :letd_args
17
+ self.letd_args = array
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module MiniService
2
+ class MissingArgumentsError < StandardError; end
3
+ class UnallowedArgumentsError < StandardError; end
4
+ class ArgumentsNotHashError < StandardError; end
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'mini_service/errors'
2
+
3
+ module MiniService
4
+ module InstanceMethods
5
+ def initialize(args)
6
+ instance_variables(args)
7
+ end
8
+
9
+ def call
10
+ perform
11
+ end
12
+
13
+ private
14
+
15
+ def instance_variables(args)
16
+ raise MiniService::MissingArgumentsError if missing_arguments?(args)
17
+
18
+ args.each do |key, value|
19
+ raise MiniService::ExtraArgumentsError unless argument_allowed?(key)
20
+
21
+ instance_variable_set "@#{key}", value
22
+ end
23
+ end
24
+
25
+ def argument_allowed?(key)
26
+ @alwd_args ||= allowed_arguments
27
+ return true if @alwd_args.empty?
28
+
29
+ @alwd_args.include? key
30
+ end
31
+
32
+ def allowed_arguments
33
+ alwd_args = []
34
+ alwd_args << letd_args if letd_args_defined?
35
+ alwd_args << reqd_args if reqd_args_defined?
36
+ alwd_args.flatten
37
+ end
38
+
39
+ def missing_arguments?(args)
40
+ return false unless reqd_args_defined?
41
+
42
+ (reqd_args - args.keys).any?
43
+ end
44
+
45
+ def reqd_args_defined?
46
+ !defined?(reqd_args).nil?
47
+ end
48
+
49
+ def letd_args_defined?
50
+ !defined?(letd_args).nil?
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module MiniService
2
+ VERSION = '0.0.0'.freeze
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'mini_service/version'
2
+ # require 'mini_service/class_methods'
3
+ # require 'mini_service/instance_methods'
4
+ require 'mini_service/base'
5
+ # require 'mini_service/errors'
6
+
7
+ module MiniService
8
+ end
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mini_service/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mini_service'
7
+ spec.version = MiniService::VERSION
8
+ spec.authors = ['Rodrgo Andrés Contreras Vilina']
9
+ spec.email = ['roanvilina@gmail.com']
10
+
11
+ spec.summary = 'Provides a simple service prototype class and its generator.'
12
+ spec.homepage = 'https://github.com/roanvilina/mini_service'
13
+ spec.license = 'MIT'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'irb'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+
31
+ spec.add_dependency 'activesupport', '~> 4.2.0'
32
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodrgo Andrés Contreras Vilina
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-11 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: irb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 4.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 4.2.0
83
+ description:
84
+ email:
85
+ - roanvilina@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/mini_service.rb
101
+ - lib/mini_service/base.rb
102
+ - lib/mini_service/class_methods.rb
103
+ - lib/mini_service/errors.rb
104
+ - lib/mini_service/instance_methods.rb
105
+ - lib/mini_service/version.rb
106
+ - mini_service.gemspec
107
+ homepage: https://github.com/roanvilina/mini_service
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
+ rubygems_version: 3.0.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Provides a simple service prototype class and its generator.
130
+ test_files: []