lite-command 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38f75eba374b926e0f6790498d28c629025a6cab44b55ddc4b7f41722ece302c
4
+ data.tar.gz: 4f08967482e946167097e2d17cb0a5b24eed984d9767b7b953aa95c6e021762d
5
+ SHA512:
6
+ metadata.gz: 2330875e2a39f223bf3995c1a9156e118c9e27a25defa84e658cfd5f1c8285b16d7da1051dc14735a1c2670d5a8503c7d65d7491db145db226eefc5f8c2ed951
7
+ data.tar.gz: bcf66be534fc5847bc1e30f6fcac4609ed442a44e687263fb91bf684b1affbaf4e360dc34b653cf97f5deb8f0bc5a322e9fa973bcb9a880f354ed6fe7c30f2de
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ block_vs_symbol_to_proc: true
3
+ each_with_index_vs_while: false
4
+ fetch_with_argument_vs_block: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
8
+ hash_merge_bang_vs_hash_brackets: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
12
+ proc_call_vs_yield: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
16
+ select_last_vs_reverse_detect: true
17
+ setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
data/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ LineLength:
9
+ Max: 100
10
+ Layout/EmptyLinesAroundBlockBody:
11
+ Exclude:
12
+ - 'spec/**/**/*'
13
+ Layout/EmptyLinesAroundClassBody:
14
+ EnforcedStyle: empty_lines_except_namespace
15
+ Layout/EmptyLinesAroundModuleBody:
16
+ EnforcedStyle: empty_lines_except_namespace
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/**/*'
20
+ - '*.gemspec'
21
+ RSpec/ExampleLength:
22
+ Enabled: false
23
+ RSpec/MultipleExpectations:
24
+ Enabled: false
25
+ Style/Documentation:
26
+ Enabled: false
27
+ Style/ExpandPathArguments:
28
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5
6
+ - 2.6
7
+ - ruby-head
8
+ matrix:
9
+ fast_finish: true
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ before_install:
13
+ - gem update --system
14
+ - gem install bundler
15
+ install:
16
+ - bundle install --jobs=3 --retry=3
17
+ script:
18
+ - bundle exec rspec
19
+ - bundle exec rubocop
20
+ - bundle exec fasterer
21
+ notifications:
22
+ email: false
23
+ slack:
24
+ secure: 2PptrBf+yl8evd/WA4XKg0tzJqSdEn5TaAhuHR90o5X6Rd0MA4SES+QZQn9A81CPuJk//V8Dur8NQQzrcAivVOsmC+JbIHZV1MS8v32Uvoy28Vr8omAne4Ec3so6rXCIMniREGcHtKeXHdNYO61VZPPjgxd7RTwajvTC7KS0ViuKBPct+hq+WfJmX6Xp2vxTHImNVurExBrFZkJyPtV+UG1tJ6mB+kF8MQ1+t3ZBtU4+6eVviADYi7FBEeP0j9gdKBCzU7aPoE+XLMWcP0tI9ibaP9crEMKi05JNDLzm5f6PGvGJWEQSCeh3CxNhaEF7eog3BAw6mUXkKI0hvFfrZeyG+VkDU6T9rAG8u55BrREi4mRNJl0c0rS2J45bFRmYOlvDNn9xrhMkkfFn8SMX3etYJYqHNBxdF2TX4SzpDrshd3hcm2btmkThn/Ua/mYUWs9OF3O/ZSu0zB+/g83zrAsjMyfqJ1PE5ZiiMD2Rjrzb/fyYcsNeVhncwRdse9LtJRqz1KpOh/r5LBrWfRRCQwlVNwMtBdFxQ7Ytl9dz/dfJmGKgM8zsdJdI6ZyV+lwa0jPaql6Y8wXjox1wwN3IRCyvoVsgxmiQAV8jnWD2lf6I/Ea5yXCImGluuRAyRrAizElXGBNubhAVtQwQinHWfaYO+mJElScBwrxTVXuzQUI=
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2019-08-07
10
+ ### Added
11
+ - Initial project version
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.gomez@drexed.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
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 lite-command.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lite-command (1.0.0)
5
+ lite-errors
6
+ lite-memoize
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ colorize (0.8.1)
13
+ diff-lcs (1.3)
14
+ fasterer (0.6.0)
15
+ colorize (~> 0.7)
16
+ ruby_parser (>= 3.13.0)
17
+ jaro_winkler (1.5.3)
18
+ lite-errors (1.0.1)
19
+ lite-memoize (1.0.1)
20
+ parallel (1.17.0)
21
+ parser (2.6.3.0)
22
+ ast (~> 2.4.0)
23
+ rainbow (3.0.0)
24
+ rake (12.3.3)
25
+ rspec (3.8.0)
26
+ rspec-core (~> 3.8.0)
27
+ rspec-expectations (~> 3.8.0)
28
+ rspec-mocks (~> 3.8.0)
29
+ rspec-core (3.8.2)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-expectations (3.8.4)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-mocks (3.8.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-support (3.8.2)
38
+ rubocop (0.74.0)
39
+ jaro_winkler (~> 1.5.1)
40
+ parallel (~> 1.10)
41
+ parser (>= 2.6)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 1.7)
45
+ rubocop-performance (1.4.1)
46
+ rubocop (>= 0.71.0)
47
+ rubocop-rspec (1.35.0)
48
+ rubocop (>= 0.60.0)
49
+ ruby-progressbar (1.10.1)
50
+ ruby_parser (3.13.1)
51
+ sexp_processor (~> 4.9)
52
+ sexp_processor (4.12.1)
53
+ unicode-display_width (1.6.0)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler
60
+ fasterer
61
+ lite-command!
62
+ rake
63
+ rspec
64
+ rubocop
65
+ rubocop-performance
66
+ rubocop-rspec
67
+
68
+ BUNDLED WITH
69
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Juan Gomez
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,224 @@
1
+ # Lite::Command
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lite-command.svg)](http://badge.fury.io/rb/lite-command)
4
+ [![Build Status](https://travis-ci.org/drexed/lite-command.svg?branch=master)](https://travis-ci.org/drexed/lite-command)
5
+
6
+ Lite::Command provides an API for building simple and complex command based service objects.
7
+ It provides mixins for handling errors and memoization to improve your object workflow productivity.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'lite-command'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install lite-command
24
+
25
+ ## Table of Contents
26
+
27
+ * [Simple](#simple)
28
+ * [Complex](#complex)
29
+ * [Extensions](#extensions)
30
+
31
+ ## Simple
32
+
33
+ Simple commands build quick class based calls but cannot be extended.
34
+ This is more of a traditional command service call as it only exposes a `call` method.
35
+
36
+ ```ruby
37
+ class SearchMovies < Lite::Command::Simple
38
+
39
+ class << self
40
+ private
41
+
42
+ # NOTE: This class method is required
43
+ def command
44
+ { generate_fingerprint => movies_by_name }
45
+ end
46
+ end
47
+
48
+ end
49
+ ```
50
+
51
+ **Caller**
52
+
53
+ ```ruby
54
+ command = SearchMovies.call('Toy Story')
55
+ command.call
56
+ ```
57
+
58
+ ## Complex
59
+
60
+ Complex commands can be used in instance and class based calls and
61
+ extended with access to errors and memoization.
62
+
63
+ ```ruby
64
+ class SearchMovies < Lite::Command::Complex
65
+
66
+ def initialize(name)
67
+ @name = name
68
+ end
69
+
70
+ # NOTE: This instance method is required
71
+ def command
72
+ { generate_fingerprint => movies_by_name }
73
+ end
74
+
75
+ private
76
+
77
+ def movies_by_name
78
+ HTTP.get("http://movies.com?title=#{title}")
79
+ end
80
+
81
+ def generate_fingerprint
82
+ Digest::MD5.hexdigest(movies_by_name)
83
+ end
84
+
85
+ end
86
+ ```
87
+
88
+ **Caller**
89
+
90
+ ```ruby
91
+ command = SearchMovies.new('Toy Story')
92
+ command.called? #=> false
93
+ command.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
94
+ command.called? #=> true
95
+
96
+ # - or -
97
+
98
+ command = SearchMovies.call('Toy Story')
99
+ command.called? #=> true
100
+ command.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
101
+
102
+ # - or -
103
+
104
+ # Useful when you are not using the Errors mixin as its a one time access call.
105
+ SearchMovies.run('Toy Story') #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
106
+ ```
107
+
108
+ **Result**
109
+
110
+ ```ruby
111
+ command = SearchMovies.new('Toy Story')
112
+ command.result #=> nil
113
+
114
+ command.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
115
+ command.result #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
116
+
117
+ command.recall! #=> Clears the call, cache, errors, and then re-performs the call
118
+ command.result #=> { 'fingerprint_2' => [ 'Toy Story 2', ... ] }
119
+ ```
120
+
121
+ ## Extensions
122
+
123
+ Extend complex base command with any of the following extensions:
124
+
125
+ ### Errors (optional)
126
+
127
+ Learn more about using [Lite::Errors](https://github.com/drexed/lite-errors)
128
+
129
+ ```ruby
130
+ class SearchMovies < Lite::Command::Complex
131
+ include Lite::Command::Extensions::Errors
132
+
133
+ # ... ommited ...
134
+
135
+ private
136
+
137
+ # Add a fingerprint error to the error pool
138
+ def generate_fingerprint
139
+ Digest::MD5.hexdigest(movies_by_name)
140
+ rescue
141
+ errors.add(:fingerprint, 'invalid md5 request value')
142
+ end
143
+
144
+ end
145
+ ```
146
+
147
+ **Caller**
148
+
149
+ ```ruby
150
+ # Useful for controllers or actions that depend on states.
151
+ SearchMovies.perform('Toy Story') do |result, success, failure|
152
+ success.call { redirect_to(movie_path, notice: "Movie can be found at: #{result}") }
153
+ failure.call { redirect_to(root_path, notice: "Movie cannot be found at: #{result}") }
154
+ end
155
+ ```
156
+
157
+ **Methods**
158
+
159
+ ```ruby
160
+ command = SearchMovies.call('Toy Story')
161
+ command.errors #=> Lite::Errors::Messages object
162
+
163
+ command.validate! #=> Raises Lite::Command::ValidationError if it has any errors
164
+ command.valid? #=> Alias for validate!
165
+
166
+ command.errored? #=> false
167
+ command.success? #=> true
168
+ command.failure? #=> Checks that it has been called and has errors
169
+ command.status #=> :failure
170
+
171
+ command.result! #=> Raises Lite::Command::ValidationError if it has any errors, if not it returns the result
172
+ ```
173
+
174
+ ### Memoize (optional)
175
+
176
+ Learn more about using [Lite::Memoize](https://github.com/drexed/lite-memoize)
177
+
178
+ ```ruby
179
+ class SearchMovies < Lite::Command::Complex
180
+ include Lite::Command::Extensions::Memoize
181
+
182
+ # ... ommited ...
183
+
184
+ private
185
+
186
+ # Sets the value in the cache
187
+ # Subsequent method calls gets the cached value
188
+ # This saves you the extra external HTTP.get call
189
+ def movies_by_name
190
+ cache.memoize { HTTP.get("http://movies.com?title=#{title}") }
191
+ end
192
+
193
+ # Gets the value in the cache
194
+ def generate_fingerprint
195
+ Digest::MD5.hexdigest(movies_by_name)
196
+ end
197
+
198
+ end
199
+ ```
200
+
201
+ **Methods**
202
+
203
+ ```ruby
204
+ command = SearchMovies.call('Toy Story')
205
+ command.cache #=> Lite::Memoize::Instance object
206
+ ```
207
+
208
+ ## Development
209
+
210
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
211
+
212
+ 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).
213
+
214
+ ## Contributing
215
+
216
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-command. 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.
217
+
218
+ ## License
219
+
220
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
221
+
222
+ ## Code of Conduct
223
+
224
+ Everyone interacting in the Lite::Command project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-command/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'lite/command'
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__)
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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lite/command/version"
4
+
5
+ %w[errors memoize].each do |name|
6
+ require "lite/command/extensions/#{name}"
7
+ end
8
+
9
+ %w[exceptions states complex simple].each do |name|
10
+ require "lite/command/#{name}"
11
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Command
5
+ class Complex
6
+
7
+ class << self
8
+
9
+ def call(*args)
10
+ klass = new(*args)
11
+ klass.call
12
+ klass
13
+ end
14
+
15
+ def run(*args)
16
+ klass = call(*args)
17
+ klass.result
18
+ end
19
+
20
+ end
21
+
22
+ attr_reader :result
23
+
24
+ def initialize(*args)
25
+ @args = args
26
+ end
27
+
28
+ def call
29
+ raise Lite::Command::NotImplementedError unless defined?(command)
30
+ return @result if called?
31
+
32
+ @called = true
33
+ @result = command
34
+ end
35
+
36
+ def called?
37
+ @called ||= false
38
+ end
39
+
40
+ def recall!
41
+ @called = false
42
+ %i[cache errors].each { |mixin| send(mixin).clear if respond_to?(mixin) }
43
+ call
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Command
5
+
6
+ class NotImplementedError < StandardError; end
7
+ class ValidationError < StandardError; end
8
+
9
+ end
10
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/errors'
4
+
5
+ module Lite
6
+ module Command
7
+ module Extensions
8
+ module Errors
9
+
10
+ module ClassMethods
11
+
12
+ def perform(*args)
13
+ klass = call(*args)
14
+
15
+ if klass.success?
16
+ yield(klass.result, Lite::Command::Success, Lite::Command::Failure)
17
+ else
18
+ yield(klass.result, Lite::Command::Failure, Lite::Command::Success)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ class << self
25
+
26
+ def included(klass)
27
+ klass.extend(ClassMethods)
28
+ end
29
+
30
+ end
31
+
32
+ def errors
33
+ @errors ||= Lite::Errors::Messages.new
34
+ end
35
+
36
+ def errored?
37
+ !errors.empty?
38
+ end
39
+
40
+ def fail!
41
+ raise Lite::Command::ValidationError
42
+ end
43
+
44
+ def failure?
45
+ called? && errored?
46
+ end
47
+
48
+ def result!
49
+ result if valid?
50
+ end
51
+
52
+ def status
53
+ return :pending unless called?
54
+
55
+ success? ? :success : :failure
56
+ end
57
+
58
+ def success?
59
+ called? && !errored?
60
+ end
61
+
62
+ def validate!
63
+ return true if success?
64
+
65
+ fail!
66
+ end
67
+
68
+ alias valid? validate!
69
+
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/memoize'
4
+
5
+ module Lite
6
+ module Command
7
+ module Extensions
8
+ module Memoize
9
+
10
+ def cache
11
+ @cache ||= Lite::Memoize::Instance.new
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Command
5
+ class Simple
6
+
7
+ class << self
8
+
9
+ def call(*args)
10
+ raise Lite::Command::NotImplementedError unless defined?(command)
11
+
12
+ command
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Command
5
+
6
+ class Success
7
+
8
+ class << self
9
+
10
+ def call
11
+ yield
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+ class Failure
19
+
20
+ class << self
21
+
22
+ def call
23
+ # Do nothing
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Command
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
@@ -0,0 +1,50 @@
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 'lite/command/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-command'
9
+ spec.version = Lite::Command::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = 'Ruby Command based framework (aka service objects)'
14
+ spec.homepage = 'http://drexed.github.io/lite-command'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-command/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-command'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_dependency 'lite-errors'
41
+ spec.add_dependency 'lite-memoize'
42
+
43
+ spec.add_development_dependency 'bundler'
44
+ spec.add_development_dependency 'fasterer'
45
+ spec.add_development_dependency 'rake'
46
+ spec.add_development_dependency 'rspec'
47
+ spec.add_development_dependency 'rubocop'
48
+ spec.add_development_dependency 'rubocop-performance'
49
+ spec.add_development_dependency 'rubocop-rspec'
50
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-command
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lite-errors
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: lite-memoize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
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
+ - !ruby/object:Gem::Dependency
56
+ name: fasterer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '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: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-performance
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - j.gomez@drexed.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".fasterer.yml"
147
+ - ".gitattributes"
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - ".rubocop.yml"
151
+ - ".travis.yml"
152
+ - CHANGELOG.md
153
+ - CODE_OF_CONDUCT.md
154
+ - Gemfile
155
+ - Gemfile.lock
156
+ - LICENSE.txt
157
+ - README.md
158
+ - Rakefile
159
+ - _config.yml
160
+ - bin/console
161
+ - bin/setup
162
+ - lib/lite/command.rb
163
+ - lib/lite/command/complex.rb
164
+ - lib/lite/command/exceptions.rb
165
+ - lib/lite/command/extensions/errors.rb
166
+ - lib/lite/command/extensions/memoize.rb
167
+ - lib/lite/command/simple.rb
168
+ - lib/lite/command/states.rb
169
+ - lib/lite/command/version.rb
170
+ - lite-command.gemspec
171
+ homepage: http://drexed.github.io/lite-command
172
+ licenses:
173
+ - MIT
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubygems_version: 3.0.4
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: Ruby Command based framework (aka service objects)
194
+ test_files: []