cmdx 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.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +64 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +5 -0
  7. data/CODE_OF_CONDUCT.md +132 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +76 -0
  10. data/Rakefile +12 -0
  11. data/docs/basics/call.md +31 -0
  12. data/docs/basics/context.md +67 -0
  13. data/docs/basics/run.md +34 -0
  14. data/docs/basics/setup.md +32 -0
  15. data/docs/batch.md +53 -0
  16. data/docs/configuration.md +57 -0
  17. data/docs/example.md +82 -0
  18. data/docs/getting_started.md +47 -0
  19. data/docs/hooks.md +59 -0
  20. data/docs/interruptions/exceptions.md +29 -0
  21. data/docs/interruptions/faults.md +89 -0
  22. data/docs/interruptions/halt.md +80 -0
  23. data/docs/logging.md +102 -0
  24. data/docs/outcomes/result.md +17 -0
  25. data/docs/outcomes/states.md +31 -0
  26. data/docs/outcomes/statuses.md +33 -0
  27. data/docs/parameters/coercions.md +52 -0
  28. data/docs/parameters/defaults.md +47 -0
  29. data/docs/parameters/definitions.md +123 -0
  30. data/docs/parameters/namespacing.md +57 -0
  31. data/docs/parameters/validations.md +312 -0
  32. data/docs/tips_and_tricks.md +79 -0
  33. data/lib/cmdx/.DS_Store +0 -0
  34. data/lib/cmdx/batch.rb +43 -0
  35. data/lib/cmdx/coercions/array.rb +15 -0
  36. data/lib/cmdx/coercions/big_decimal.rb +23 -0
  37. data/lib/cmdx/coercions/boolean.rb +27 -0
  38. data/lib/cmdx/coercions/complex.rb +21 -0
  39. data/lib/cmdx/coercions/date.rb +26 -0
  40. data/lib/cmdx/coercions/date_time.rb +26 -0
  41. data/lib/cmdx/coercions/float.rb +21 -0
  42. data/lib/cmdx/coercions/hash.rb +31 -0
  43. data/lib/cmdx/coercions/integer.rb +21 -0
  44. data/lib/cmdx/coercions/rational.rb +21 -0
  45. data/lib/cmdx/coercions/string.rb +15 -0
  46. data/lib/cmdx/coercions/time.rb +26 -0
  47. data/lib/cmdx/coercions/virtual.rb +15 -0
  48. data/lib/cmdx/configuration.rb +25 -0
  49. data/lib/cmdx/context.rb +15 -0
  50. data/lib/cmdx/core_ext/hash.rb +36 -0
  51. data/lib/cmdx/core_ext/module.rb +48 -0
  52. data/lib/cmdx/core_ext/object.rb +55 -0
  53. data/lib/cmdx/error.rb +23 -0
  54. data/lib/cmdx/errors.rb +92 -0
  55. data/lib/cmdx/fault.rb +47 -0
  56. data/lib/cmdx/faults.rb +11 -0
  57. data/lib/cmdx/immutator.rb +21 -0
  58. data/lib/cmdx/lazy_struct.rb +79 -0
  59. data/lib/cmdx/log_formatters/json.rb +13 -0
  60. data/lib/cmdx/log_formatters/key_value.rb +13 -0
  61. data/lib/cmdx/log_formatters/line.rb +14 -0
  62. data/lib/cmdx/log_formatters/logstash.rb +18 -0
  63. data/lib/cmdx/log_formatters/raw.rb +13 -0
  64. data/lib/cmdx/logger.rb +16 -0
  65. data/lib/cmdx/parameter.rb +101 -0
  66. data/lib/cmdx/parameter_inspector.rb +23 -0
  67. data/lib/cmdx/parameter_serializer.rb +20 -0
  68. data/lib/cmdx/parameter_validator.rb +19 -0
  69. data/lib/cmdx/parameter_value.rb +136 -0
  70. data/lib/cmdx/parameters.rb +34 -0
  71. data/lib/cmdx/parameters_inspector.rb +13 -0
  72. data/lib/cmdx/parameters_serializer.rb +13 -0
  73. data/lib/cmdx/railtie.rb +32 -0
  74. data/lib/cmdx/result.rb +170 -0
  75. data/lib/cmdx/result_inspector.rb +31 -0
  76. data/lib/cmdx/result_logger.rb +22 -0
  77. data/lib/cmdx/result_serializer.rb +38 -0
  78. data/lib/cmdx/run.rb +33 -0
  79. data/lib/cmdx/run_inspector.rb +21 -0
  80. data/lib/cmdx/run_serializer.rb +16 -0
  81. data/lib/cmdx/task.rb +151 -0
  82. data/lib/cmdx/task_hook.rb +18 -0
  83. data/lib/cmdx/utils/datetime_formatter.rb +17 -0
  84. data/lib/cmdx/utils/method_name.rb +24 -0
  85. data/lib/cmdx/utils/runtime.rb +19 -0
  86. data/lib/cmdx/validators/custom.rb +20 -0
  87. data/lib/cmdx/validators/exclusion.rb +51 -0
  88. data/lib/cmdx/validators/format.rb +27 -0
  89. data/lib/cmdx/validators/inclusion.rb +51 -0
  90. data/lib/cmdx/validators/length.rb +114 -0
  91. data/lib/cmdx/validators/numeric.rb +114 -0
  92. data/lib/cmdx/validators/presence.rb +27 -0
  93. data/lib/cmdx/version.rb +7 -0
  94. data/lib/cmdx.rb +80 -0
  95. data/lib/generators/cmdx/batch_generator.rb +30 -0
  96. data/lib/generators/cmdx/install_generator.rb +15 -0
  97. data/lib/generators/cmdx/task_generator.rb +30 -0
  98. data/lib/generators/cmdx/templates/batch.rb.tt +7 -0
  99. data/lib/generators/cmdx/templates/install.rb +23 -0
  100. data/lib/generators/cmdx/templates/task.rb.tt +9 -0
  101. data/lib/locales/en.yml +36 -0
  102. data/lib/locales/es.yml +36 -0
  103. metadata +288 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aed549d510aee08ccdb114f75b51b7aefe1a9776905f53e75ec5c39477932350
