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.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.cursor/prompts/docs.md +4 -1
  4. data/.cursor/prompts/llms.md +20 -0
  5. data/.cursor/prompts/rspec.md +4 -1
  6. data/.cursor/prompts/yardoc.md +3 -2
  7. data/.cursor/rules/cursor-instructions.mdc +56 -1
  8. data/.irbrc +6 -0
  9. data/.rubocop.yml +29 -18
  10. data/.ruby-version +1 -1
  11. data/CHANGELOG.md +6 -128
  12. data/LLM.md +3317 -0
  13. data/README.md +68 -44
  14. data/docs/attributes/coercions.md +162 -0
  15. data/docs/attributes/defaults.md +90 -0
  16. data/docs/attributes/definitions.md +281 -0
  17. data/docs/attributes/naming.md +78 -0
  18. data/docs/attributes/validations.md +309 -0
  19. data/docs/basics/chain.md +56 -249
  20. data/docs/basics/context.md +56 -289
  21. data/docs/basics/execution.md +114 -0
  22. data/docs/basics/setup.md +37 -334
  23. data/docs/callbacks.md +89 -467
  24. data/docs/deprecation.md +91 -174
  25. data/docs/getting_started.md +212 -202
  26. data/docs/internationalization.md +11 -647
  27. data/docs/interruptions/exceptions.md +23 -198
  28. data/docs/interruptions/faults.md +71 -151
  29. data/docs/interruptions/halt.md +109 -186
  30. data/docs/logging.md +44 -256
  31. data/docs/middlewares.md +113 -426
  32. data/docs/outcomes/result.md +81 -228
  33. data/docs/outcomes/states.md +33 -221
  34. data/docs/outcomes/statuses.md +21 -311
  35. data/docs/tips_and_tricks.md +120 -70
  36. data/docs/workflows.md +99 -283
  37. data/lib/cmdx/.DS_Store +0 -0
  38. data/lib/cmdx/attribute.rb +229 -0
  39. data/lib/cmdx/attribute_registry.rb +94 -0
  40. data/lib/cmdx/attribute_value.rb +193 -0
  41. data/lib/cmdx/callback_registry.rb +69 -77
  42. data/lib/cmdx/chain.rb +56 -73
  43. data/lib/cmdx/coercion_registry.rb +52 -68
  44. data/lib/cmdx/coercions/array.rb +19 -18
  45. data/lib/cmdx/coercions/big_decimal.rb +20 -24
  46. data/lib/cmdx/coercions/boolean.rb +26 -25
  47. data/lib/cmdx/coercions/complex.rb +21 -22
  48. data/lib/cmdx/coercions/date.rb +25 -23
  49. data/lib/cmdx/coercions/date_time.rb +24 -25
  50. data/lib/cmdx/coercions/float.rb +25 -22
  51. data/lib/cmdx/coercions/hash.rb +31 -32
  52. data/lib/cmdx/coercions/integer.rb +30 -24
  53. data/lib/cmdx/coercions/rational.rb +29 -24
  54. data/lib/cmdx/coercions/string.rb +19 -22
  55. data/lib/cmdx/coercions/symbol.rb +37 -0
  56. data/lib/cmdx/coercions/time.rb +26 -25
  57. data/lib/cmdx/configuration.rb +49 -108
  58. data/lib/cmdx/context.rb +222 -44
  59. data/lib/cmdx/deprecator.rb +61 -0
  60. data/lib/cmdx/errors.rb +42 -252
  61. data/lib/cmdx/exceptions.rb +39 -0
  62. data/lib/cmdx/faults.rb +78 -39
  63. data/lib/cmdx/freezer.rb +51 -0
  64. data/lib/cmdx/identifier.rb +30 -0
  65. data/lib/cmdx/locale.rb +52 -0
  66. data/lib/cmdx/log_formatters/json.rb +21 -22
  67. data/lib/cmdx/log_formatters/key_value.rb +20 -22
  68. data/lib/cmdx/log_formatters/line.rb +15 -22
  69. data/lib/cmdx/log_formatters/logstash.rb +22 -23
  70. data/lib/cmdx/log_formatters/raw.rb +16 -22
  71. data/lib/cmdx/middleware_registry.rb +70 -74
  72. data/lib/cmdx/middlewares/correlate.rb +90 -54
  73. data/lib/cmdx/middlewares/runtime.rb +58 -0
  74. data/lib/cmdx/middlewares/timeout.rb +48 -68
  75. data/lib/cmdx/railtie.rb +12 -45
  76. data/lib/cmdx/result.rb +229 -314
  77. data/lib/cmdx/task.rb +194 -366
  78. data/lib/cmdx/utils/call.rb +49 -0
  79. data/lib/cmdx/utils/condition.rb +71 -0
  80. data/lib/cmdx/utils/format.rb +61 -0
  81. data/lib/cmdx/validator_registry.rb +63 -72
  82. data/lib/cmdx/validators/exclusion.rb +38 -67
  83. data/lib/cmdx/validators/format.rb +48 -49
  84. data/lib/cmdx/validators/inclusion.rb +43 -74
  85. data/lib/cmdx/validators/length.rb +91 -154
  86. data/lib/cmdx/validators/numeric.rb +87 -162
  87. data/lib/cmdx/validators/presence.rb +37 -50
  88. data/lib/cmdx/version.rb +1 -1
  89. data/lib/cmdx/worker.rb +178 -0
  90. data/lib/cmdx/workflow.rb +85 -81
  91. data/lib/cmdx.rb +19 -13
  92. data/lib/generators/cmdx/install_generator.rb +14 -13
  93. data/lib/generators/cmdx/task_generator.rb +25 -50
  94. data/lib/generators/cmdx/templates/install.rb +11 -46
  95. data/lib/generators/cmdx/templates/task.rb.tt +3 -2
  96. data/lib/locales/en.yml +18 -4
  97. data/src/cmdx-logo.png +0 -0
  98. metadata +32 -116
  99. data/docs/ai_prompts.md +0 -393
  100. data/docs/basics/call.md +0 -317
  101. data/docs/configuration.md +0 -344
  102. data/docs/parameters/coercions.md +0 -396
  103. data/docs/parameters/defaults.md +0 -335
  104. data/docs/parameters/definitions.md +0 -446
  105. data/docs/parameters/namespacing.md +0 -378
  106. data/docs/parameters/validations.md +0 -405
  107. data/docs/testing.md +0 -553
  108. data/lib/cmdx/callback.rb +0 -53
  109. data/lib/cmdx/chain_inspector.rb +0 -56
  110. data/lib/cmdx/chain_serializer.rb +0 -63
  111. data/lib/cmdx/coercion.rb +0 -57
  112. data/lib/cmdx/coercions/virtual.rb +0 -29
  113. data/lib/cmdx/core_ext/hash.rb +0 -83
  114. data/lib/cmdx/core_ext/module.rb +0 -98
  115. data/lib/cmdx/core_ext/object.rb +0 -125
  116. data/lib/cmdx/correlator.rb +0 -122
  117. data/lib/cmdx/error.rb +0 -60
  118. data/lib/cmdx/fault.rb +0 -140
  119. data/lib/cmdx/immutator.rb +0 -52
  120. data/lib/cmdx/lazy_struct.rb +0 -246
  121. data/lib/cmdx/log_formatters/pretty_json.rb +0 -40
  122. data/lib/cmdx/log_formatters/pretty_key_value.rb +0 -38
  123. data/lib/cmdx/log_formatters/pretty_line.rb +0 -41
  124. data/lib/cmdx/logger.rb +0 -49
  125. data/lib/cmdx/logger_ansi.rb +0 -68
  126. data/lib/cmdx/logger_serializer.rb +0 -116
  127. data/lib/cmdx/middleware.rb +0 -70
  128. data/lib/cmdx/parameter.rb +0 -312
  129. data/lib/cmdx/parameter_evaluator.rb +0 -231
  130. data/lib/cmdx/parameter_inspector.rb +0 -66
  131. data/lib/cmdx/parameter_registry.rb +0 -106
  132. data/lib/cmdx/parameter_serializer.rb +0 -59
  133. data/lib/cmdx/result_ansi.rb +0 -71
  134. data/lib/cmdx/result_inspector.rb +0 -71
  135. data/lib/cmdx/result_logger.rb +0 -59
  136. data/lib/cmdx/result_serializer.rb +0 -104
  137. data/lib/cmdx/rspec/matchers.rb +0 -28
  138. data/lib/cmdx/rspec/result_matchers/be_executed.rb +0 -42
  139. data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +0 -94
  140. data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +0 -94
  141. data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +0 -59
  142. data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +0 -57
  143. data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +0 -87
  144. data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +0 -51
  145. data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +0 -58
  146. data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +0 -59
  147. data/lib/cmdx/rspec/result_matchers/have_context.rb +0 -86
  148. data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +0 -54
  149. data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +0 -52
  150. data/lib/cmdx/rspec/result_matchers/have_metadata.rb +0 -114
  151. data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +0 -66
  152. data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +0 -64
  153. data/lib/cmdx/rspec/result_matchers/have_runtime.rb +0 -78
  154. data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +0 -76
  155. data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +0 -62
  156. data/lib/cmdx/rspec/task_matchers/have_callback.rb +0 -85
  157. data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +0 -68
  158. data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +0 -92
  159. data/lib/cmdx/rspec/task_matchers/have_middleware.rb +0 -46
  160. data/lib/cmdx/rspec/task_matchers/have_parameter.rb +0 -181
  161. data/lib/cmdx/task_deprecator.rb +0 -52
  162. data/lib/cmdx/task_processor.rb +0 -246
  163. data/lib/cmdx/task_serializer.rb +0 -57
  164. data/lib/cmdx/utils/ansi_color.rb +0 -73
  165. data/lib/cmdx/utils/log_timestamp.rb +0 -36
  166. data/lib/cmdx/utils/monotonic_runtime.rb +0 -34
  167. data/lib/cmdx/utils/name_affix.rb +0 -52
  168. data/lib/cmdx/validator.rb +0 -57
  169. data/lib/generators/cmdx/templates/workflow.rb.tt +0 -7
  170. data/lib/generators/cmdx/workflow_generator.rb +0 -84
  171. data/lib/locales/ar.yml +0 -35
  172. data/lib/locales/cs.yml +0 -35
  173. data/lib/locales/da.yml +0 -35
  174. data/lib/locales/de.yml +0 -35
  175. data/lib/locales/el.yml +0 -35
  176. data/lib/locales/es.yml +0 -35
  177. data/lib/locales/fi.yml +0 -35
  178. data/lib/locales/fr.yml +0 -35
  179. data/lib/locales/he.yml +0 -35
  180. data/lib/locales/hi.yml +0 -35
  181. data/lib/locales/it.yml +0 -35
  182. data/lib/locales/ja.yml +0 -35
  183. data/lib/locales/ko.yml +0 -35
  184. data/lib/locales/nl.yml +0 -35
  185. data/lib/locales/no.yml +0 -35
  186. data/lib/locales/pl.yml +0 -35
  187. data/lib/locales/pt.yml +0 -35
  188. data/lib/locales/ru.yml +0 -35
  189. data/lib/locales/sv.yml +0 -35
  190. data/lib/locales/th.yml +0 -35
  191. data/lib/locales/tr.yml +0 -35
  192. data/lib/locales/vi.yml +0 -35
  193. data/lib/locales/zh.yml +0 -35
