mini_service 0.0.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 521aa07f48ec8e042961b602d497fa8cdc566d86f4260d20be9d6740dcee96cf
4
- data.tar.gz: 72b008ade7364935850e3d53a14ca828e096848542f769d2757818aa2d5411e7
3
+ metadata.gz: 86059adcd037a276ceefc6f754b044e7e6edec6b50ba136a570a577041207a6e
4
+ data.tar.gz: ec513a37a31fc0e5ba05e5607ef35cf104694827742c1ad1e908d37c3549381e
5
5
  SHA512:
6
- metadata.gz: 3b6846fa288706326ff07f4e82e7b51dc645abe70f91bf7f4761f6535f22cd7ae5165b87aada11be9672127aed719dfdbd58074dc8408f7a62439a789dc596d3
7
- data.tar.gz: 57627ad9fb5d33b57b49de5263f6386876e54cc6507d1d5c52cdfb5c5ea0d34b685f8a5386021921c70d13521b00d8decacbf882da14865cf7e07e7e8e1d1b6d
6
+ metadata.gz: 5fc8954c2ba3cf57c71344ebab5301d10e5fcacd26aca2619e6c43db341f28f723b27032519378201f92014dcb710e95edfb0a9310f7af7038957a2ef6189ef2
7
+ data.tar.gz: 662975b79f39def95086679c022b08d5abfa8cc5d18a78efc83292768b7a563509299a7a978199696d8e69d14fa866e0f722fafa4c68b5d5a17113810e4ed906
data/exe/mini_service ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path('../lib', __dir__)
5
+ $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
6
+ require 'mini_service/cli'
7
+
8
+ Signal.trap('INT') do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ MiniService::CLI.start
15
+ rescue MiniService::CLI::Error => e
16
+ puts "ERROR: #{e.message}"
17
+ exit 1
18
+ end
data/lib/mini_service.rb CHANGED
@@ -1,8 +1,16 @@
1
- require 'mini_service/version'
2
- # require 'mini_service/class_methods'
3
- # require 'mini_service/instance_methods'
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
5
+
6
+ require 'thor'
7
+ require 'tty-file'
8
+ require 'tty-prompt'
9
+
4
10
  require 'mini_service/base'
5
- # require 'mini_service/errors'
11
+ require 'mini_service/error'
12
+ require 'mini_service/file_generator'
13
+ require 'mini_service/version'
6
14
 
7
15
  module MiniService
8
16
  end
@@ -1,9 +1,98 @@
1
- require 'mini_service/class_methods.rb'
2
- require 'mini_service/instance_methods'
1
+ # typed: true
2
+ # frozen_string_literal: true
3
3
 
4
4
  module MiniService
5
5
  class Base
6
- extend MiniService::ClassMethods
7
- include MiniService::InstanceMethods
6
+ class ExtraArgumentError < ArgumentError; end
7
+ class MissingArgumentError < ArgumentError; end
8
+
9
+ private_class_method :new
10
+
11
+ class << self
12
+ attr_accessor :required
13
+ attr_accessor :optional
14
+ attr_reader :opts
15
+
16
+ def set_initial_values(req, opt)
17
+ @required ||= req || []
18
+ @optional ||= opt || {}
19
+ nil
20
+ end
21
+
22
+ def arguments(*req, **opt)
23
+ add_required_arguments(req)
24
+ add_optional_arguments(opt)
25
+
26
+ define_readers
27
+ end
28
+
29
+ def add_required_arguments(arguments)
30
+ arguments.each(&method(:add_required_argument))
31
+ end
32
+
33
+ def add_optional_arguments(options)
34
+ optional.merge!(options)
35
+ end
36
+
37
+ def add_required_argument(argument)
38
+ case argument
39
+ when Symbol, String then required << argument
40
+ when Array then add_required_arguments(argument)
41
+ end
42
+ end
43
+
44
+ def define_readers
45
+ required.each { |r| attr_reader r }
46
+ optional.each_key { |r| attr_reader r }
47
+ end
48
+
49
+ def inherited(subclass)
50
+ subclass.set_initial_values(required, optional)
51
+ subclass.arguments
52
+ end
53
+
54
+ def call(**opts)
55
+ @opts = opts
56
+ validate_extra_arguments
57
+ validate_missing_arguments
58
+
59
+ new(**optional.merge(opts)).send :perform
60
+ end
61
+
62
+ def validate_extra_arguments
63
+ if extra_arguments.any?
64
+ raise ExtraArgumentError,
65
+ "Extra arguments: #{extra_arguments.join(', ')}"
66
+ end
67
+ end
68
+
69
+ def validate_missing_arguments
70
+ if missing_arguments.any?
71
+ raise MissingArgumentError,
72
+ "Missing arguments: #{missing_arguments.join(', ')}"
73
+ end
74
+ end
75
+
76
+ def missing_arguments
77
+ (required - opts.keys)
78
+ end
79
+
80
+ def extra_arguments
81
+ (opts.keys - (required + optional.keys))
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def initialize(opts)
88
+ opts.each do |key, value|
89
+ instance_variable_set "@#{key}", value
90
+ end
91
+ end
92
+
93
+ def perform
94
+ raise NoMethodError,
95
+ 'Please define the private instance method perform'
96
+ end
8
97
  end