4
+ data.tar.gz: b57596b1393185082d94616f8c08494d030a2d78bb85610674363a365efa15f5
5
+ SHA512:
6
+ metadata.gz: 7d123c5a08fc89494b759eddea90e614858e55db733a3690e899de46b5e6e93e574a9bf2d9899f4f9c9c118686d5ff2f84cfd75b110ce242b79d7627ae3dfbd8
7
+ data.tar.gz: 17e336e03307f3dddaf7dba53d4a201f031f54348bc0e43059a17ace90cf7f4a762ad6e97ec731b32d1064616c978948867e8aa746ff96f52e93996376828a7c
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,64 @@
1
+ plugins:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+ AllCops:
6
+ NewCops: enable
7
+ DisplayCopNames: true
8
+ DisplayStyleGuide: true
9
+ TargetRubyVersion: 3.1.0
10
+ Gemspec/DevelopmentDependencies:
11
+ EnforcedStyle: gemspec
12
+ Layout/EmptyLinesAroundAttributeAccessor:
13
+ Enabled: true
14
+ Layout/EmptyLinesAroundClassBody:
15
+ EnforcedStyle: empty_lines_except_namespace
16
+ Layout/EmptyLinesAroundModuleBody:
17
+ EnforcedStyle: empty_lines_except_namespace
18
+ Layout/LineLength:
19
+ Enabled: false
20
+ Metrics/AbcSize:
21
+ Enabled: false
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - 'spec/**/**/*'
25
+ - '*.gemspec'
26
+ Metrics/ClassLength:
27
+ Enabled: false
28
+ Metrics/CyclomaticComplexity:
29
+ Enabled: false
30
+ Metrics/MethodLength:
31
+ Enabled: false
32
+ Metrics/ModuleLength:
33
+ Enabled: false
34
+ Metrics/PerceivedComplexity:
35
+ Enabled: false
36
+ Naming/MethodParameterName:
37
+ Enabled: false
38
+ RSpec/AnyInstance:
39
+ Enabled: false
40
+ RSpec/ExampleLength:
41
+ Enabled: false
42
+ RSpec/MultipleExpectations:
43
+ Enabled: false
44
+ RSpec/NestedGroups:
45
+ Enabled: false
46
+ RSpec/SpecFilePathFormat:
47
+ CustomTransform:
48
+ CMDx: cmdx
49
+ RSpec/VerifiedDoubleReference:
50
+ Enabled: false
51
+ Style/Documentation:
52
+ Enabled: false
53
+ Style/FormatStringToken:
54
+ Enabled: false
55
+ Style/FrozenStringLiteralComment:
56
+ Enabled: true
57
+ EnforcedStyle: always_true
58
+ SafeAutoCorrect: true
59
+ Style/OpenStructUse:
60
+ Enabled: false
61
+ Style/OptionalBooleanParameter:
62
+ Enabled: false
63
+ Style/StringLiterals:
64
+ EnforcedStyle: double_quotes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-07
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 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,76 @@
1
+ # CMDx
2
+
3
+ [![forthebadge](http://forthebadge.com/images/badges/made-with-ruby.svg)](http://forthebadge.com)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/cmdx.svg)](https://badge.fury.io/rb/cmdx)
6
+ [![CI](https://github.com/drexed/cmdx/actions/workflows/ruby.yml/badge.svg)](https://github.com/drexed/cmdx/actions/workflows/ruby.yml)
7
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=shields)](http://makeapullrequest.com)
8
+
9
+ `CMDx` is a framework for expressive processing of business logic. Design
10
+ code of varying levels of complexity that is built through branching and
11
+ composition, informative halting, and exhaustive tracing/debugging.
12
+
13
+ ## Installation
14
+
15
+ **Prerequisites:** This gem requires Ruby `>= 3.1` to be installed.
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'cmdx'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install cmdx
30
+
31
+ ## Table of contents
32
+
33
+ - [Getting Started](https://github.com/drexed/cmdx/blob/main/docs/getting_started.md)
34
+ - [Configuration](https://github.com/drexed/cmdx/blob/main/docs/configuration.md)
35
+ - Basics
36
+ - [Setup](https://github.com/drexed/cmdx/blob/main/docs/basics/setup.md)
37
+ - [Call](https://github.com/drexed/cmdx/blob/main/docs/basics/call.md)
38
+ - [Context](https://github.com/drexed/cmdx/blob/main/docs/basics/context.md)
39
+ - [Run](https://github.com/drexed/cmdx/blob/main/docs/basics/run.md)
40
+ - Interruptions
41
+ - [Halt](https://github.com/drexed/cmdx/blob/main/docs/interruptions/halt.md)
42
+ - [Faults](https://github.com/drexed/cmdx/blob/main/docs/interruptions/faults.md)
43
+ - [Exceptions](https://github.com/drexed/cmdx/blob/main/docs/interruptions/exceptions.md)
44
+ - Parameters
45
+ - [Definitions](https://github.com/drexed/cmdx/blob/main/docs/parameters/definitions.md)
46
+ - [Namespacing](https://github.com/drexed/cmdx/blob/main/docs/parameters/namespacing.md)
47
+ - [Coercions](https://github.com/drexed/cmdx/blob/main/docs/parameters/coercions.md)
48
+ - [Validations](https://github.com/drexed/cmdx/blob/main/docs/parameters/validations.md)
49
+ - [Defaults](https://github.com/drexed/cmdx/blob/main/docs/parameters/defaults.md)
50
+ - Outcomes
51
+ - [Result](#results)
52
+ - [States](https://github.com/drexed/cmdx/blob/main/docs/outcomes/states.md)
53
+ - [Statuses](https://github.com/drexed/cmdx/blob/main/docs/outcomes/statuses.md)
54
+ - [Hooks](https://github.com/drexed/cmdx/blob/main/docs/hooks.md)
55
+ - [Batch](https://github.com/drexed/cmdx/blob/main/docs/batch.md)
56
+ - [Logging](https://github.com/drexed/cmdx/blob/main/docs/logging.md)
57
+ - [Tips & Tricks](https://github.com/drexed/cmdx/blob/main/docs/tips_and_tricks.md)
58
+ - [Example](https://github.com/drexed/cmdx/blob/main/docs/example.md)
59
+
60
+ ## Development
61
+
62
+ 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.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/drexed/cmdx. 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.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
73
+
74
+ ## Code of Conduct
75
+
76
+ Everyone interacting in the CMDx project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/drexed/cmdx/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,31 @@
1
+ # Basics - Call
2
+
3
+ Calling a task executes the logic within it. Tasks can only be executed via
4
+ the `call` and `call!` class methods.
5
+
6
+ ## Non-bang
7
+
8
+ The `call` method will always return a `CMDx::Result` object after execution.
9
+
10
+ ```ruby
11
+ ProcessOrderTask.call #=> <CMDx::Result ...>
12
+ ```
13
+
14
+ ## Bang
15
+
16
+ The bang `call!` method raises a `CMDx::Fault` based exception depending on the defined
17
+ `task_halt` status options, otherwise it will return a `CMDx::Result` object. This
18
+ form of call is useful in background jobs where retries are done via the exception mechanism.
19
+
20
+ ```ruby
21
+ ProcessOrderTask.call! #=> raises CMDx::Failed
22
+ ```
23
+
24
+ > [!IMPORTANT]
25
+ > Tasks are single use objects, once they have been called they are frozen and cannot be called again
26
+ > as result object will be returned. Build a new task call to execute a new instance of the same task.
27
+
28
+ ---
29
+
30
+ - **Prev:** [Basics - Setup](https://github.com/drexed/cmdx/blob/main/docs/basics/setup.md)
31
+ - **Next:** [Basics - Context](https://github.com/drexed/cmdx/blob/main/docs/basics/context.md)
@@ -0,0 +1,67 @@
1
+ # Basics - Context
2
+
3
+ The task `context` provides a form of storage to the task objects themselves.
4
+
5
+ ## Loading
6
+
7
+ Loading a task with data is as simple as passing objects in a key value format.
8
+ The context object is a custom version of an [OpenStruct](https://github.com/ruby/ostruct)
9
+ called a `LazyStruct` with no limitations for what can be stored.
10
+
11
+ ```ruby
12
+ ProcessOrderTask.call(email: "bob@bigcorp.com", order: order)
13
+ ```
14
+
15
+ ## Access
16
+
17
+ Tasks with a loaded context can be accessed within a task itself. Read, set and
18
+ alter the context attributes anywhere within the task object.
19
+
20
+ ```ruby
21
+ class ProcessOrderTask < CMDx::Task
22
+
23
+ def call
24
+ # Reading from context storage:
25
+ context.email #=> "bob@bigcorp.com"
26
+ ctx.order #=> <Order #a1b2c3d>
27
+ context.idk #=> nil
28
+
29
+ # Writing to context storage:
30
+ context.first_name = "Bob"
31
+ ctx.middle_name ||= "Biggie"
32
+ context.merge!(last_name: "Boomer")
33
+ end
34
+
35
+ end
36
+ ```
37
+
38
+ [Learn more](https://github.com/drexed/cmdx/blob/main/lib/cmdx/lazy_struct.rb)
39
+ about the `LazyStruct` public API for interacting with the context.
40
+
41
+ > [!NOTE]
42
+ > Attributes that are **NOT** loaded into the context will return `nil`.
43
+
44
+ ## Passing
45
+
46
+ Context objects can be passed to other tasks which allows you to build small tasks
47
+ that passes data around as part of a higher level task.
48
+
49
+ ```ruby
50
+ # Within task:
51
+ class ProcessOrderTask < CMDx::Task
52
+
53
+ def call
54
+ SendEmailConfirmationTask.call(context)
55
+ end
56
+
57
+ end
58
+
59
+ # After call:
60
+ result = ProcessOrderTask.call(email: "bob@bigcorp.com", order: order)
61
+ NotifyPartnerWarehousesTask.call(result.ctx)
62
+ ```
63
+
64
+ ---
65
+
66
+ - **Prev:** [Basics - Call](https://github.com/drexed/cmdx/blob/main/docs/basics/call.md)
67
+ - **Next:** [Basics - Run](https://github.com/drexed/cmdx/blob/main/docs/basics/run.md)
@@ -0,0 +1,34 @@
1
+ # Basics - Run
2
+
3
+ A run represents a group of tasks executed as part of collection.
4
+
5
+ When building complex tasks, it's best to pass the parents context to subtasks
6
+ (unless necessary or preventative) so that it gains automated indexing and the
7
+ parents `run_id`. This makes it easy to identify all tasks involved in one
8
+ execution from logging and stdout console calls.
9
+
10
+ ```ruby
11
+ class ProcessOrderTask < CMDx::Task
12
+
13
+ def call
14
+ # Subtasks inherit the ProcessOrderTask run_id:
15
+ SendEmailConfirmationTask.call(context)
16
+ NotifyPartnerWarehousesTask.call(context)
17
+ end
18
+
19
+ end
20
+
21
+ result = ProcessOrderTask.call
22
+ puts result.run.to_s
23
+
24
+ # Task name Index Run ID Task ID etc
25
+ # -----------------------------------------------------------------
26
+ #=> ProcessOrderTask 0 foobar123 abc123 ...
27
+ #=> SendEmailConfirmationTask 1 foobar123 def456 ...
28
+ #=> NotifyPartnerWarehousesTask 2 foobar123 ghi789 ...
29
+ ```
30
+
31
+ ---
32
+
33
+ - **Prev:** [Basics - Context](https://github.com/drexed/cmdx/blob/main/docs/basics/context.md)
34
+ - **Next:** [Interruptions - Halt](https://github.com/drexed/cmdx/blob/main/docs/interruptions/halt.md)
@@ -0,0 +1,32 @@
1
+ # Basics - Setup
2
+
3
+ A task represents a unit of work to execute. While `CMDx` offers a plethora
4
+ of features, a `call` method is the only thing required to execute a task.
5
+
6
+ ```ruby
7
+ class ProcessOrderTask < CMDx::Task
8
+
9
+ def call
10
+ # Do work...
11
+ end
12
+
13
+ private
14
+
15
+ # Business logic...
16
+
17
+ end
18
+ ```
19
+
20
+ > [!TIP]
21
+ > While complexity designed into a task is up to the engineer, it's
22
+ > suggested that tasks be small and composed into higher level tasks.
23
+
24
+ ## Generator
25
+
26
+ Run `rails g cmdx:task [NAME]` to create a task template file under `app/cmds`.
27
+ Tasks will inherit from `ApplicationTask` if available or fall back to `CMDx::Task`.
28
+
29
+ ---
30
+
31
+ - **Prev:** [Configuration](https://github.com/drexed/cmdx/blob/main/docs/configuration.md)
32
+ - **Next:** [Basics - Call](https://github.com/drexed/cmdx/blob/main/docs/basics/call.md)
data/docs/batch.md ADDED
@@ -0,0 +1,53 @@
1
+ # Batch
2
+
3
+ A CMDx::Batch is a task that calls other tasks in a linear fashion. The
4
+ context is passed down to each task, building on it knowledge with
5
+ each step. This is useful for composing multiple steps into one call.
6
+
7
+ > [!WARNING]
8
+ > Do **NOT** define a call method in this class. The batch class automatically
9
+ > defines the call logic.
10
+
11
+ ```ruby
12
+ class BatchProcessCheckout < CMDx::Batch
13
+
14
+ # Task level settings:
15
+ task_settings!(batch_halt: CMDx::Result::FAILED)
16
+
17
+ # Single declaration:
18
+ process FinalizeInvoiceTask
19
+
20
+ # Multiple declarations:
21
+ process SendConfirmationEmailTask, SendConfirmationTextTask
22
+
23
+ # With options (applies to all declared in that group):
24
+ process BatchNotifyPartnerWarehouses, batch_halt: [CMDx::Result::SKIPPED, CMDx::Result::FAILED]
25
+ process BatchNotifyUsaWarehouses, unless: proc { context.invoice.fulfilled_in_house? }
26
+
27
+ end
28
+ ```
29
+
30
+ > [!IMPORTANT]
31
+ > Process steps are executed in the order they are declared (FIFO: first in, first out).
32
+
33
+ The `process` method support the following options:
34
+
35
+ | Option | Description |
36
+ | ------------- | ----------- |
37
+ | `:if` | Specifies a callable method, proc or string to determine if processing steps should occur. |
38
+ | `:unless` | Specifies a callable method, proc, or string to determine if processing steps should not occur. |
39
+ | `:batch_halt` | Sets which result statuses processing of further steps should be prevented. (default: `CMDx::Result::FAILED`) |
40
+
41
+ > [!NOTE]
42
+ > Batches stop execution on `failed` by default. This is due to the concept
43
+ > of `skipped` being a bypass mechanism, rather than a choke point in execution.
44
+
45
+ ## Generator
46
+
47
+ Run `rails g cmdx:batch [NAME]` to create a batch task template file under `app/cmds`.
48
+ Tasks will inherit from `ApplicationBatch` if available or fall back to `CMDx::Batch`.
49
+
50
+ ---
51
+
52
+ - **Prev:** [Hooks](https://github.com/drexed/cmdx/blob/main/docs/hooks.md)
53
+ - **Next:** [Logging](https://github.com/drexed/cmdx/blob/main/docs/logging.md)
@@ -0,0 +1,57 @@
1
+ # Configuration
2
+
3
+ Configuration of tasks can be done at global and task levels.
4
+
5
+ ## Global settings
6
+
7
+ Run `rails g cmdx:install` to generate a configuration file within the
8
+ `config/initializers` directory. These settings will be preloaded into all tasks.
9
+
10
+ ```ruby
11
+ CMDx.configure do |config|
12
+ # Define which statuses a bang `call!` will halt and raise a fault.
13
+ # This option can accept an array of statuses.
14
+ config.task_halt = CMDx::Result::FAILED
15
+
16
+ # Enable task timeouts to prevent call execution beyond a defined threshold.
17
+ config.task_timeout = nil
18
+
19
+ # Define which statuses a batch task will halt execution from proceeding to the next step.
20
+ # By default skipped tasks are treated as a NOOP so processing is continued.
21
+ # This option can accept an array of statuses.
22
+ config.batch_halt = CMDx::Result::FAILED
23
+
24
+ # Enable batch timeouts to prevent call execution beyond a defined threshold.
25
+ # TIP: remember to account for all defined tasks when setting this value
26
+ config.batch_timeout = nil
27
+
28
+ # A list of available log formatter can be found at:
29
+ # https://github.com/drexed/cmdx/tree/main/lib/cmdx/log_formatters
30
+ config.logger = Logger.new($stdout, formatter: CMDx::LogFormatters::Line.new)
31
+ end
32
+ ```
33
+
34
+ ## Task settings
35
+
36
+ Finely tune tasks via class level settings.
37
+
38
+ ```ruby
39
+ class ProcessOrderTask < CMDx::Task
40
+
41
+ # Task level settings:
42
+ task_settings!(task_timeout: 1, tags: ["v1", "DEPRECATED"])
43
+
44
+ def call
45
+ # Do work...
46
+ end
47
+
48
+ end
49
+ ```
50
+
51
+ > [!NOTE]
52
+ > `tags` is a task level setting only. Tags will automatically be appended to logs.
53
+
54
+ ---
55
+
56
+ - **Prev:** [Configuration](https://github.com/drexed/cmdx/blob/main/docs/configuration.md)
57
+ - **Next:** [Basics - Setup](https://github.com/drexed/cmdx/blob/main/docs/basics/setup.md)