neco 0.1.0 → 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: 5b8600b54de50cbf86de418e8ac297f91ca7944829ad55a970c212f7ebe9f3bc
4
- data.tar.gz: 0b96f25e550ce59eff7822f73173da69198c6c6ad2db83fa7b8f0b5c30b9e224
3
+ metadata.gz: '09593673a33bf626933d34caa3e67de31d0b38bbbec9d5d5e8859208453879c3'
4
+ data.tar.gz: a946ab87bdb749357ad6a9d22b45caa3de04d4914feede5f74bdbaedc0d68a8c
5
5
  SHA512:
6
- metadata.gz: '05398284cdd89b706c5c37134fabf33ee97475396555c8a49f264e0595efec4ba80d4ee69b2cc980b107e340e69467d13154cec84e1faffe167e96d5f0ccc9ea'
7
- data.tar.gz: d0a0fe2de63bcb45e152a94112ef5470610bc9e4e18d3a8616bfc0617388e08598e0452e65caa56e5cee9e574206b643fd8143be5f96939c800f8cbbaf00ae42
6
+ metadata.gz: ffc374588f707eb08f6d5a0bd1703c17f13c49e60b956de7032c09491e659db82a9a11795b23f213cb579aa87f38a6279d4f70e750dcab9f48ace4dfda3fbf37
7
+ data.tar.gz: 7cc86cb61a48a396b1ec8f724952d99b486e22e2d62227d57af57dc57f2421b14e7f6c4cf843da62273c277d98bb7924f402ad909a1f6997b8134484d14fc458
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- neco (0.1.0)
4
+ neco (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -18,15 +18,15 @@ GEM
18
18
  coderay (~> 1.1.0)
19
19
  method_source (~> 0.9.0)
20
20
  rainbow (3.0.0)
21
- rake (10.5.0)
22
- rubocop (0.70.0)
21
+ rake (12.3.2)
22
+ rubocop (0.71.0)
23
23
  jaro_winkler (~> 1.5.1)
24
24
  parallel (~> 1.10)
25
25
  parser (>= 2.6)
26
26
  rainbow (>= 2.2.2, < 4.0)
27
27
  ruby-progressbar (~> 1.7)
28
28
  unicode-display_width (>= 1.4.0, < 1.7)
29
- ruby-progressbar (1.10.0)
29
+ ruby-progressbar (1.10.1)
30
30
  unicode-display_width (1.6.0)
31
31
 
32
32
  PLATFORMS
@@ -37,7 +37,7 @@ DEPENDENCIES
37
37
  minitest (~> 5.0)
38
38
  neco!
39
39
  pry (>= 0.12.2)
40
- rake (~> 10.0)
40
+ rake (~> 12.3)
41
41
  rubocop (>= 0.70.0)
42
42
 
43
43
  BUNDLED WITH
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
- # Neco
1
+ [![Build Status](https://travis-ci.com/okuramasafumi/neco.svg?branch=master)](https://travis-ci.com/okuramasafumi/neco)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d011a37ef8ebf1ea2afd/maintainability)](https://codeclimate.com/github/okuramasafumi/neco/maintainability)
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/neco`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+ # Neco
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Neco is a NEo COmmand pattern library for Ruby.
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,7 +23,75 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ Include `Neco::Command` module and define your command with DSLs like below:
27
+
28
+ ```ruby
29
+ class Foo
30
+ include Neco::Command
31
+
32
+ validates do |name:|
33
+ name == 'Tama'
34
+ end
35
+
36
+ main do |name:|
37
+ puts "Hello, #{name}!"
38
+ end
39
+ end
40
+
41
+ Foo.call(name: 'Tama') # => 'Hello, Tama!'
42
+ ```
43
+
44
+ Here, `validates` block defines validations and `main` block defines main logic for the command (simple, right?)
45
+
46
+ You can also compose multiple commands into one command using `Neco::Composition` module. Neco automatically triggers rollbacks when one of the commands raises an exception.
47
+
48
+ ```ruby
49
+ class Command1
50
+ include Neco::Command
51
+
52
+ main do
53
+ set :cat_name, 'Tama'
54
+ end
55
+
56
+ rollback do
57
+ puts 'Rolling back Command1!'
58
+ end
59
+ end
60
+
61
+ class Command2
62
+ include Neco::Command
63
+
64
+ main do |cat_name:|
65
+ puts "Hello, #{cat_name}!"
66
+ end
67
+
68
+ rollback do
69
+ puts 'Rolling back Command2!'
70
+ end
71
+ end
72
+
73
+ class Command3
74
+ include Neco::Command
75
+
76
+ # Unused block argument
77
+ main do |cat_name:|
78
+ raise 'OMG!!!'
79
+ end
80
+ end
81
+
82
+ class Bar
83
+ include Neco::Composition
84
+
85
+ composes Command1, Command2, Command3
86
+ end
87
+
88
+ Bar.call
89
+ # => 'Hello, Tama!'
90
+ # => 'Rolling back Command2!'
91
+ # => 'Rolling back Command1!'
92
+ ```
93
+
94
+ See `examples` directory for details.
26
95
 
27
96
  ## Development
28
97
 
@@ -32,7 +101,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
101
 
33
102
  ## Contributing
34
103
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/neco. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/okuramasafumi/neco. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
105
 
37
106
  ## License
38
107
 
@@ -40,4 +109,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
109
 
41
110
  ## Code of Conduct
42
111
 
43
- Everyone interacting in the Neco project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/neco/blob/master/CODE_OF_CONDUCT.md).
112
+ Everyone interacting in the Neco project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/okuramasafumi/neco/blob/master/CODE_OF_CONDUCT.md).
data/lib/neco/command.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'neco/container'
4
+ require 'neco/result'
4
5
 
5
6
  module Neco
6
7
  # Command module is a basic module of Neco.
@@ -31,7 +32,6 @@ module Neco
31
32
  def call(*args, **params)
32
33
  instance = new(*args, **params)
33
34
  instance.call
34
- true
35
35
  end
36
36
  end
37
37
 
@@ -55,7 +55,12 @@ module Neco
55
55
  return false unless validate
56
56
 
57
57
  main = self.class.instance_variable_get(:@main)
58
- instance_exec(*@args, **@params, &main)
58
+ begin
59
+ instance_exec(*@args, **@params, &main)
60
+ Success.new
61
+ rescue StandardError => e
62
+ Failure.new(exception: e)
63
+ end
59
64
  end
60
65
 
61
66
  def validate
@@ -14,11 +14,13 @@ module Neco
14
14
  def call(*args, **params)
15
15
  @environment.merge!(params)
16
16
  @commands.each do |command|
17
- command.call(*args, **@environment)
18
- @called << command
19
- rescue StandardError
20
- @called.reverse_each(&:revert)
21
- break
17
+ result = command.call(*args, **@environment)
18
+ if result.success?
19
+ @called << command
20
+ else
21
+ @called.reverse_each(&:revert)
22
+ break
23
+ end
22
24
  end
23
25
  end
24
26
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neco
4
+ # Base class of Result
5
+ class Result
6
+ end
7
+
8
+ # Success is returned when the command executed successfully.
9
+ class Success < Result
10
+ def success?
11
+ true
12
+ end
13
+
14
+ def failure?
15
+ false
16
+ end
17
+ end
18
+
19
+ # Failure is returned when the command failed.
20
+ # This class has several extra information because failure has many ways to achieve
21
+ # while success has only one way.
22
+ class Failure < Result
23
+ def initialize(exception:)
24
+ @exception = exception
25
+ end
26
+
27
+ def success?
28
+ false
29
+ end
30
+
31
+ def failure?
32
+ true
33
+ end
34
+
35
+ def message
36
+ @exception.message
37
+ end
38
+ end
39
+ end
data/lib/neco/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Neco
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/neco.gemspec CHANGED
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'bundler', '~> 2.0'
31
31
  spec.add_development_dependency 'minitest', '~> 5.0'
32
32
  spec.add_development_dependency 'pry', '>= 0.12.2'
33
- spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rake', '~> 12.3'
34
34
  spec.add_development_dependency 'rubocop', '>= 0.70.0'
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-24 00:00:00.000000000 Z
11
+ date: 2019-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '12.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '12.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +106,7 @@ files:
106
106
  - lib/neco/command.rb
107
107
  - lib/neco/composition.rb
108
108
  - lib/neco/container.rb
109
+ - lib/neco/result.rb
109
110
  - lib/neco/version.rb
110
111
  - neco.gemspec
111
112
  homepage: https://github.com/okuramasafumi/neco