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
@@ -2,107 +2,76 @@
2
2
 
3
3
  module CMDx
4
4
  module Validators
5
- # Validator class for including values within a specified set.
5
+ # Validates that a value is included in a specified set or range
6
6
  #
7
- # This validator ensures that a value is included in a given array or range
8
- # of allowed values. It supports both discrete value inclusion and range-based
9
- # inclusion validation.
10
- class Inclusion < Validator
7
+ # This validator ensures that the given value is present within a collection
8
+ # of allowed values or falls within a specified range. It supports both
9
+ # discrete value lists and range-based validations.
10
+ module Inclusion
11
11
 
12
- # Validates that the given value is included in the inclusion set.
13
- #
14
- # @param value [Object] the value to validate
15
- # @param options [Hash] validation options containing inclusion configuration
16
- # @option options [Hash] :inclusion inclusion validation configuration
17
- # @option options [Array, Range] :inclusion.in the values to include
18
- # @option options [Array, Range] :inclusion.within alias for :in
19
- # @option options [String] :inclusion.message custom error message
20
- # @option options [String] :inclusion.of_message custom error message for array inclusion
21
- # @option options [String] :inclusion.in_message custom error message for range inclusion
22
- # @option options [String] :inclusion.within_message alias for :in_message
23
- #
24
- # @return [void]
25
- #
26
- # @raise [ValidationError] if the value is not found in the inclusion set
27
- #
28
- # @example Including from an array
29
- # Validators::Inclusion.call("user", inclusion: { in: ["user", "admin"] })
30
- # #=> nil (no error raised)
31
- #
32
- # @example Including from a range
33
- # Validators::Inclusion.call(5, inclusion: { in: 1..10 })
34
- # #=> nil (no error raised)
35
- #
36
- # @example Invalid inclusion from array
37
- # Validators::Inclusion.call("guest", inclusion: { in: ["user", "admin"] })
38
- # # raises ValidationError: "must be one of: \"user\", \"admin\""
39
- #
40
- # @example Invalid inclusion from range
41
- # Validators::Inclusion.call(15, inclusion: { in: 1..10 })
42
- # # raises ValidationError: "must be within 1 and 10"
43
- #
44
- # @example Using a custom message
45
- # Validators::Inclusion.call("guest", inclusion: { in: ["user", "admin"], message: "Invalid role selected" })
46
- # # raises ValidationError: "Invalid role selected"
12
+ extend self
13
+
14
+ # Validates that a value is included in the specified options
15
+ #
16
+ # @param value [Object] The value to validate for inclusion
17
+ # @param options [Hash] Validation configuration options
18
+ # @option options [Array, Range] :in The collection of allowed values or range
19
+ # @option options [Array, Range] :within Alias for :in option
20
+ # @option options [String] :message Custom error message template
21
+ # @option options [String] :of_message Custom message for discrete value inclusions
22
+ # @option options [String] :in_message Custom message for range-based inclusions
23
+ # @option options [String] :within_message Custom message for range-based inclusions
24
+ #
25
+ # @return [nil] Returns nil if validation passes
26
+ #
27
+ # @raise [ValidationError] When the value is not found in the allowed collection
28
+ #
29
+ # @example Include specific values
30
+ # Inclusion.call("admin", in: ["admin", "user", "guest"])
31
+ # # => nil (validation passes)
32
+ # @example Include values within a range
33
+ # Inclusion.call(5, in: 1..10)
34
+ # # => nil (validation passes - 5 is within 1..10)
35
+ # @example Include with custom message
36
+ # Inclusion.call("test", in: ["admin", "user"], message: "must be one of: %{values}")
47
37
  def call(value, options = {})
48
38
  values = options[:in] || options[:within]
49
39
 
50
40
  if values.is_a?(Range)
51
41
  raise_within_validation_error!(values.begin, values.end, options) unless values.cover?(value)
52
- elsif Array(values).none? { |v| v === value } # rubocop:disable Style/CaseEquality
42
+ elsif Array(values).none? { |v| v === value }
53
43
  raise_of_validation_error!(values, options)
54
44
  end
55
45
  end
56
46
 