9
98
  end
@@ -0,0 +1,37 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require 'mini_service'
5
+ require_relative 'version'
6
+ require_relative 'commands/generate'
7
+
8
+ module MiniService
9
+ class CLI < Thor
10
+ class Error < StandardError; end
11
+
12
+ desc 'version', 'mini_service version'
13
+ def version
14
+ puts "v#{MiniService::VERSION}"
15
+ end
16
+ map %w[--version -v] => :version
17
+
18
+ desc 'generate', 'Command description...'
19
+ method_option :help, aliases: '-h', type: :boolean,
20
+ desc: 'Display usage information'
21
+ def generate(name = nil, *arguments)
22
+ generate_execute(arguments, name)
23
+ rescue Error
24
+ nil
25
+ end
26
+
27
+ private
28
+
29
+ def generate_execute(arguments, name)
30
+ if options[:help]
31
+ invoke :help, ['generate']
32
+ else
33
+ MiniService::Commands::Generate.new(name, *arguments).execute
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require 'forwardable'
5
+
6
+ module MiniService
7
+ class Command
8
+ extend Forwardable
9
+
10
+ def_delegators :command, :run
11
+
12
+ def generator
13
+ require 'tty-file'
14
+ TTY::File
15
+ end
16
+
17
+ def prompt(**options)
18
+ require 'tty-prompt'
19
+ TTY::Prompt.new(options)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../../../lib/mini_service/command'
5
+
6
+ module MiniService
7
+ module Commands
8
+ class Generate < MiniService::Command
9
+ class NameError < Error; end
10
+
11
+ def initialize(name, *arguments)
12
+ @name = name
13
+ @arguments = arguments
14
+ ask_name
15
+ rescue TTY::Reader::InputInterrupt
16
+ puts "\nNot OK"
17
+ end
18
+
19
+ attr_reader :name, :arguments
20
+
21
+ def execute(_input: $stdin, output: $stdout)
22
+ raise NameError unless name
23
+
24
+ puts_info output: output
25
+ generator.create_file location, file_content
26
+ output.puts 'OK'
27
+ end
28
+
29
+ def ask_name
30
+ unless name
31
+ @name = prompt.ask 'Name? '
32
+ end
33
+ end
34
+
35
+ def location
36
+ prompt.select 'Dónde?', select_options
37
+ end
38
+
39
+ def file_content
40
+ MiniService::FileGenerator.file name.split('::'), arguments
41
+ end
42
+
43
+ def puts_info(output:)
44
+ output.puts "@name: #{name}"
45
+ output.puts "@arguments: #{arguments.join(', ')}"
46
+ output.puts "parsed_name: #{parsed_name}"
47
+ end
48
+
49
+ def select_options
50
+ options = ['lib/services'] + Dir['**/services']
51
+ options.map! { |item| item + '/' + parsed_name + '.rb' }
52
+ options.uniq
53
+ end
54
+
55
+ def parsed_name
56
+ underscore(name)
57
+ end
58
+
59
+ def underscore(camel_cased_word)
60
+ return camel_cased_word unless %r{[A-Z-]|::}.match?(camel_cased_word)
61
+
62
+ subd_word camel_cased_word.to_s.gsub('::', '/')
63
+ end
64
+
65
+ def subd_word(word)
66
+ word.gsub!(%r{([A-Z\d]+)([A-Z][a-z])}, '\1_\2')
67
+ word.gsub!(%r{([a-z\d])([A-Z])}, '\1_\2')
68
+ word.tr!('-', '_')
69
+ word.downcase!
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,7 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
4
+ module MiniService
5
+ class Error < StandardError
6
+ end
7
+ end
@@ -0,0 +1,100 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ module Shift
5
+ refine String do
6
+ def shift(separator: "\n", shift: 2)
7
+ split(separator).map! do |line|
8
+ (' ' * shift) + line unless line.size.zero?
9
+ end.join(separator)
10
+ end
11
+ end
12
+ end
13
+
14
+ module MiniService
15
+ module FileGenerator
16
+ using Shift
17
+
18
+ class << self
19
+ DEFAULT_TEXT = <<~DEFAULT
20
+ private
21
+
22
+ def perform
23
+ # Put your code in here
24
+ return nil
25
+ end
26
+ DEFAULT
27
+
28
+ def file(array, args = [])
29
+ [
30
+ '# frozen_string_literal: true',
31
+ '',
32
+ superduper(array, text_with_arguments(args))
33
+ ].compact.join("\n")
34
+ end
35
+
36
+ def text_with_arguments(args)
37
+ [
38
+ asdf(args) ? arguments(args) : nil,
39
+ asdf(args) ? '' : nil,
40
+ DEFAULT_TEXT
41
+ ].compact.join("\n")
42
+ end
43
+
44
+ def asdf(args)
45
+ args.any? ? arguments(args) : nil
46
+ end
47
+
48
+ def arguments(args)
49
+ "arguments #{args.map(&method(:argument)).join(', ')}"
50
+ end
51
+
52
+ def argument(arg)
53
+ if arg[':']
54
+ arg.gsub(':', ': ')
55
+ else
56
+ ":#{arg}"
57
+ end
58
+ end
59
+
60
+ def superduper(array, text, mode = :class)
61
+ a = array.pop
62
+ return text unless a
63
+
64
+ superduper(array, super_text(a, mode, text), :module)
65
+ end
66
+
67
+ def module_end(name:, text: '', shift: 0)
68
+ [
69
+ "module #{name}".shift(shift: shift),
70
+ shift_text(text: text, shift: shift + 2),
71
+ shift_text(text: 'end', shift: shift)
72
+ ].join("\n") + "\n"
73
+ end
74
+
75
+ def class_end(name:, text: '', shift: 0)
76
+ [
77
+ shift_text(text: "class #{name} < MiniService::Base", shift: shift),
78
+ (text.size.zero? ? nil : shift_text(text: text, shift: shift + 2)),
79
+ shift_text(text: 'end', shift: shift)
80
+ ].compact.join("\n") + "\n"
81
+ end
82
+
83
+ def shift_text(text: '', separator: "\n", shift: 2)
84
+ text.split(separator).map do |line|
85
+ (' ' * shift) + line unless line.size.zero?
86
+ end.join(separator)
87
+ end
88
+
89
+ private
90
+
91
+ def super_text(name, mode, text)
92
+ if mode == :class
93
+ class_end(name: name, text: text)
94
+ else
95
+ module_end(name: name, text: text)
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,3 +1,6 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
1
4
  module MiniService
