simple_tools 0.1.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 +7 -0
- data/Gemfile +6 -0
- data/README.md +111 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/simple_tools/operation.rb +77 -0
- data/lib/simple_tools/version.rb +3 -0
- data/lib/simple_tools.rb +5 -0
- data/simple_tools.gemspec +36 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b41f5e2bc4aae37adeeafe28d802074c6275a2f2ca6f4adb8a45536f3b41e362
|
4
|
+
data.tar.gz: e56eb6bb951d112b7c31367ffc80b5b50e31d26b362047afa84d43ac0e33f1fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc6fa20a9d3e7f7b1d8e2ed06e1ca91d49c02174acdc8a2d7ce93ca375135a521e4d1fa21b4dc7011509f6f60f2e77a4b90928a2e2a669efcab212aa95fb32f9
|
7
|
+
data.tar.gz: 6ee47fd86e0b0e7823ddc01b6d0aa56538782f5afb174693603b8964b8d985f3051d4abda719c3aa61e825d5ee78a9ddca918c21115b5e3496e9d21ac1871769
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# SimpleTools
|
2
|
+
|
3
|
+
Collection of classes which helps to organize code, increase readability and maintainability.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_tools', source: 'https://github.com/isidzukuri/simple_tools'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_tools
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Operation
|
24
|
+
Inspired by trailblaizer operation.
|
25
|
+
|
26
|
+
Idea is to put complex task(operation) in class, split it into commands(steps) and invoke each step one by one.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class BasicOperation < SimpleTools::Operation
|
30
|
+
step :command_one
|
31
|
+
step :command_two
|
32
|
+
|
33
|
+
def command_one
|
34
|
+
p 'run some code'
|
35
|
+
update_context(:first_var, params[:some_value])
|
36
|
+
end
|
37
|
+
|
38
|
+
def command_two
|
39
|
+
context[:first_var]
|
40
|
+
p 'run other code'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
Example of calling operation:
|
45
|
+
```ruby
|
46
|
+
=> result = BasicOperation.call(some_value: 100)
|
47
|
+
# 'run some code'
|
48
|
+
# 'run other code'
|
49
|
+
=> result.success?
|
50
|
+
# true
|
51
|
+
=> result.context[:first_var]
|
52
|
+
# 100
|
53
|
+
```
|
54
|
+
|
55
|
+
`SimpleTools::Operation` respond to `.call` and can receive hash of parameters.
|
56
|
+
Inside operation to share variables between steps use `update_context(:any_key, 'any value')` setter and `context[:any_key]` getter.
|
57
|
+
|
58
|
+
`.call` returns object which respond to `.success?` and returns boolean value.
|
59
|
+
|
60
|
+
Also `.context` method is available which returns setted in operation values.
|
61
|
+
|
62
|
+
If on some step occurs error next steps will not be invoked and current call of operation is considered as failed.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class FailedOperation < SimpleTools::Operation
|
66
|
+
step :command_one
|
67
|
+
step :command_with_error
|
68
|
+
step :command_three
|
69
|
+
|
70
|
+
def command_one
|
71
|
+
p '1'
|
72
|
+
end
|
73
|
+
|
74
|
+
def command_with_error
|
75
|
+
p '2'
|
76
|
+
error!(:name, 'not valid message')
|
77
|
+
# or for multiple errors:
|
78
|
+
errors!({name: ['one more error', 'and another error']})
|
79
|
+
p '2.1'
|
80
|
+
end
|
81
|
+
|
82
|
+
def command_three
|
83
|
+
# will not run because :command_with_error has failed
|
84
|
+
p '3'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
`error!`, `errors!` - add new item(s) to list of errors. It do not raise exception and dont break execution of current method.
|
89
|
+
|
90
|
+
Example of calling failed operation:
|
91
|
+
```ruby
|
92
|
+
=> result = FailedOperation.call
|
93
|
+
# 1
|
94
|
+
# 2
|
95
|
+
# 2.1
|
96
|
+
=> result.success?
|
97
|
+
# false
|
98
|
+
=> result.errors
|
99
|
+
# {name: ['error description', 'one more error', 'and another error']}
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
## Development
|
104
|
+
|
105
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
106
|
+
|
107
|
+
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).
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/isidzukuri/simple_tools.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "simple_tools"
|
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,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleTools
|
4
|
+
class Operation
|
5
|
+
attr_reader :params, :errors, :context
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_reader :steps
|
9
|
+
|
10
|
+
def call(params = {})
|
11
|
+
instance = new(params)
|
12
|
+
instance.call
|
13
|
+
instance
|
14
|
+
end
|
15
|
+
|
16
|
+
def step(key)
|
17
|
+
raise(ArgumentError, 'symbol should be given') unless key.is_a?(Symbol)
|
18
|
+
|
19
|
+
(@steps ||= []).push(key.to_sym)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(params)
|
24
|
+
@errors = {}
|
25
|
+
@context = {}
|
26
|
+
@params = params
|
27
|
+
end
|
28
|
+
|
29
|
+
def call
|
30
|
+
raise(ArgumentError, 'steps are not defined') unless self.class.steps
|
31
|
+
|
32
|
+
self.class.steps.each do |step|
|
33
|
+
public_send(step)
|
34
|
+
break unless success?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def success?
|
39
|
+
errors.empty?
|
40
|
+
# force developer to write descriptions of errors
|
41
|
+
end
|
42
|
+
|
43
|
+
def failure?
|
44
|
+
!success?
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def update_context(key, value)
|
50
|
+
raise(ArgumentError, 'key can not be empty') unless present?(key)
|
51
|
+
|
52
|
+
@context[key] = value
|
53
|
+
end
|
54
|
+
|
55
|
+
def errors!(messages)
|
56
|
+
raise(ArgumentError, 'hash should be given') unless messages.is_a?(Hash)
|
57
|
+
|
58
|
+
messages.each do |key, value|
|
59
|
+
raise(ArgumentError, 'hash with array values should be given') unless value.is_a?(Array)
|
60
|
+
|
61
|
+
(@errors[key.to_sym] ||= []).push(*value)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def error!(key, message)
|
66
|
+
raise(ArgumentError, 'key can not be empty') unless present?(key)
|
67
|
+
|
68
|
+
raise(ArgumentError, 'message can not be empty') unless present?(message)
|
69
|
+
|
70
|
+
(@errors[key.to_sym] ||= []).push(message.to_s)
|
71
|
+
end
|
72
|
+
|
73
|
+
def present?(value)
|
74
|
+
value.respond_to?(:empty?) ? !value.empty? : !!value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/simple_tools.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'simple_tools/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'simple_tools'
|
7
|
+
spec.version = SimpleTools::VERSION
|
8
|
+
spec.authors = ['isidzukuri']
|
9
|
+
spec.email = ['axesigon@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Collection of classes which helps to organize code, increase readability and maintainability.'
|
12
|
+
spec.description = 'Collection of classes which helps to organize code, increase readability and maintainability.'
|
13
|
+
spec.homepage = 'https://github.com/isidzukuri/simple_tools'
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
21
|
+
'public gem pushes.'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
27
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
end
|
29
|
+
spec.bindir = 'exe'
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec'
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- isidzukuri
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-12 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Collection of classes which helps to organize code, increase readability
|
56
|
+
and maintainability.
|
57
|
+
email:
|
58
|
+
- axesigon@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Gemfile
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- bin/console
|
67
|
+
- bin/setup
|
68
|
+
- lib/simple_tools.rb
|
69
|
+
- lib/simple_tools/operation.rb
|
70
|
+
- lib/simple_tools/version.rb
|
71
|
+
- simple_tools.gemspec
|
72
|
+
homepage: https://github.com/isidzukuri/simple_tools
|
73
|
+
licenses: []
|
74
|
+
metadata:
|
75
|
+
allowed_push_host: https://rubygems.org
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.7.4
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Collection of classes which helps to organize code, increase readability
|
96
|
+
and maintainability.
|
97
|
+
test_files: []
|