57
47
  private
58
48
 
59
- # Raises a validation error for array-based inclusion.
60
- #
61
- # @param values [Array] the allowed values
62
- # @param options [Hash] validation options
49
+ # Raises validation error for discrete value inclusions
63
50
  #
64
- # @return [void]
51
+ # @param values [Array] The allowed values that caused the error
52
+ # @param options [Hash] Validation options containing custom messages
65
53
  #
66
- # @raise [ValidationError] always raised with appropriate message
67
- #
68
- # @example
69
- # raise_of_validation_error!(["user", "admin"], {})
70
- # # raises ValidationError: "must be one of: \"user\", \"admin\""
54
+ # @raise [ValidationError] With appropriate error message
71
55
  def raise_of_validation_error!(values, options)
72
- values = values.map(&:inspect).join(", ") unless values.nil?
56
+ values = values.map(&:inspect).join(", ") unless values.nil?
73
57
  message = options[:of_message] || options[:message]
74
58
  message %= { values: } unless message.nil?
75
59
 
76
- raise ValidationError, message || I18n.t(
77
- "cmdx.validators.inclusion.of",
78
- values:,
79
- default: "must be one of: #{values}"
80
- )
60
+ raise ValidationError, message || Locale.t("cmdx.validators.inclusion.of", values:)
81
61
  end
82
62
 
83
- # Raises a validation error for range-based inclusion.
84
- #
85
- # @param min [Object] the minimum value of the range
86
- # @param max [Object] the maximum value of the range
87
- # @param options [Hash] validation options
88
- #
89
- # @return [void]
63
+ # Raises validation error for range-based inclusions
90
64
  #
91
- # @raise [ValidationError] always raised with appropriate message
65
+ # @param min [Object] The minimum value of the allowed range
66
+ # @param max [Object] The maximum value of the allowed range
67
+ # @param options [Hash] Validation options containing custom messages
92
68
  #
93
- # @example
94
- # raise_within_validation_error!(1, 10, {})
95
- # # raises ValidationError: "must be within 1 and 10"
69
+ # @raise [ValidationError] With appropriate error message
96
70
  def raise_within_validation_error!(min, max, options)
97
71
  message = options[:in_message] || options[:within_message] || options[:message]
98
72
  message %= { min:, max: } unless message.nil?
99
73
 
100
- raise ValidationError, message || I18n.t(
101
- "cmdx.validators.inclusion.within",
102
- min:,
103
- max:,
104
- default: "must be within #{min} and #{max}"
105
- )
74
+ raise ValidationError, message || Locale.t("cmdx.validators.inclusion.within", min:, max:)
106
75
  end
107
76
 
108
77
  end
@@ -2,223 +2,160 @@
2
2
 
3
3
  module CMDx
4
4
  module Validators
5
- # Validator class for validating the length of values.
5
+ # Validates the length of a value against various constraints.
6
6
  #