@@ -1,25 +1,16 @@
1
- # Tips & Tricks
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
- - [Parameter Optimization](#parameter-optimization)
12
- - [Efficient Parameter Definitions](#efficient-parameter-definitions)
13
- - [Monitoring and Observability](#monitoring-and-observability)
14
- - [ActiveRecord Query Tagging](#activerecord-query-tagging)
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
- ```txt
31
- /app
32
- /commands
33
- /orders
34
- - process_order_task.rb
35
- - validate_order_task.rb
36
- - fulfill_order_task.rb
37
- - order_processing_workflow.rb
38
- /notifications
39
- - send_email_task.rb
40
- - send_sms_task.rb
41
- - post_slack_message_task.rb
42
- - notification_delivery_workflow.rb
43
- /payments
44
- - charge_payment_task.rb
45
- - refund_payment_task.rb
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
- # Tasks: Verb + Noun + Task
57
- class ProcessOrderTask < CMDx::Task; end
58
- class SendEmailTask < CMDx::Task; end
59
- class ValidatePaymentTask < CMDx::Task; end
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 CreateUserTask < CMDx::Task; end # ✓ Good
67
- class CreatingUserTask < CMDx::Task; end # ❌ Avoid
68
- class UserCreationTask < CMDx::Task; end # ❌ Avoid
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
- ## Parameter Optimization
87
+ ### Style Guide
88
+
89
+ Follow a style pattern for consistent task design:
72
90
 
73
- ### Efficient Parameter Definitions
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 UpdateUserProfileTask < CMDx::Task
79
- # Apply common options to multiple parameters
135
+ class ConfigureCompany < CMDx::Task
136
+ # Apply common options to multiple attributes
80
137
  with_options(type: :string, presence: true) do
81
- required :email, format: { with: URI::MailTo::EMAIL_REGEXP }
82
- optional :first_name, :last_name
83
- optional :phone, format: { with: /\A\+?[\d\s\-\(\)]+\z/ }
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 parameters with shared prefix
87
- required :address do
88
- with_options(prefix: :address_) do
89
- required :street, :city, :postal_code, type: :string
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 :state, type: :string
148
+ optional :region, type: :string
92
149
  end
93
150
  end
94
151
 
95
- # Shared validation rules
96
- with_options(type: :integer, numericality: { greater_than: 0 }) do
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
- ## Monitoring and Observability
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/commands/application_task.rb
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:ProcessOrderTask,cmdx_chain_id:018c2b95-b764-7615*/ SELECT * FROM orders WHERE id = 1
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:** [AI Prompts](ai_prompts.md)
189
+ - **Prev:** [Workflows](workflows.md)
140
190
  - **Next:** [Getting Started](getting_started.md)