cmdx 1.13.0 → 1.15.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 (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +92 -76
  3. data/LICENSE.txt +3 -20
  4. data/README.md +16 -8
  5. data/lib/cmdx/attribute.rb +42 -5
  6. data/lib/cmdx/context.rb +16 -0
  7. data/lib/cmdx/executor.rb +9 -9
  8. data/lib/cmdx/result.rb +27 -7
  9. data/lib/cmdx/task.rb +19 -0
  10. data/lib/cmdx/validator_registry.rb +1 -0
  11. data/lib/cmdx/validators/absence.rb +61 -0
  12. data/lib/cmdx/version.rb +1 -1
  13. data/lib/locales/af.yml +1 -0
  14. data/lib/locales/ar.yml +1 -0
  15. data/lib/locales/az.yml +1 -0
  16. data/lib/locales/be.yml +1 -0
  17. data/lib/locales/bg.yml +1 -0
  18. data/lib/locales/bn.yml +1 -0
  19. data/lib/locales/bs.yml +1 -0
  20. data/lib/locales/ca.yml +1 -0
  21. data/lib/locales/cnr.yml +1 -0
  22. data/lib/locales/cs.yml +1 -0
  23. data/lib/locales/cy.yml +1 -0
  24. data/lib/locales/da.yml +1 -0
  25. data/lib/locales/de.yml +1 -0
  26. data/lib/locales/dz.yml +1 -0
  27. data/lib/locales/el.yml +1 -0
  28. data/lib/locales/en.yml +1 -0
  29. data/lib/locales/eo.yml +1 -0
  30. data/lib/locales/es.yml +1 -0
  31. data/lib/locales/et.yml +1 -0
  32. data/lib/locales/eu.yml +1 -0
  33. data/lib/locales/fa.yml +1 -0
  34. data/lib/locales/fi.yml +1 -0
  35. data/lib/locales/fr.yml +1 -0
  36. data/lib/locales/fy.yml +1 -0
  37. data/lib/locales/gd.yml +1 -0
  38. data/lib/locales/gl.yml +1 -0
  39. data/lib/locales/he.yml +1 -0
  40. data/lib/locales/hi.yml +1 -0
  41. data/lib/locales/hr.yml +1 -0
  42. data/lib/locales/hu.yml +1 -0
  43. data/lib/locales/hy.yml +1 -0
  44. data/lib/locales/id.yml +1 -0
  45. data/lib/locales/is.yml +1 -0
  46. data/lib/locales/it.yml +1 -0
  47. data/lib/locales/ja.yml +1 -0
  48. data/lib/locales/ka.yml +1 -0
  49. data/lib/locales/kk.yml +1 -0
  50. data/lib/locales/km.yml +1 -0
  51. data/lib/locales/kn.yml +1 -0
  52. data/lib/locales/ko.yml +1 -0
  53. data/lib/locales/lb.yml +1 -0
  54. data/lib/locales/lo.yml +1 -0
  55. data/lib/locales/lt.yml +1 -0
  56. data/lib/locales/lv.yml +1 -0
  57. data/lib/locales/mg.yml +1 -0
  58. data/lib/locales/mk.yml +1 -0
  59. data/lib/locales/ml.yml +1 -0
  60. data/lib/locales/mn.yml +1 -0
  61. data/lib/locales/mr-IN.yml +1 -0
  62. data/lib/locales/ms.yml +1 -0
  63. data/lib/locales/nb.yml +1 -0
  64. data/lib/locales/ne.yml +1 -0
  65. data/lib/locales/nl.yml +1 -0
  66. data/lib/locales/nn.yml +1 -0
  67. data/lib/locales/oc.yml +1 -0
  68. data/lib/locales/or.yml +1 -0
  69. data/lib/locales/pa.yml +1 -0
  70. data/lib/locales/pl.yml +1 -0
  71. data/lib/locales/pt.yml +1 -0
  72. data/lib/locales/rm.yml +1 -0
  73. data/lib/locales/ro.yml +1 -0
  74. data/lib/locales/ru.yml +1 -0
  75. data/lib/locales/sc.yml +1 -0
  76. data/lib/locales/sk.yml +1 -0
  77. data/lib/locales/sl.yml +1 -0
  78. data/lib/locales/sq.yml +1 -0
  79. data/lib/locales/sr.yml +1 -0
  80. data/lib/locales/st.yml +1 -0
  81. data/lib/locales/sv.yml +1 -0
  82. data/lib/locales/sw.yml +1 -0
  83. data/lib/locales/ta.yml +1 -0
  84. data/lib/locales/te.yml +1 -0
  85. data/lib/locales/th.yml +1 -0
  86. data/lib/locales/tl.yml +1 -0
  87. data/lib/locales/tr.yml +1 -0
  88. data/lib/locales/tt.yml +1 -0
  89. data/lib/locales/ug.yml +1 -0
  90. data/lib/locales/uk.yml +1 -0
  91. data/lib/locales/ur.yml +1 -0
  92. data/lib/locales/uz.yml +1 -0
  93. data/lib/locales/vi.yml +1 -0
  94. data/lib/locales/wo.yml +1 -0
  95. data/lib/locales/zh-CN.yml +1 -0
  96. data/lib/locales/zh-HK.yml +1 -0
  97. data/lib/locales/zh-TW.yml +1 -0
  98. data/lib/locales/zh-YUE.yml +1 -0
  99. data/mkdocs.yml +65 -36
  100. metadata +4 -57
  101. data/.cursor/prompts/docs.md +0 -12
  102. data/.cursor/prompts/llms.md +0 -8
  103. data/.cursor/prompts/rspec.md +0 -24
  104. data/.cursor/prompts/yardoc.md +0 -15
  105. data/.cursor/rules/cursor-instructions.mdc +0 -68
  106. data/.irbrc +0 -18
  107. data/.rspec +0 -4
  108. data/.rubocop.yml +0 -95
  109. data/.ruby-version +0 -1
  110. data/.yard-lint.yml +0 -174
  111. data/.yardopts +0 -7
  112. data/docs/.DS_Store +0 -0
  113. data/docs/assets/favicon.ico +0 -0
  114. data/docs/assets/favicon.svg +0 -1
  115. data/docs/attributes/coercions.md +0 -155
  116. data/docs/attributes/defaults.md +0 -77
  117. data/docs/attributes/definitions.md +0 -283
  118. data/docs/attributes/naming.md +0 -68
  119. data/docs/attributes/transformations.md +0 -63
  120. data/docs/attributes/validations.md +0 -336
  121. data/docs/basics/chain.md +0 -108
  122. data/docs/basics/context.md +0 -121
  123. data/docs/basics/execution.md +0 -152
  124. data/docs/basics/setup.md +0 -107
  125. data/docs/callbacks.md +0 -157
  126. data/docs/configuration.md +0 -314
  127. data/docs/deprecation.md +0 -143
  128. data/docs/getting_started.md +0 -137
  129. data/docs/index.md +0 -134
  130. data/docs/internationalization.md +0 -126
  131. data/docs/interruptions/exceptions.md +0 -52
  132. data/docs/interruptions/faults.md +0 -169
  133. data/docs/interruptions/halt.md +0 -216
  134. data/docs/logging.md +0 -90
  135. data/docs/middlewares.md +0 -191
  136. data/docs/outcomes/result.md +0 -197
  137. data/docs/outcomes/states.md +0 -66
  138. data/docs/outcomes/statuses.md +0 -65
  139. data/docs/retries.md +0 -121
  140. data/docs/stylesheets/extra.css +0 -42
  141. data/docs/tips_and_tricks.md +0 -157
  142. data/docs/workflows.md +0 -226
  143. data/examples/active_record_database_transaction.md +0 -27
  144. data/examples/active_record_query_tagging.md +0 -46
  145. data/examples/flipper_feature_flags.md +0 -50
  146. data/examples/paper_trail_whatdunnit.md +0 -39
  147. data/examples/redis_idempotency.md +0 -71
  148. data/examples/sentry_error_tracking.md +0 -46
  149. data/examples/sidekiq_async_execution.md +0 -29
  150. data/examples/stoplight_circuit_breaker.md +0 -36
  151. data/src/cmdx-dark-logo.png +0 -0
  152. data/src/cmdx-favicon.svg +0 -1
  153. data/src/cmdx-light-logo.png +0 -0
  154. data/src/cmdx-logo.svg +0 -1
data/.rubocop.yml DELETED
@@ -1,95 +0,0 @@
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
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/FirstHashElementIndentation:
19
- EnforcedStyle: consistent
20
- Layout/LineLength:
21
- Enabled: false
22
- Lint/MissingSuper:
23
- Exclude:
24
- - 'spec/**/**/*'
25
- Lint/ShadowedException:
26
- Enabled: false
27
- Lint/UnusedMethodArgument:
28
- Exclude:
29
- - 'lib/cmdx/coercions/**/*'
30
- - 'lib/cmdx/log_formatters/**/*'
31
- - 'lib/cmdx/middlewares/**/*'
32
- - 'lib/cmdx/validators/**/*'
33
- Metrics/AbcSize:
34
- Enabled: false
35
- Metrics/BlockLength:
36
- Enabled: false
37
- Metrics/ClassLength:
38
- Enabled: false
39
- Metrics/CyclomaticComplexity:
40
- Enabled: false
41
- Metrics/MethodLength:
42
- Enabled: false
43
- Metrics/ModuleLength:
44
- Enabled: false
45
- Metrics/PerceivedComplexity:
46
- Enabled: false
47
- Naming/MethodParameterName:
48
- Enabled: false
49
- RSpec/DescribeClass:
50
- Exclude:
51
- - 'spec/integrations/**/*'
52
- RSpec/ExampleLength:
53
- Enabled: false
54
- RSpec/IndexedLet:
55
- Enabled: false
56
- RSpec/MessageSpies:
57
- EnforcedStyle: receive
58
- RSpec/MultipleExpectations:
59
- Enabled: false
60
- RSpec/MultipleMemoizedHelpers:
61
- Enabled: false
62
- RSpec/NestedGroups:
63
- Enabled: false
64
- RSpec/SpecFilePathFormat:
65
- CustomTransform:
66
- CMDx: cmdx
67
- RSpec/SubjectStub:
68
- Enabled: false
69
- RSpec/StubbedMock:
70
- Enabled: false
71
- RSpec/VerifiedDoubleReference:
72
- Enabled: false
73
- Style/ArgumentsForwarding:
74
- Exclude:
75
- - 'lib/cmdx/utils/call.rb'
76
- Style/CaseEquality:
77
- Enabled: false
78
- Style/DocumentDynamicEvalDefinition:
79
- Enabled: false
80
- Style/Documentation:
81
- Enabled: false
82
- Style/DoubleNegation:
83
- Enabled: false
84
- Style/FrozenStringLiteralComment:
85
- Enabled: true
86
- EnforcedStyle: always_true
87
- SafeAutoCorrect: true
88
- Style/ModuleFunction:
89
- EnforcedStyle: extend_self
90
- Style/OptionalBooleanParameter:
91
- Enabled: false
92
- Style/StringConcatenation:
93
- Enabled: false
94
- Style/StringLiterals:
95
- EnforcedStyle: double_quotes
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 3.4.5
data/.yard-lint.yml DELETED
@@ -1,174 +0,0 @@
1
- # YARD-Lint Configuration
2
- # See https://github.com/mensfeld/yard-lint for documentation
3
-
4
- # Global settings for all validators
5
- AllValidators:
6
- # YARD command-line options (applied to all validators by default)
7
- YardOptions:
8
- - --private
9
- - --protected
10
-
11
- # Global file exclusion patterns
12
- Exclude:
13
- - '\.git'
14
- - 'vendor/**/*'
15
- - 'node_modules/**/*'
16
- - 'spec/**/*'
17
- - 'test/**/*'
18
-
19
- # Exit code behavior (error, warning, convention, never)
20
- FailOnSeverity: warning
21
-
22
- # Minimum documentation coverage percentage (0-100)
23
- # Fails if coverage is below this threshold
24
- # MinCoverage: 80.0
25
-
26
- # Diff mode settings
27
- DiffMode:
28
- # Default base ref for --diff (auto-detects main/master if not specified)
29
- DefaultBaseRef: ~
30
-
31
- # Documentation validators
32
- Documentation/UndocumentedObjects:
33
- Description: 'Checks for classes, modules, and methods without documentation.'
34
- Enabled: false
35
- Severity: warning
36
- ExcludedMethods:
37
- - 'initialize/0' # Exclude parameter-less initialize
38
- - '/^_/' # Exclude private methods (by convention)
39
-
40
- Documentation/UndocumentedMethodArguments:
41
- Description: 'Checks for method parameters without @param tags.'
42
- Enabled: true
43
- Severity: warning
44
-
45
- Documentation/UndocumentedBooleanMethods:
46
- Description: 'Checks that question mark methods document their boolean return.'
47
- Enabled: true
48
- Severity: warning
49
-
50
- Documentation/UndocumentedOptions:
51
- Description: 'Detects methods with options hash parameters but no @option tags.'
52
- Enabled: true
53
- Severity: warning
54
-
55
- Documentation/MarkdownSyntax:
56
- Description: 'Detects common markdown syntax errors in documentation.'
57
- Enabled: true
58
- Severity: warning
59
-
60
- # Tags validators
61
- Tags/Order:
62
- Description: 'Enforces consistent ordering of YARD tags.'
63
- Enabled: true
64
- Severity: convention
65
- EnforcedOrder:
66
- - param
67
- - option
68
- - return
69
- - raise
70
- - example
71
-
72
- Tags/InvalidTypes:
73
- Description: 'Validates type definitions in @param, @return, @option tags.'
74
- Enabled: true
75
- Severity: warning
76
- ValidatedTags:
77
- - param
78
- - option
79
- - return
80
-
81
- Tags/TypeSyntax:
82
- Description: 'Validates YARD type syntax using YARD parser.'
83
- Enabled: true
84
- Severity: warning
85
- ValidatedTags:
86
- - param
87
- - option
88
- - return
89
- - yieldreturn
90
-
91
- Tags/MeaninglessTag:
92
- Description: 'Detects @param/@option tags on classes, modules, or constants.'
93
- Enabled: true
94
- Severity: warning
95
- CheckedTags:
96
- - param
97
- - option
98
- InvalidObjectTypes:
99
- - class
100
- - module
101
- - constant
102
-
103
- Tags/CollectionType:
104
- Description: 'Validates Hash collection syntax consistency.'
105
- Enabled: true
106
- Severity: convention
107
- EnforcedStyle: long # 'long' for Hash{K => V} (YARD standard), 'short' for {K => V}
108
- ValidatedTags:
109
- - param
110
- - option
111
- - return
112
- - yieldreturn
113
-
114
- Tags/TagTypePosition:
115
- Description: 'Validates type annotation position in tags.'
116
- Enabled: true
117
- Severity: convention
118
- CheckedTags:
119
- - param
120
- - option
121
- # EnforcedStyle: 'type_after_name' (YARD standard: @param name [Type])
122
- # or 'type_first' (@param [Type] name)
123
- EnforcedStyle: type_after_name
124
-
125
- Tags/ApiTags:
126
- Description: 'Enforces @api tags on public objects.'
127
- Enabled: false # Opt-in validator
128
- Severity: warning
129
- AllowedApis:
130
- - public
131
- - private
132
- - internal
133
-
134
- Tags/OptionTags:
135
- Description: 'Requires @option tags for methods with options parameters.'
136
- Enabled: true
137
- Severity: warning
138
-
139
- # Warnings validators - catches YARD parser errors
140
- Warnings/UnknownTag:
141
- Description: 'Detects unknown YARD tags.'
142
- Enabled: false
143
- Severity: error
144
-
145
- Warnings/UnknownDirective:
146
- Description: 'Detects unknown YARD directives.'
147
- Enabled: true
148
- Severity: error
149
-
150
- Warnings/InvalidTagFormat:
151
- Description: 'Detects malformed tag syntax.'
152
- Enabled: true
153
- Severity: error
154
-
155
- Warnings/InvalidDirectiveFormat:
156
- Description: 'Detects malformed directive syntax.'
157
- Enabled: true
158
- Severity: error
159
-
160
- Warnings/DuplicatedParameterName:
161
- Description: 'Detects duplicate @param tags.'
162
- Enabled: true
163
- Severity: error
164
-
165
- Warnings/UnknownParameterName:
166
- Description: 'Detects @param tags for non-existent parameters.'
167
- Enabled: true
168
- Severity: error
169
-
170
- # Semantic validators
171
- Semantic/AbstractMethods:
172
- Description: 'Ensures @abstract methods do not have real implementations.'
173
- Enabled: true
174
- Severity: warning
data/.yardopts DELETED
@@ -1,7 +0,0 @@
1
- --no-private
2
- --output-dir
3
- docs/api
4
- --exclude
5
- lib/generators/**/*.rb
6
- lib/cmdx/railtie.rb
7
- lib/**/*.rb
data/docs/.DS_Store DELETED
Binary file
Binary file
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="1000" height="1000"><g clip-path="url(#SvgjsClipPath1038)"><rect width="1000" height="1000" fill="#ffffff"></rect><g transform="matrix(6.235191420376605,0,0,6.235191420376605,175.46452176081812,150)"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="104.098" height="112.266"><svg width="104.098" height="112.266" viewBox="0 0 60 64.708" xmlns="http://www.w3.org/2000/svg"><path d="M29.907 17.723 26.4 23.17 13.384 3.323h3.507l9.508 14.77 1.938-3.139L18.737 0H7.291L26.4 29.262 45.045 0h-3.97L30 17.54zM9.23 61.293H6.091l18.646-29.447L4.43.093H.46l20.308 31.846L0 64.708l10.985-.092 39.138-61.2h3.323L31.57 37.754l13.662 21.415h3.97L35.445 37.754 59.537.093H48.37zm29.63-23.262 15.047 23.262H43.384l-13.662-20.77-15.508 24.093h3.97l11.63-18 11.723 18H60L41.17 35.354l-.278-.461z" fill="#000"></path></svg></svg></g></g><defs><clipPath id="SvgjsClipPath1038"><rect width="1000" height="1000" x="0" y="0" rx="150" ry="150"></rect></clipPath></defs></svg>
@@ -1,155 +0,0 @@
1
- # Attributes - Coercions
2
-
3
- Automatically convert inputs to expected types. Coercions handle everything from simple string-to-integer conversions to JSON parsing.
4
-
5
- See [Global Configuration](../getting_started.md#coercions) for custom coercion setup.
6
-
7
- ## Usage
8
-
9
- Define attribute types to enable automatic coercion:
10
-
11
- ```ruby
12
- class ParseMetrics < CMDx::Task
13
- # Coerce into a symbol
14
- attribute :measurement_type, type: :symbol
15
-
16
- # Coerce into a rational fallback to big decimal
17
- attribute :value, type: [:rational, :big_decimal]
18
-
19
- # Coerce with options
20
- attribute :recorded_at, type: :date, strptime: "%m-%d-%Y"
21
-
22
- def work
23
- measurement_type #=> :temperature
24
- recorded_at #=> <Date 2024-01-23>
25
- value #=> 98.6 (Float)
26
- end
27
- end
28
-
29
- ParseMetrics.execute(
30
- measurement_type: "temperature",
31
- recorded_at: "01-23-2020",
32
- value: "98.6"
33
- )
34
- ```
35
-
36
- !!! tip
37
-
38
- Specify multiple coercion types for attributes that could be a variety of value formats. CMDx attempts each type in order until one succeeds.
39
-
40
- ## Built-in Coercions
41
-
42
- | Type | Options | Description | Examples |
43
- |------|---------|-------------|----------|
44
- | `:array` | | Array conversion with JSON support | `"val"` → `["val"]`<br>`"[1,2,3]"` → `[1, 2, 3]` |
45
- | `:big_decimal` | `:precision` | High-precision decimal | `"123.456"` → `BigDecimal("123.456")` |
46
- | `:boolean` | | Boolean with text patterns | `"yes"` → `true`, `"no"` → `false` |
47
- | `:complex` | | Complex numbers | `"1+2i"` → `Complex(1, 2)` |
48
- | `:date` | `:strptime` | Date objects | `"2024-01-23"` → `Date.new(2024, 1, 23)` |
49
- | `:datetime` | `:strptime` | DateTime objects | `"2024-01-23 10:30"` → `DateTime.new(2024, 1, 23, 10, 30)` |
50
- | `:float` | | Floating-point numbers | `"123.45"` → `123.45` |
51
- | `:hash` | | Hash conversion with JSON support | `'{"a":1}'` → `{"a" => 1}` |
52
- | `:integer` | | Integer with hex/octal support | `"0xFF"` → `255`, `"077"` → `63` |
53
- | `:rational` | | Rational numbers | `"1/2"` → `Rational(1, 2)` |
54
- | `:string` | | String conversion | `123` → `"123"` |
55
- | `:symbol` | | Symbol conversion | `"abc"` → `:abc` |
56
- | `:time` | `:strptime` | Time objects | `"10:30:00"` → `Time.new(2024, 1, 23, 10, 30)` |
57
-
58
- ## Declarations
59
-
60
- !!! warning "Important"
61
-
62
- Custom coercions must raise `CMDx::CoercionError` with a descriptive message.
63
-
64
- ### Proc or Lambda
65
-
66
- Use anonymous functions for simple coercion logic:
67
-
68
- ```ruby
69
- class TransformCoordinates < CMDx::Task
70
- # Proc
71
- register :callback, :geolocation, proc do |value, options = {}|
72
- begin
73
- Geolocation(value)
74
- rescue StandardError
75
- raise CMDx::CoercionError, "could not convert into a geolocation"
76
- end
77
- end
78
-
79
- # Lambda
80
- register :callback, :geolocation, ->(value, options = {}) {
81
- begin
82
- Geolocation(value)
83
- rescue StandardError
84
- raise CMDx::CoercionError, "could not convert into a geolocation"
85
- end
86
- }
87
- end
88
- ```
89
-
90
- ### Class or Module
91
-
92
- Register custom coercion logic for specialized type handling:
93
-
94
- ```ruby
95
- class GeolocationCoercion
96
- def self.call(value, options = {})
97
- Geolocation(value)
98
- rescue StandardError
99
- raise CMDx::CoercionError, "could not convert into a geolocation"
100
- end
101
- end
102
-
103
- class TransformCoordinates < CMDx::Task
104
- register :coercion, :geolocation, GeolocationCoercion
105
-
106
- attribute :latitude, type: :geolocation
107
- end
108
- ```
109
-
110
- ## Removals
111
-
112
- Remove unwanted coercions:
113
-
114
- !!! warning
115
-
116
- Each `deregister` call removes one coercion. Use multiple calls for batch removals.
117
-
118
- ```ruby
119
- class TransformCoordinates < CMDx::Task
120
- deregister :coercion, :geolocation
121
- end
122
- ```
123
-
124
- ## Error Handling
125
-
126
- Coercion failures provide detailed error information including attribute paths, attempted types, and specific failure reasons:
127
-
128
- ```ruby
129
- class AnalyzePerformance < CMDx::Task
130
- attribute :iterations, type: :integer
131
- attribute :score, type: [:float, :big_decimal]
132
-
133
- def work
134
- # Your logic here...
135
- end
136
- end
137
-
138
- result = AnalyzePerformance.execute(
139
- iterations: "not-a-number",
140
- score: "invalid-float"
141
- )
142
-
143
- result.state #=> "interrupted"
144
- result.status #=> "failed"
145
- result.reason #=> "Invalid"
146
- result.metadata #=> {
147
- # errors: {
148
- # full_message: "iterations could not coerce into an integer. score could not coerce into one of: float, big_decimal.",
149
- # messages: {
150
- # iterations: ["could not coerce into an integer"],
151
- # score: ["could not coerce into one of: float, big_decimal"]
152
- # }
153
- # }
154
- # }
155
- ```
@@ -1,77 +0,0 @@
1
- # Attributes - Defaults
2
-
3
- Provide fallback values for optional attributes. Defaults kick in when values aren't provided or are `nil`.
4
-
5
- ## Declarations
6
-
7
- Defaults work seamlessly with coercions, validations, and nested attributes:
8
-
9
- ### Static Values
10
-
11
- ```ruby
12
- class OptimizeDatabase < CMDx::Task
13
- attribute :strategy, default: :incremental
14
- attribute :level, default: "basic"
15
- attribute :notify_admin, default: true
16
- attribute :timeout_minutes, default: 30
17
- attribute :indexes, default: []
18
- attribute :options, default: {}
19
-
20
- def work
21
- strategy #=> :incremental
22
- level #=> "basic"
23
- notify_admin #=> true
24
- timeout_minutes #=> 30
25
- indexes #=> []
26
- options #=> {}
27
- end
28
- end
29
- ```
30
-
31
- ### Symbol References
32
-
33
- Reference instance methods by symbol for dynamic default values:
34
-
35
- ```ruby
36
- class ProcessAnalytics < CMDx::Task
37
- attribute :granularity, default: :default_granularity
38
-
39
- def work
40
- # Your logic here...
41
- end
42
-
43
- private
44
-
45
- def default_granularity
46
- Current.user.premium? ? "hourly" : "daily"
47
- end
48
- end
49
- ```
50
-
51
- ### Proc or Lambda
52
-
53
- Use anonymous functions for dynamic default values:
54
-
55
- ```ruby
56
- class CacheContent < CMDx::Task
57
- # Proc
58
- attribute :expire_hours, default: proc { Current.tenant.cache_duration || 24 }
59
-
60
- # Lambda
61
- attribute :compression, default: -> { Current.tenant.premium? ? "gzip" : "none" }
62
- end
63
- ```
64
-
65
- ## Coercions and Validations
66
-
67
- Defaults follow the same coercion and validation rules as provided values:
68
-
69
- ```ruby
70
- class ScheduleBackup < CMDx::Task
71
- # Coercions
72
- attribute :retention_days, default: "7", type: :integer
73
-
74
- # Validations
75
- optional :frequency, default: "daily", inclusion: { in: %w[hourly daily weekly monthly] }
76
- end
77
- ```