7
- # This validator ensures that a value's length meets specified criteria.
8
- # It supports various length validation options including exact length,
9
- # minimum/maximum bounds, range validation, and exclusion patterns.
10
- class Length < Validator
11
-
12
- # Validates that the given value's length meets the specified criteria.
13
- #
14
- # @param value [Object] the value to validate (must respond to #length)
15
- # @param options [Hash] validation options containing length configuration
16
- # @option options [Hash] :length length validation configuration
17
- # @option options [Range] :length.within acceptable length range
18
- # @option options [Range] :length.not_within unacceptable length range
19
- # @option options [Range] :length.in alias for :within
20
- # @option options [Range] :length.not_in alias for :not_within
21
- # @option options [Integer] :length.min minimum acceptable length
22
- # @option options [Integer] :length.max maximum acceptable length
23
- # @option options [Integer] :length.is exact required length
24
- # @option options [Integer] :length.is_not exact forbidden length
25
- # @option options [String] :length.message custom error message
26
- # @option options [String] :length.within_message custom error message for within validation
27
- # @option options [String] :length.not_within_message custom error message for not_within validation
28
- # @option options [String] :length.in_message alias for :within_message
29
- # @option options [String] :length.not_in_message alias for :not_within_message
30
- # @option options [String] :length.min_message custom error message for minimum validation
31
- # @option options [String] :length.max_message custom error message for maximum validation
32
- # @option options [String] :length.is_message custom error message for exact validation
33
- # @option options [String] :length.is_not_message custom error message for exact exclusion validation
34
- #
35
- # @return [void]
36
- #
37
- # @raise [ValidationError] if the value's length doesn't meet the criteria
38
- # @raise [ArgumentError] if no known length validator options are provided
39
- #
40
- # @example Validating within a range
41
- # Validators::Length.call("hello", length: { within: 1..10 })
42
- # #=> nil (no error raised)
43
- #
44
- # @example Validating minimum length
45
- # Validators::Length.call("hi", length: { min: 5 })
46
- # # raises ValidationError: "length must be at least 5"
47
- #
48
- # @example Validating exact length
49
- # Validators::Length.call("test", length: { is: 4 })
50
- # #=> nil (no error raised)
51
- #
52
- # @example Validating with custom message
53
- # Validators::Length.call("", length: { min: 1, message: "cannot be empty" })
54
- # # raises ValidationError: "cannot be empty"
7
+ # This validator supports multiple length validation strategies including exact length,
8
+ # minimum/maximum bounds, and range-based validation. It can be used to ensure
9
+ # values meet specific length requirements for strings, arrays, and other
10
+ # enumerable objects.
11
+ module Length
12
+
13
+ extend self
14
+
15
+ # Validates a value's length against specified constraints.
16
+ #
17
+ # @param value [String, Array, Hash, Object] The value to validate (must respond to #length)
18
+ # @param options [Hash] Validation options
19
+ # @option options [Range] :within Range that the length must fall within (inclusive)
20
+ # @option options [Range] :not_within Range that the length must not fall within
21
+ # @option options [Range] :in Alias for :within
22
+ # @option options [Range] :not_in Range that the length must not fall within
23
+ # @option options [Integer] :min Minimum allowed length
24
+ # @option options [Integer] :max Maximum allowed length
25
+ # @option options [Integer] :is Exact required length
26
+ # @option options [Integer] :is_not Length that is not allowed
27
+ # @option options [String] :message Custom error message for all validations
28
+ # @option options [String] :within_message Custom message for within/range validations
29
+ # @option options [String] :in_message Custom message for :in validation
30
+ # @option options [String] :not_within_message Custom message for not_within validation
31
+ # @option options [String] :not_in_message Custom message for not_in validation
32
+ # @option options [String] :min_message Custom message for minimum length validation
33
+ # @option options [String] :max_message Custom message for maximum length validation
34
+ # @option options [String] :is_message Custom message for exact length validation
35
+ # @option options [String] :is_not_message Custom message for is_not validation
36
+ #
37
+ # @return [nil] Returns nil if validation passes
38
+ #
39
+ # @raise [ValidationError] When validation fails
40
+ # @raise [ArgumentError] When unknown validation options are provided
41
+ #
42
+ # @example Exact length validation
43
+ # Length.call("hello", is: 5)
44
+ # # => nil (validation passes)
45
+ # @example Range-based validation
46
+ # Length.call("test", within: 3..6)
47
+ # # => nil (validation passes - length 4 is within range)
48
+ # @example Min/max validation
49
+ # Length.call("username", min: 3, max: 20)
50
+ # # => nil (validation passes - length 8 is between 3 and 20)
51
+ # @example Exclusion validation
52
+ # Length.call("short", not_in: 1..3)
53
+ # # => nil (validation passes - length 5 is not in excluded range)
55
54
  def call(value, options = {})
56
55
  case options
57
- in { within: within }
56
+ in within:
58
57
  raise_within_validation_error!(within.begin, within.end, options) unless within.cover?(value.length)
59
- in { not_within: not_within }
58
+ in not_within:
60
59
  raise_not_within_validation_error!(not_within.begin, not_within.end, options) if not_within.cover?(value.length)