2
- VERSION = '0.0.1'.freeze
5
+ VERSION = '0.2.0'
3
6
  end
metadata CHANGED
@@ -1,37 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Rodrgo Andrés Contreras Vilina
7
+ - "@vaporyhumo"
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-11 00:00:00.000000000 Z
11
+ date: 2020-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
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
14
+ name: thor
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
32
18
  - !ruby/object:Gem::Version
33
19
  version: '0'
34
- type: :development
20
+ type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
@@ -39,75 +25,56 @@ dependencies:
39
25
  - !ruby/object:Gem::Version
40
26
  version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
- name: rake
28
+ name: tty-file
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  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
- - - "~>"
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
- version: '3.0'
62
- type: :development
33
+ version: '0'
34
+ type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - "~>"
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
- version: '3.0'
40
+ version: '0'
69
41
  - !ruby/object:Gem::Dependency
70
- name: activesupport
42
+ name: tty-prompt
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
- - - "~>"
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
- version: 4.2.0
47
+ version: '0'
76
48
  type: :runtime
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - "~>"
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
- version: 4.2.0
83
- description:
54
+ version: '0'
55
+ description: Services do one thing and then vanish, POOF!~
84
56
  email:
85
57
  - roanvilina@gmail.com
86
- executables: []
58
+ executables:
59
+ - mini_service
87
60
  extensions: []
88
61
  extra_rdoc_files: []
89
62
  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
63
+ - exe/mini_service
100
64
  - lib/mini_service.rb
101
65
  - 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
66
+ - lib/mini_service/cli.rb
67
+ - lib/mini_service/command.rb
68
+ - lib/mini_service/commands/generate.rb
69
+ - lib/mini_service/error.rb
70
+ - lib/mini_service/file_generator.rb
105
71
  - lib/mini_service/version.rb
106
- - mini_service.gemspec
107
- homepage: https://github.com/roanvilina/mini_service
108
- licenses:
109
- - MIT
110
- metadata: {}
72
+ homepage: https://www.github.com/vaporyhumo/mini_service
73
+ licenses: []
74
+ metadata:
75
+ homepage_uri: https://www.github.com/vaporyhumo/mini_service
76
+ source_code_uri: https://www.github.com/vaporyhumo/mini_service
77
+ changelog_uri: https://github.com/vaporyhumo/mini_service/blob/master/CHANGELOG.md
111
78
  post_install_message:
112
79
  rdoc_options: []
113
80
  require_paths:
@@ -116,15 +83,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
83
  requirements:
117
84
  - - ">="
118
85
  - !ruby/object:Gem::Version
119
- version: '0'
86
+ version: 2.4.0
120
87
  required_rubygems_version: !ruby/object:Gem::Requirement
121
88
  requirements:
122
89
  - - ">="
123
90
  - !ruby/object:Gem::Version
124
91
  version: '0'
125
92
  requirements: []
126
- rubygems_version: 3.0.1
93
+ rubygems_version: 3.1.2
127
94
  signing_key:
128
95
  specification_version: 4
129
- summary: Provides a simple service prototype class.
96
+ summary: A base class for services.
130
97
  test_files: []
data/.gitignore DELETED
@@ -1,13 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
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 DELETED
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in mini_service.gemspec
4
- gemspec
data/Gemfile.lock DELETED
@@ -1,50 +0,0 @@
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 DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,126 +0,0 @@
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_reqs %i[a1 a2]
13
- mini_lets %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
-
28
- ```
29
- Instance variables will be available for each key-value pair
30
- so you can use `@a1` inside perform if `a1` is given in the `args` hash
31
-
32
- Adding `mini_reqs` will raise an error if you miss `reqd` arguments
33
-
34
- Adding `mini_lets` will raise an error if you add arguments not `letd` or `reqd`
35
-
36
- **ALWAYS** pass arguments in a hash.
37
-
38
- ## Usage
39
- You can create your own services by inheriting from `MiniService::Base` class.
40
-
41
- Your `MiniServices` should be used via their `call` method, providing arguments
42
- in a hash, like
43
- `ExampleService.call(a1: 1, a2: 2)` or `ExampleService.call(args)` with args
44
- built elsewhere.
45
-
46
- This will automatically initialize your service with one `@instance_variable`
47
- for each `key-value` pair
48
- you've given.
49
-
50
- So for example a service like this:
51
- ```ruby
52
- class ExampleService < MiniService::Base
53
- private
54
-
55
- def perform
56
- # TODO: put your service logic in here~!!
57
- end
58
- end
59
- ```
60
-
61
- when called like this: `ExampleService.call(ex: true, ample: 1)`, would have
62
- both `@ex = true` and
63
- `@ample = 1` set.
64
-
65
- The `perform` method you see in the example, is the only method that will be
66
- run when you `call` a
67
- `MiniService`; that is, all your logic should be put there, and the final
68
- result should be `return`ed as the
69
- service's response.
70
-
71
- Also, `MiniServices` provide a nice way of declaring which arguments should be
72
- required and let through, raising errors when this conditions are not met.
73
- You can declare them using `mini_reqs` and `mini_lets` methods on your class'
74
- definition, giving them each an array of symbols.
75
-
76
- ```ruby
77
- class ExampleService < MiniService::Base
78
- mini_reqs %i[a1 a2]
79
- mini_lets %i[a3 a4]
80
-
81
- private
82
-
83
- ···
84
-
85
- end
86
- ```
87
-
88
- So it would raise `MissingArguments` or `UnallowedArguments` if you miss `a1`
89
- or add `a5` respectively
90
-
91
- ## Installation
92
- Add this line to your application's Gemfile:
93
-
94
- ```ruby
95
- gem 'mini-service'
96
- ```
97
-
98
- And then execute:
99
- ```bash
100
- $ bundle
101
- ```
102
-
103
- Or install it yourself as:
104
- ```bash
105
- $ gem install mini-service
106
- ```
107
-
108
- ## Contributing
109
- If you wan't to contribute you can contact me via github or directly fork the
110
- repository and then make a
111
- pull request.
112
- For now, I'll only accept improvements on the code or functionality directly
113
- mentioned in the Next Changes
114
- section.
115
-
116
- ## Next Changes
117
- Adding a simple generator for the service, that works by using
118
- `rails generate mini_service YourServiceName`
119
- to create a template file inside your app/services
120
- directory with the provided name.
121
-
122
- Also, testing the whole service.
123
-
124
- ## License
125
- The gem is available as open source under the terms of the
126
- [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile DELETED
@@ -1,6 +0,0 @@
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 DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,8 +0,0 @@
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
@@ -1,20 +0,0 @@
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
@@ -1,5 +0,0 @@
1
- module MiniService
2
- class MissingArgumentsError < StandardError; end
3
- class UnallowedArgumentsError < StandardError; end
4
- class ArgumentsNotHashError < StandardError; end
5
- end
@@ -1,53 +0,0 @@
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
data/mini_service.gemspec DELETED
@@ -1,32 +0,0 @@
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.'
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