cmdx 1.1.1 → 1.5.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 +4 -4
- data/.DS_Store +0 -0
- data/.cursor/prompts/docs.md +4 -1
- data/.cursor/prompts/llms.md +20 -0
- data/.cursor/prompts/rspec.md +4 -1
- data/.cursor/prompts/yardoc.md +3 -2
- data/.cursor/rules/cursor-instructions.mdc +56 -1
- data/.irbrc +6 -0
- data/.rubocop.yml +29 -18
- data/.ruby-version +1 -1
- data/CHANGELOG.md +6 -128
- data/LLM.md +3317 -0
- data/README.md +68 -44
- data/docs/attributes/coercions.md +162 -0
- data/docs/attributes/defaults.md +90 -0
- data/docs/attributes/definitions.md +281 -0
- data/docs/attributes/naming.md +78 -0
- data/docs/attributes/validations.md +309 -0
- data/docs/basics/chain.md +56 -249
- data/docs/basics/context.md +56 -289
- data/docs/basics/execution.md +114 -0
- data/docs/basics/setup.md +37 -334
- data/docs/callbacks.md +89 -467
- data/docs/deprecation.md +91 -174
- data/docs/getting_started.md +212 -202
- data/docs/internationalization.md +11 -647
- data/docs/interruptions/exceptions.md +23 -198
- data/docs/interruptions/faults.md +71 -151
- data/docs/interruptions/halt.md +109 -186
- data/docs/logging.md +44 -256
- data/docs/middlewares.md +113 -426
- data/docs/outcomes/result.md +81 -228
- data/docs/outcomes/states.md +33 -221
- data/docs/outcomes/statuses.md +21 -311
- data/docs/tips_and_tricks.md +120 -70
- data/docs/workflows.md +99 -283
- data/lib/cmdx/.DS_Store +0 -0
- data/lib/cmdx/attribute.rb +229 -0
- data/lib/cmdx/attribute_registry.rb +94 -0
- data/lib/cmdx/attribute_value.rb +193 -0
- data/lib/cmdx/callback_registry.rb +69 -77
- data/lib/cmdx/chain.rb +56 -73
- data/lib/cmdx/coercion_registry.rb +52 -68
- data/lib/cmdx/coercions/array.rb +19 -18
- data/lib/cmdx/coercions/big_decimal.rb +20 -24
- data/lib/cmdx/coercions/boolean.rb +26 -25
- data/lib/cmdx/coercions/complex.rb +21 -22
- data/lib/cmdx/coercions/date.rb +25 -23
- data/lib/cmdx/coercions/date_time.rb +24 -25
- data/lib/cmdx/coercions/float.rb +25 -22
- data/lib/cmdx/coercions/hash.rb +31 -32
- data/lib/cmdx/coercions/integer.rb +30 -24
- data/lib/cmdx/coercions/rational.rb +29 -24
- data/lib/cmdx/coercions/string.rb +19 -22
- data/lib/cmdx/coercions/symbol.rb +37 -0
- data/lib/cmdx/coercions/time.rb +26 -25
- data/lib/cmdx/configuration.rb +49 -108
- data/lib/cmdx/context.rb +222 -44
- data/lib/cmdx/deprecator.rb +61 -0
- data/lib/cmdx/errors.rb +42 -252
- data/lib/cmdx/exceptions.rb +39 -0
- data/lib/cmdx/faults.rb +78 -39
- data/lib/cmdx/freezer.rb +51 -0
- data/lib/cmdx/identifier.rb +30 -0
- data/lib/cmdx/locale.rb +52 -0
- data/lib/cmdx/log_formatters/json.rb +21 -22
- data/lib/cmdx/log_formatters/key_value.rb +20 -22
- data/lib/cmdx/log_formatters/line.rb +15 -22
- data/lib/cmdx/log_formatters/logstash.rb +22 -23
- data/lib/cmdx/log_formatters/raw.rb +16 -22
- data/lib/cmdx/middleware_registry.rb +70 -74
- data/lib/cmdx/middlewares/correlate.rb +90 -54
- data/lib/cmdx/middlewares/runtime.rb +58 -0
- data/lib/cmdx/middlewares/timeout.rb +48 -68
- data/lib/cmdx/railtie.rb +12 -45
- data/lib/cmdx/result.rb +229 -314
- data/lib/cmdx/task.rb +194 -366
- data/lib/cmdx/utils/call.rb +49 -0
- data/lib/cmdx/utils/condition.rb +71 -0
- data/lib/cmdx/utils/format.rb +61 -0
- data/lib/cmdx/validator_registry.rb +63 -72
- data/lib/cmdx/validators/exclusion.rb +38 -67
- data/lib/cmdx/validators/format.rb +48 -49
- data/lib/cmdx/validators/inclusion.rb +43 -74
- data/lib/cmdx/validators/length.rb +91 -154
- data/lib/cmdx/validators/numeric.rb +87 -162
- data/lib/cmdx/validators/presence.rb +37 -50
- data/lib/cmdx/version.rb +1 -1
- data/lib/cmdx/worker.rb +178 -0
- data/lib/cmdx/workflow.rb +85 -81
- data/lib/cmdx.rb +19 -13
- data/lib/generators/cmdx/install_generator.rb +14 -13
- data/lib/generators/cmdx/task_generator.rb +25 -50
- data/lib/generators/cmdx/templates/install.rb +11 -46
- data/lib/generators/cmdx/templates/task.rb.tt +3 -2
- data/lib/locales/en.yml +18 -4
- data/src/cmdx-logo.png +0 -0
- metadata +32 -116
- data/docs/ai_prompts.md +0 -393
- data/docs/basics/call.md +0 -317
- data/docs/configuration.md +0 -344
- data/docs/parameters/coercions.md +0 -396
- data/docs/parameters/defaults.md +0 -335
- data/docs/parameters/definitions.md +0 -446
- data/docs/parameters/namespacing.md +0 -378
- data/docs/parameters/validations.md +0 -405
- data/docs/testing.md +0 -553
- data/lib/cmdx/callback.rb +0 -53
- data/lib/cmdx/chain_inspector.rb +0 -56
- data/lib/cmdx/chain_serializer.rb +0 -63
- data/lib/cmdx/coercion.rb +0 -57
- data/lib/cmdx/coercions/virtual.rb +0 -29
- data/lib/cmdx/core_ext/hash.rb +0 -83
- data/lib/cmdx/core_ext/module.rb +0 -98
- data/lib/cmdx/core_ext/object.rb +0 -125
- data/lib/cmdx/correlator.rb +0 -122
- data/lib/cmdx/error.rb +0 -60
- data/lib/cmdx/fault.rb +0 -140
- data/lib/cmdx/immutator.rb +0 -52
- data/lib/cmdx/lazy_struct.rb +0 -246
- data/lib/cmdx/log_formatters/pretty_json.rb +0 -40
- data/lib/cmdx/log_formatters/pretty_key_value.rb +0 -38
- data/lib/cmdx/log_formatters/pretty_line.rb +0 -41
- data/lib/cmdx/logger.rb +0 -49
- data/lib/cmdx/logger_ansi.rb +0 -68
- data/lib/cmdx/logger_serializer.rb +0 -116
- data/lib/cmdx/middleware.rb +0 -70
- data/lib/cmdx/parameter.rb +0 -312
- data/lib/cmdx/parameter_evaluator.rb +0 -231
- data/lib/cmdx/parameter_inspector.rb +0 -66
- data/lib/cmdx/parameter_registry.rb +0 -106
- data/lib/cmdx/parameter_serializer.rb +0 -59
- data/lib/cmdx/result_ansi.rb +0 -71
- data/lib/cmdx/result_inspector.rb +0 -71
- data/lib/cmdx/result_logger.rb +0 -59
- data/lib/cmdx/result_serializer.rb +0 -104
- data/lib/cmdx/rspec/matchers.rb +0 -28
- data/lib/cmdx/rspec/result_matchers/be_executed.rb +0 -42
- data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +0 -94
- data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +0 -94
- data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +0 -59
- data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +0 -57
- data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +0 -87
- data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +0 -51
- data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +0 -58
- data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +0 -59
- data/lib/cmdx/rspec/result_matchers/have_context.rb +0 -86
- data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +0 -54
- data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +0 -52
- data/lib/cmdx/rspec/result_matchers/have_metadata.rb +0 -114
- data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +0 -66
- data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +0 -64
- data/lib/cmdx/rspec/result_matchers/have_runtime.rb +0 -78
- data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +0 -76
- data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +0 -62
- data/lib/cmdx/rspec/task_matchers/have_callback.rb +0 -85
- data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +0 -68
- data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +0 -92
- data/lib/cmdx/rspec/task_matchers/have_middleware.rb +0 -46
- data/lib/cmdx/rspec/task_matchers/have_parameter.rb +0 -181
- data/lib/cmdx/task_deprecator.rb +0 -52
- data/lib/cmdx/task_processor.rb +0 -246
- data/lib/cmdx/task_serializer.rb +0 -57
- data/lib/cmdx/utils/ansi_color.rb +0 -73
- data/lib/cmdx/utils/log_timestamp.rb +0 -36
- data/lib/cmdx/utils/monotonic_runtime.rb +0 -34
- data/lib/cmdx/utils/name_affix.rb +0 -52
- data/lib/cmdx/validator.rb +0 -57
- data/lib/generators/cmdx/templates/workflow.rb.tt +0 -7
- data/lib/generators/cmdx/workflow_generator.rb +0 -84
- data/lib/locales/ar.yml +0 -35
- data/lib/locales/cs.yml +0 -35
- data/lib/locales/da.yml +0 -35
- data/lib/locales/de.yml +0 -35
- data/lib/locales/el.yml +0 -35
- data/lib/locales/es.yml +0 -35
- data/lib/locales/fi.yml +0 -35
- data/lib/locales/fr.yml +0 -35
- data/lib/locales/he.yml +0 -35
- data/lib/locales/hi.yml +0 -35
- data/lib/locales/it.yml +0 -35
- data/lib/locales/ja.yml +0 -35
- data/lib/locales/ko.yml +0 -35
- data/lib/locales/nl.yml +0 -35
- data/lib/locales/no.yml +0 -35
- data/lib/locales/pl.yml +0 -35
- data/lib/locales/pt.yml +0 -35
- data/lib/locales/ru.yml +0 -35
- data/lib/locales/sv.yml +0 -35
- data/lib/locales/th.yml +0 -35
- data/lib/locales/tr.yml +0 -35
- data/lib/locales/vi.yml +0 -35
- data/lib/locales/zh.yml +0 -35
data/docs/tips_and_tricks.md
CHANGED
@@ -1,25 +1,16 @@
|
|
1
|
-
# Tips
|
1
|
+
# Tips and Tricks
|
2
2
|
|
3
3
|
This guide covers advanced patterns and optimization techniques for getting the most out of CMDx in production applications.
|
4
4
|
|
5
5
|
## Table of Contents
|
6
6
|
|
7
|
-
- [TLDR](#tldr)
|
8
7
|
- [Project Organization](#project-organization)
|
9
8
|
- [Directory Structure](#directory-structure)
|
10
9
|
- [Naming Conventions](#naming-conventions)
|
11
|
-
- [
|
12
|
-
- [
|
13
|
-
- [
|
14
|
-
|
15
|
-
|
16
|
-
## TLDR
|
17
|
-
|
18
|
-
- **Organization** - Group commands by domain in `/app/commands` with descriptive subdirectories
|
19
|
-
- **Naming** - Tasks use "Verb + Noun + Task", workflows use "Noun + Verb + Workflow"
|
20
|
-
- **Parameter optimization** - Use `with_options` to reduce duplication in parameter definitions
|
21
|
-
- **Monitoring** - Enable ActiveRecord query tagging for better debugging and observability
|
22
|
-
- **Base classes** - Create `ApplicationTask` and `ApplicationWorkflow` for shared configuration
|
10
|
+
- [Story Telling](#story-telling)
|
11
|
+
- [Style Guide](#style-guide)
|
12
|
+
- [Attribute Options](#attribute-options)
|
13
|
+
- [ActiveRecord Query Tagging](#activerecord-query-tagging)
|
23
14
|
|
24
15
|
## Project Organization
|
25
16
|
|
@@ -27,25 +18,22 @@ This guide covers advanced patterns and optimization techniques for getting the
|
|
27
18
|
|
28
19
|
Create a well-organized command structure for maintainable applications:
|
29
20
|
|
30
|
-
```
|
31
|
-
/app
|
32
|
-
|
33
|
-
/
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
/
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
- validate_payment_method_task.rb
|
47
|
-
- application_task.rb
|
48
|
-
- application_workflow.rb
|
21
|
+
```text
|
22
|
+
/app/
|
23
|
+
└── /tasks/
|
24
|
+
├── /invoices/
|
25
|
+
│ ├── calculate_tax.rb
|
26
|
+
│ ├── validate_invoice.rb
|
27
|
+
│ ├── send_invoice.rb
|
28
|
+
│ └── process_invoice.rb # workflow
|
29
|
+
├── /reports/
|
30
|
+
│ ├── generate_pdf.rb
|
31
|
+
│ ├── compile_data.rb
|
32
|
+
│ ├── export_csv.rb
|
33
|
+
│ └── create_reports.rb # workflow
|
34
|
+
├── application_task.rb # base class
|
35
|
+
├── authenticate_session.rb
|
36
|
+
└── activate_account.rb
|
49
37
|
```
|
50
38
|
|
51
39
|
### Naming Conventions
|
@@ -53,60 +41,121 @@ Create a well-organized command structure for maintainable applications:
|
|
53
41
|
Follow consistent naming patterns for clarity and maintainability:
|
54
42
|
|
55
43
|
```ruby
|
56
|
-
#
|
57
|
-
class
|
58
|
-
class
|
59
|
-
class
|
60
|
-
|
61
|
-
# Workflows: Noun + Verb + Workflow
|
62
|
-
class OrderProcessingWorkflow < CMDx::Workflow; end
|
63
|
-
class NotificationDeliveryWorkflow < CMDx::Workflow; end
|
44
|
+
# Verb + Noun
|
45
|
+
class ExportData < CMDx::Task; end
|
46
|
+
class CompressFile < CMDx::Task; end
|
47
|
+
class ValidateSchema < CMDx::Task; end
|
64
48
|
|
65
49
|
# Use present tense verbs for actions
|
66
|
-
class
|
67
|
-
class
|
68
|
-
class
|
50
|
+
class GenerateToken < CMDx::Task; end # ✓ Good
|
51
|
+
class GeneratingToken < CMDx::Task; end # ❌ Avoid
|
52
|
+
class TokenGeneration < CMDx::Task; end # ❌ Avoid
|
53
|
+
```
|
54
|
+
|
55
|
+
### Story Telling
|
56
|
+
|
57
|
+
Consider using descriptive methods to express the task’s flow, rather than concentrating all logic inside the `work` method.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class ProcessOrder < CMDx::Task
|
61
|
+
def work
|
62
|
+
charge_payment_method
|
63
|
+
assign_to_warehouse
|
64
|
+
send_notification
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def charge_payment_method
|
70
|
+
order.primary_payment_method.charge!
|
71
|
+
end
|
72
|
+
|
73
|
+
def assign_to_warehouse
|
74
|
+
order.ready_for_shipping!
|
75
|
+
end
|
76
|
+
|
77
|
+
def send_notification
|
78
|
+
if order.products_out_of_stock?
|
79
|
+
OrderMailer.pending(order).deliver
|
80
|
+
else
|
81
|
+
OrderMailer.preparing(order).deliver
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
69
85
|
```
|
70
86
|
|
71
|
-
|
87
|
+
### Style Guide
|
88
|
+
|
89
|
+
Follow a style pattern for consistent task design:
|
72
90
|
|
73
|
-
|
91
|
+
```ruby
|
92
|
+
class ExportReport < CMDx::Task
|
93
|
+
|
94
|
+
# 1. Register functions
|
95
|
+
register :middleware, CMDx::Middlewares::Correlate
|
96
|
+
register :validator, :format, FormatValidator
|
97
|
+
|
98
|
+
# 2. Define callbacks
|
99
|
+
before_execution :find_report
|
100
|
+
on_complete :track_export_metrics, if: ->(task) { Current.tenant.analytics? }
|
101
|
+
|
102
|
+
# 3. Declare attributes
|
103
|
+
attributes :user_id
|
104
|
+
required :report_id
|
105
|
+
optional :format_type
|
106
|
+
|
107
|
+
# 4. Define work method
|
108
|
+
def work
|
109
|
+
report.compile!
|
110
|
+
report.export!
|
111
|
+
|
112
|
+
context.exported_at = Time.now
|
113
|
+
end
|
114
|
+
|
115
|
+
# TIP: Favor private business logic to reduce the surface of the public API.
|
116
|
+
private
|
117
|
+
|
118
|
+
# 5. Build helper functions
|
119
|
+
def find_report
|
120
|
+
@report ||= Report.find(report_id)
|
121
|
+
end
|
122
|
+
|
123
|
+
def track_export_metrics
|
124
|
+
Analytics.increment(:report_exported)
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
## Attribute Options
|
74
131
|
|
75
132
|
Use Rails `with_options` to reduce duplication and improve readability:
|
76
133
|
|
77
134
|
```ruby
|
78
|
-
class
|
79
|
-
# Apply common options to multiple
|
135
|
+
class ConfigureCompany < CMDx::Task
|
136
|
+
# Apply common options to multiple attributes
|
80
137
|
with_options(type: :string, presence: true) do
|
81
|
-
|
82
|
-
|
83
|
-
optional :
|
138
|
+
attributes :website, format: { with: URI::DEFAULT_PARSER.make_regexp(%w[http https]) }
|
139
|
+
required :company_name, :industry
|
140
|
+
optional :description, format: { with: /\A[\w\s\-\.,!?]+\z/ }
|
84
141
|
end
|
85
142
|
|
86
|
-
# Nested
|
87
|
-
required :
|
88
|
-
with_options(prefix: :
|
89
|
-
|
143
|
+
# Nested attributes with shared prefix
|
144
|
+
required :headquarters do
|
145
|
+
with_options(prefix: :hq_) do
|
146
|
+
attributes :street, :city, :zip_code, type: :string
|
90
147
|
required :country, type: :string, inclusion: { in: VALID_COUNTRIES }
|
91
|
-
optional :
|
148
|
+
optional :region, type: :string
|
92
149
|
end
|
93
150
|
end
|
94
151
|
|
95
|
-
|
96
|
-
|
97
|
-
optional :age, numericality: { less_than: 150 }
|
98
|
-
optional :years_experience, numericality: { less_than: 80 }
|
99
|
-
end
|
100
|
-
|
101
|
-
def call
|
102
|
-
# Implementation
|
152
|
+
def work
|
153
|
+
# Your logic here...
|
103
154
|
end
|
104
155
|
end
|
105
156
|
```
|
106
157
|
|
107
|
-
##
|
108
|
-
|
109
|
-
### ActiveRecord Query Tagging
|
158
|
+
## ActiveRecord Query Tagging
|
110
159
|
|
111
160
|
Automatically tag SQL queries for better debugging:
|
112
161
|
|
@@ -116,13 +165,14 @@ config.active_record.query_log_tags_enabled = true
|
|
116
165
|
config.active_record.query_log_tags << :cmdx_task_class
|
117
166
|
config.active_record.query_log_tags << :cmdx_chain_id
|
118
167
|
|
119
|
-
# app/
|
168
|
+
# app/tasks/application_task.rb
|
120
169
|
class ApplicationTask < CMDx::Task
|
121
170
|
before_execution :set_execution_context
|
122
171
|
|
123
172
|
private
|
124
173
|
|
125
174
|
def set_execution_context
|
175
|
+
# NOTE: This could easily be made into a middleware
|
126
176
|
ActiveSupport::ExecutionContext.set(
|
127
177
|
cmdx_task_class: self.class.name,
|
128
178
|
cmdx_chain_id: chain.id
|
@@ -131,10 +181,10 @@ class ApplicationTask < CMDx::Task
|
|
131
181
|
end
|
132
182
|
|
133
183
|
# SQL queries will now include comments like:
|
134
|
-
# /*cmdx_task_class:
|
184
|
+
# /*cmdx_task_class:ExportReportTask,cmdx_chain_id:018c2b95-b764-7615*/ SELECT * FROM reports WHERE id = 1
|
135
185
|
```
|
136
186
|
|
137
187
|
---
|
138
188
|
|
139
|
-
- **Prev:** [
|
189
|
+
- **Prev:** [Workflows](workflows.md)
|
140
190
|
- **Next:** [Getting Started](getting_started.md)
|