61
- in { in: yn }
62
- raise_within_validation_error!(yn.begin, yn.end, options) unless yn.cover?(value.length)
63
- in { not_in: not_in }
60
+ in in: xin
61
+ raise_within_validation_error!(xin.begin, xin.end, options) unless xin.cover?(value.length)
62
+ in not_in:
64
63
  raise_not_within_validation_error!(not_in.begin, not_in.end, options) if not_in.cover?(value.length)
65
- in { min: min, max: max }
64
+ in min:, max:
66
65
  raise_within_validation_error!(min, max, options) unless value.length.between?(min, max)
67
- in { min: min }
66
+ in min:
68
67
  raise_min_validation_error!(min, options) unless min <= value.length
69
- in { max: max }
68
+ in max:
70
69
  raise_max_validation_error!(max, options) unless value.length <= max
71
- in { is: is }
70
+ in is:
72
71
  raise_is_validation_error!(is, options) unless value.length == is
73
- in { is_not: is_not }
72
+ in is_not:
74
73
  raise_is_not_validation_error!(is_not, options) if value.length == is_not
75
74
  else
76
- raise ArgumentError, "no known length validator options given"
75
+ raise ArgumentError, "unknown length validator options given"
77
76
  end
78
77
  end
79
78
 
80
79
  private
81
80
 
82
- # Raises a validation error for within/in range validation.
81
+ # Raises validation error for within/range validations.
83
82
  #
84
- # @param min [Integer] the minimum acceptable length
85
- # @param max [Integer] the maximum acceptable length
86
- # @param options [Hash] validation options
83
+ # @param min [Integer] Minimum length value
84
+ # @param max [Integer] Maximum length value
85
+ # @param options [Hash] Validation options containing custom messages
87
86
  #
88
- # @return [void]
89
- #
90
- # @raise [ValidationError] always raised with appropriate message
91
- #
92
- # @example
93
- # raise_within_validation_error!(5, 10, {})
94
- # # raises ValidationError: "length must be within 5 and 10"
87
+ # @raise [ValidationError] Always raised with appropriate message
95
88
  def raise_within_validation_error!(min, max, options)
96
89
  message = options[:within_message] || options[:in_message] || options[:message]
97
90
  message %= { min:, max: } unless message.nil?
98
91
 
99
- raise ValidationError, message || I18n.t(
100
- "cmdx.validators.length.within",
101
- min:,
102
- max:,
103
- default: "length must be within #{min} and #{max}"
104
- )
92
+ raise ValidationError, message || Locale.t("cmdx.validators.length.within", min:, max:)
105
93
  end
106
94
 
107
- # Raises a validation error for not_within/not_in range validation.
108
- #
109
- # @param min [Integer] the minimum forbidden length
110
- # @param max [Integer] the maximum forbidden length
111
- # @param options [Hash] validation options
95
+ # Raises validation error for not_within validations.
112
96
  #
113
- # @return [void]
97
+ # @param min [Integer] Minimum length value
98
+ # @param max [Integer] Maximum length value
99
+ # @param options [Hash] Validation options containing custom messages
114
100
  #
115
- # @raise [ValidationError] always raised with appropriate message
116
- #
117
- # @example
118
- # raise_not_within_validation_error!(5, 10, {})
119
- # # raises ValidationError: "length must not be within 5 and 10"
101
+ # @raise [ValidationError] Always raised with appropriate message
120
102
  def raise_not_within_validation_error!(min, max, options)
121
103
  message = options[:not_within_message] || options[:not_in_message] || options[:message]
122
104
  message %= { min:, max: } unless message.nil?
123
105
 
124
- raise ValidationError, message || I18n.t(
125
- "cmdx.validators.length.not_within",
126
- min:,
127
- max:,
128
- default: "length must not be within #{min} and #{max}"
129
- )
106
+ raise ValidationError, message || Locale.t("cmdx.validators.length.not_within", min:, max:)
130
107
  end
131
108
 
132
- # Raises a validation error for minimum length validation.
133
- #
134
- # @param min [Integer] the minimum acceptable length
135
- # @param options [Hash] validation options
136
- #
137
- # @return [void]
109
+ # Raises validation error for minimum length validation.
138
110
  #
139
- # @raise [ValidationError] always raised with appropriate message
111
+ # @param min [Integer] Minimum required length
112
+ # @param options [Hash] Validation options containing custom messages
140
113
  #
141
- # @example
142
- # raise_min_validation_error!(5, {})
143
- # # raises ValidationError: "length must be at least 5"
114
+ # @raise [ValidationError] Always raised with appropriate message
144
115
  def raise_min_validation_error!(min, options)
145
116
  message = options[:min_message] || options[:message]
146
117
  message %= { min: } unless message.nil?
147
118
 
148
- raise ValidationError, message || I18n.t(
149
- "cmdx.validators.length.min",
150
- min:,
151
- default: "length must be at least #{min}"
152
- )
119
+ raise ValidationError, message || Locale.t("cmdx.validators.length.min", min:)
153
120
  end
154
121
 
155
- # Raises a validation error for maximum length validation.
122
+ # Raises validation error for maximum length validation.
156
123
  #
157
- # @param max [Integer] the maximum acceptable length
158
- # @param options [Hash] validation options
124
+ # @param max [Integer] Maximum allowed length
125
+ # @param options [Hash] Validation options containing custom messages
159
126
  #
160
- # @return [void]
161
- #
162
- # @raise [ValidationError] always raised with appropriate message
163
- #
164
- # @example
165
- # raise_max_validation_error!(10, {})
166
- # # raises ValidationError: "length must be at most 10"
127
+ # @raise [ValidationError] Always raised with appropriate message
167
128
  def raise_max_validation_error!(max, options)
168
129
  message = options[:max_message] || options[:message]
169
130
  message %= { max: } unless message.nil?
170
131
 
171
- raise ValidationError, message || I18n.t(
172
- "cmdx.validators.length.max",
173
- max:,
174
- default: "length must be at most #{max}"
175
- )
132
+ raise ValidationError, message || Locale.t("cmdx.validators.length.max", max:)
176
133
  end
177
134
 
178
- # Raises a validation error for exact length validation.
179
- #
180
- # @param is [Integer] the exact required length
181
- # @param options [Hash] validation options
135
+ # Raises validation error for exact length validation.
182
136
  #
183
- # @return [void]
137
+ # @param is [Integer] Required exact length
138
+ # @param options [Hash] Validation options containing custom messages
184
139
  #
185
- # @raise [ValidationError] always raised with appropriate message
186
- #
187
- # @example
188
- # raise_is_validation_error!(5, {})
189
- # # raises ValidationError: "length must be 5"
140
+ # @raise [ValidationError] Always raised with appropriate message
190
141
  def raise_is_validation_error!(is, options)
191
142
  message = options[:is_message] || options[:message]
192
143
  message %= { is: } unless message.nil?
193
144
 
194
- raise ValidationError, message || I18n.t(
195
- "cmdx.validators.length.is",
196
- is:,
197
- default: "length must be #{is}"
198
- )
145
+ raise ValidationError, message || Locale.t("cmdx.validators.length.is", is:)
199
146
  end
200
147
 
201
- # Raises a validation error for exact length exclusion validation.
202
- #
203
- # @param is_not [Integer] the exact forbidden length
204
- # @param options [Hash] validation options
205
- #
206
- # @return [void]
148
+ # Raises validation error for is_not length validation.
207
149
  #
208
- # @raise [ValidationError] always raised with appropriate message
150
+ # @param is_not [Integer] Length that is not allowed
151
+ # @param options [Hash] Validation options containing custom messages
209
152
  #
210
- # @example
211
- # raise_is_not_validation_error!(5, {})
212
- # # raises ValidationError: "length must not be 5"
153
+ # @raise [ValidationError] Always raised with appropriate message
213
154
  def raise_is_not_validation_error!(is_not, options)
214
155
  message = options[:is_not_message] || options[:message]
215
156
  message %= { is_not: } unless message.nil?
216
157
 
217
- raise ValidationError, message || I18n.t(
218
- "cmdx.validators.length.is_not",
219
- is_not:,
220
- default: "length must not be #{is_not}"
221
- )
158
+ raise ValidationError, message || Locale.t("cmdx.validators.length.is_not", is_not:)
222
159
  end
223
160
 
224
161
  end