cmdx 1.1.2 → 1.5.1

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 (192) 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 +55 -1
  8. data/.irbrc +6 -0
  9. data/.rubocop.yml +29 -18
  10. data/CHANGELOG.md +11 -132
  11. data/LLM.md +3317 -0
  12. data/README.md +68 -44
  13. data/docs/attributes/coercions.md +162 -0
  14. data/docs/attributes/defaults.md +90 -0
  15. data/docs/attributes/definitions.md +281 -0
  16. data/docs/attributes/naming.md +78 -0
  17. data/docs/attributes/validations.md +309 -0
  18. data/docs/basics/chain.md +56 -249
  19. data/docs/basics/context.md +56 -289
  20. data/docs/basics/execution.md +114 -0
  21. data/docs/basics/setup.md +37 -334
  22. data/docs/callbacks.md +89 -467
  23. data/docs/deprecation.md +91 -174
  24. data/docs/getting_started.md +212 -202
  25. data/docs/internationalization.md +11 -647
  26. data/docs/interruptions/exceptions.md +23 -198
  27. data/docs/interruptions/faults.md +71 -151
  28. data/docs/interruptions/halt.md +109 -186
  29. data/docs/logging.md +44 -256
  30. data/docs/middlewares.md +113 -426
  31. data/docs/outcomes/result.md +81 -228
  32. data/docs/outcomes/states.md +33 -221
  33. data/docs/outcomes/statuses.md +21 -311
  34. data/docs/tips_and_tricks.md +120 -70
  35. data/docs/workflows.md +99 -283
  36. data/lib/cmdx/.DS_Store +0 -0
  37. data/lib/cmdx/attribute.rb +229 -0
  38. data/lib/cmdx/attribute_registry.rb +94 -0
  39. data/lib/cmdx/attribute_value.rb +193 -0
  40. data/lib/cmdx/callback_registry.rb +69 -77
  41. data/lib/cmdx/chain.rb +56 -73
  42. data/lib/cmdx/coercion_registry.rb +52 -68
  43. data/lib/cmdx/coercions/array.rb +19 -18
  44. data/lib/cmdx/coercions/big_decimal.rb +20 -24
  45. data/lib/cmdx/coercions/boolean.rb +26 -25
  46. data/lib/cmdx/coercions/complex.rb +21 -22
  47. data/lib/cmdx/coercions/date.rb +25 -23
  48. data/lib/cmdx/coercions/date_time.rb +24 -25
  49. data/lib/cmdx/coercions/float.rb +25 -22
  50. data/lib/cmdx/coercions/hash.rb +31 -32
  51. data/lib/cmdx/coercions/integer.rb +30 -24
  52. data/lib/cmdx/coercions/rational.rb +29 -24
  53. data/lib/cmdx/coercions/string.rb +19 -22
  54. data/lib/cmdx/coercions/symbol.rb +37 -0
  55. data/lib/cmdx/coercions/time.rb +26 -25
  56. data/lib/cmdx/configuration.rb +49 -108
  57. data/lib/cmdx/context.rb +222 -44
  58. data/lib/cmdx/deprecator.rb +61 -0
  59. data/lib/cmdx/errors.rb +42 -252
  60. data/lib/cmdx/exceptions.rb +39 -0
  61. data/lib/cmdx/faults.rb +78 -39
  62. data/lib/cmdx/freezer.rb +51 -0
  63. data/lib/cmdx/identifier.rb +30 -0
  64. data/lib/cmdx/locale.rb +52 -0
  65. data/lib/cmdx/log_formatters/json.rb +21 -22
  66. data/lib/cmdx/log_formatters/key_value.rb +20 -22
  67. data/lib/cmdx/log_formatters/line.rb +15 -22
  68. data/lib/cmdx/log_formatters/logstash.rb +22 -23
  69. data/lib/cmdx/log_formatters/raw.rb +16 -22
  70. data/lib/cmdx/middleware_registry.rb +70 -74
  71. data/lib/cmdx/middlewares/correlate.rb +90 -54
  72. data/lib/cmdx/middlewares/runtime.rb +58 -0
  73. data/lib/cmdx/middlewares/timeout.rb +48 -68
  74. data/lib/cmdx/railtie.rb +12 -45
  75. data/lib/cmdx/result.rb +229 -314
  76. data/lib/cmdx/task.rb +194 -366
  77. data/lib/cmdx/utils/call.rb +49 -0
  78. data/lib/cmdx/utils/condition.rb +71 -0
  79. data/lib/cmdx/utils/format.rb +61 -0
  80. data/lib/cmdx/validator_registry.rb +63 -72
  81. data/lib/cmdx/validators/exclusion.rb +38 -67
  82. data/lib/cmdx/validators/format.rb +48 -49
  83. data/lib/cmdx/validators/inclusion.rb +43 -74
  84. data/lib/cmdx/validators/length.rb +101 -162
  85. data/lib/cmdx/validators/numeric.rb +95 -170
  86. data/lib/cmdx/validators/presence.rb +37 -50
  87. data/lib/cmdx/version.rb +1 -1
  88. data/lib/cmdx/worker.rb +178 -0
  89. data/lib/cmdx/workflow.rb +85 -81
  90. data/lib/cmdx.rb +19 -13
  91. data/lib/generators/cmdx/install_generator.rb +14 -13
  92. data/lib/generators/cmdx/task_generator.rb +25 -50
  93. data/lib/generators/cmdx/templates/install.rb +11 -46
  94. data/lib/generators/cmdx/templates/task.rb.tt +3 -2
  95. data/lib/locales/en.yml +18 -4
  96. data/src/cmdx-logo.png +0 -0
  97. metadata +32 -116
  98. data/docs/ai_prompts.md +0 -393
  99. data/docs/basics/call.md +0 -317
  100. data/docs/configuration.md +0 -344
  101. data/docs/parameters/coercions.md +0 -396
  102. data/docs/parameters/defaults.md +0 -335
  103. data/docs/parameters/definitions.md +0 -446
  104. data/docs/parameters/namespacing.md +0 -378
  105. data/docs/parameters/validations.md +0 -405
  106. data/docs/testing.md +0 -553
  107. data/lib/cmdx/callback.rb +0 -53
  108. data/lib/cmdx/chain_inspector.rb +0 -56
  109. data/lib/cmdx/chain_serializer.rb +0 -63
  110. data/lib/cmdx/coercion.rb +0 -57
  111. data/lib/cmdx/coercions/virtual.rb +0 -29
  112. data/lib/cmdx/core_ext/hash.rb +0 -83
  113. data/lib/cmdx/core_ext/module.rb +0 -98
  114. data/lib/cmdx/core_ext/object.rb +0 -125
  115. data/lib/cmdx/correlator.rb +0 -122
  116. data/lib/cmdx/error.rb +0 -67
  117. data/lib/cmdx/fault.rb +0 -140
  118. data/lib/cmdx/immutator.rb +0 -52
  119. data/lib/cmdx/lazy_struct.rb +0 -246
  120. data/lib/cmdx/log_formatters/pretty_json.rb +0 -40
  121. data/lib/cmdx/log_formatters/pretty_key_value.rb +0 -38
  122. data/lib/cmdx/log_formatters/pretty_line.rb +0 -41
  123. data/lib/cmdx/logger.rb +0 -49
  124. data/lib/cmdx/logger_ansi.rb +0 -68
  125. data/lib/cmdx/logger_serializer.rb +0 -116
  126. data/lib/cmdx/middleware.rb +0 -70
  127. data/lib/cmdx/parameter.rb +0 -312
  128. data/lib/cmdx/parameter_evaluator.rb +0 -231
  129. data/lib/cmdx/parameter_inspector.rb +0 -66
  130. data/lib/cmdx/parameter_registry.rb +0 -106
  131. data/lib/cmdx/parameter_serializer.rb +0 -59
  132. data/lib/cmdx/result_ansi.rb +0 -71
  133. data/lib/cmdx/result_inspector.rb +0 -71
  134. data/lib/cmdx/result_logger.rb +0 -59
  135. data/lib/cmdx/result_serializer.rb +0 -104
  136. data/lib/cmdx/rspec/matchers.rb +0 -28
  137. data/lib/cmdx/rspec/result_matchers/be_executed.rb +0 -42
  138. data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +0 -94
  139. data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +0 -94
  140. data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +0 -59
  141. data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +0 -57
  142. data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +0 -87
  143. data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +0 -51
  144. data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +0 -58
  145. data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +0 -59
  146. data/lib/cmdx/rspec/result_matchers/have_context.rb +0 -86
  147. data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +0 -54
  148. data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +0 -52
  149. data/lib/cmdx/rspec/result_matchers/have_metadata.rb +0 -114
  150. data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +0 -66
  151. data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +0 -64
  152. data/lib/cmdx/rspec/result_matchers/have_runtime.rb +0 -78
  153. data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +0 -76
  154. data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +0 -62
  155. data/lib/cmdx/rspec/task_matchers/have_callback.rb +0 -85
  156. data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +0 -68
  157. data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +0 -92
  158. data/lib/cmdx/rspec/task_matchers/have_middleware.rb +0 -46
  159. data/lib/cmdx/rspec/task_matchers/have_parameter.rb +0 -181
  160. data/lib/cmdx/task_deprecator.rb +0 -58
  161. data/lib/cmdx/task_processor.rb +0 -246
  162. data/lib/cmdx/task_serializer.rb +0 -57
  163. data/lib/cmdx/utils/ansi_color.rb +0 -73
  164. data/lib/cmdx/utils/log_timestamp.rb +0 -36
  165. data/lib/cmdx/utils/monotonic_runtime.rb +0 -34
  166. data/lib/cmdx/utils/name_affix.rb +0 -52
  167. data/lib/cmdx/validator.rb +0 -57
  168. data/lib/generators/cmdx/templates/workflow.rb.tt +0 -7
  169. data/lib/generators/cmdx/workflow_generator.rb +0 -84
  170. data/lib/locales/ar.yml +0 -35
  171. data/lib/locales/cs.yml +0 -35
  172. data/lib/locales/da.yml +0 -35
  173. data/lib/locales/de.yml +0 -35
  174. data/lib/locales/el.yml +0 -35
  175. data/lib/locales/es.yml +0 -35
  176. data/lib/locales/fi.yml +0 -35
  177. data/lib/locales/fr.yml +0 -35
  178. data/lib/locales/he.yml +0 -35
  179. data/lib/locales/hi.yml +0 -35
  180. data/lib/locales/it.yml +0 -35
  181. data/lib/locales/ja.yml +0 -35
  182. data/lib/locales/ko.yml +0 -35
  183. data/lib/locales/nl.yml +0 -35
  184. data/lib/locales/no.yml +0 -35
  185. data/lib/locales/pl.yml +0 -35
  186. data/lib/locales/pt.yml +0 -35
  187. data/lib/locales/ru.yml +0 -35
  188. data/lib/locales/sv.yml +0 -35
  189. data/lib/locales/th.yml +0 -35
  190. data/lib/locales/tr.yml +0 -35
  191. data/lib/locales/vi.yml +0 -35
  192. 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,162 @@
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 = {})
55
+ length = value&.length
56
+
56
57
  case options
57
- in { within: within }
58
- raise_within_validation_error!(within.begin, within.end, options) unless within.cover?(value.length)
59
- in { not_within: not_within }
60
- 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 }
64
- raise_not_within_validation_error!(not_in.begin, not_in.end, options) if not_in.cover?(value.length)
65
- in { min: min, max: max }
66
- raise_within_validation_error!(min, max, options) unless value.length.between?(min, max)
67
- in { min: min }
68
- raise_min_validation_error!(min, options) unless min <= value.length
69
- in { max: max }
70
- raise_max_validation_error!(max, options) unless value.length <= max
71
- in { is: is }
72
- raise_is_validation_error!(is, options) unless value.length == is
73
- in { is_not: is_not }
74
- raise_is_not_validation_error!(is_not, options) if value.length == is_not
58
+ in within:
59
+ raise_within_validation_error!(within.begin, within.end, options) unless within&.cover?(length)
60
+ in not_within:
61
+ raise_not_within_validation_error!(not_within.begin, not_within.end, options) if not_within&.cover?(length)
62
+ in in: xin
63
+ raise_within_validation_error!(xin.begin, xin.end, options) unless xin&.cover?(length)
64
+ in not_in:
65
+ raise_not_within_validation_error!(not_in.begin, not_in.end, options) if not_in&.cover?(length)
66
+ in min:, max:
67
+ raise_within_validation_error!(min, max, options) unless length&.between?(min, max)
68
+ in min:
69
+ raise_min_validation_error!(min, options) unless !length.nil? && (min <= length)
70
+ in max:
71
+ raise_max_validation_error!(max, options) unless !length.nil? && (length <= max)
72
+ in is:
73
+ raise_is_validation_error!(is, options) unless !length.nil? && (length == is)
74
+ in is_not:
75
+ raise_is_not_validation_error!(is_not, options) if !length.nil? && (length == is_not)
75
76
  else
76
- raise ArgumentError, "no known length validator options given"
77
+ raise ArgumentError, "unknown length validator options given"
77
78
  end
78
79
  end
79
80
 
80
81
  private
81
82
 
82
- # Raises a validation error for within/in range validation.
83
- #
84
- # @param min [Integer] the minimum acceptable length
85
- # @param max [Integer] the maximum acceptable length
86
- # @param options [Hash] validation options
83
+ # Raises validation error for within/range validations.
87
84
  #
88
- # @return [void]
85
+ # @param min [Integer] Minimum length value
86
+ # @param max [Integer] Maximum length value
87
+ # @param options [Hash] Validation options containing custom messages
89
88
  #
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"
89
+ # @raise [ValidationError] Always raised with appropriate message
95
90
  def raise_within_validation_error!(min, max, options)
96
91
  message = options[:within_message] || options[:in_message] || options[:message]
97
92
  message %= { min:, max: } unless message.nil?
98
93
 
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
- )
94
+ raise ValidationError, message || Locale.t("cmdx.validators.length.within", min:, max:)
105
95
  end
106
96
 
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
97
+ # Raises validation error for not_within validations.
112
98
  #
113
- # @return [void]
99
+ # @param min [Integer] Minimum length value
100
+ # @param max [Integer] Maximum length value
101
+ # @param options [Hash] Validation options containing custom messages
114
102
  #
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"
103
+ # @raise [ValidationError] Always raised with appropriate message
120
104
  def raise_not_within_validation_error!(min, max, options)
121
105
  message = options[:not_within_message] || options[:not_in_message] || options[:message]
122
106
  message %= { min:, max: } unless message.nil?
123
107
 
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
- )
108
+ raise ValidationError, message || Locale.t("cmdx.validators.length.not_within", min:, max:)
130
109
  end
131
110
 
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]
111
+ # Raises validation error for minimum length validation.
138
112
  #
139
- # @raise [ValidationError] always raised with appropriate message
113
+ # @param min [Integer] Minimum required length
114
+ # @param options [Hash] Validation options containing custom messages
140
115
  #
141
- # @example
142
- # raise_min_validation_error!(5, {})
143
- # # raises ValidationError: "length must be at least 5"
116
+ # @raise [ValidationError] Always raised with appropriate message
144
117
  def raise_min_validation_error!(min, options)
145
118
  message = options[:min_message] || options[:message]
146
119
  message %= { min: } unless message.nil?
147
120
 
148
- raise ValidationError, message || I18n.t(
149
- "cmdx.validators.length.min",
150
- min:,
151
- default: "length must be at least #{min}"
152
- )
121
+ raise ValidationError, message || Locale.t("cmdx.validators.length.min", min:)
153
122
  end
154
123
 
155
- # Raises a validation error for maximum length validation.
124
+ # Raises validation error for maximum length validation.
156
125
  #
157
- # @param max [Integer] the maximum acceptable length
158
- # @param options [Hash] validation options
126
+ # @param max [Integer] Maximum allowed length
127
+ # @param options [Hash] Validation options containing custom messages
159
128
  #
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"
129
+ # @raise [ValidationError] Always raised with appropriate message
167
130
  def raise_max_validation_error!(max, options)
168
131
  message = options[:max_message] || options[:message]
169
132
  message %= { max: } unless message.nil?
170
133
 
171
- raise ValidationError, message || I18n.t(
172
- "cmdx.validators.length.max",
173
- max:,
174
- default: "length must be at most #{max}"
175
- )
134
+ raise ValidationError, message || Locale.t("cmdx.validators.length.max", max:)
176
135
  end
177
136
 
178
- # Raises a validation error for exact length validation.
179
- #
180
- # @param is [Integer] the exact required length
181
- # @param options [Hash] validation options
137
+ # Raises validation error for exact length validation.
182
138
  #
183
- # @return [void]
139
+ # @param is [Integer] Required exact length
140
+ # @param options [Hash] Validation options containing custom messages
184
141
  #
185
- # @raise [ValidationError] always raised with appropriate message
186
- #
187
- # @example
188
- # raise_is_validation_error!(5, {})
189
- # # raises ValidationError: "length must be 5"
142
+ # @raise [ValidationError] Always raised with appropriate message
190
143
  def raise_is_validation_error!(is, options)
191
144
  message = options[:is_message] || options[:message]
192
145
  message %= { is: } unless message.nil?
193
146
 
194
- raise ValidationError, message || I18n.t(
195
- "cmdx.validators.length.is",
196
- is:,
197
- default: "length must be #{is}"
198
- )
147
+ raise ValidationError, message || Locale.t("cmdx.validators.length.is", is:)
199
148
  end
200
149
 
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]
150
+ # Raises validation error for is_not length validation.
207
151
  #
208
- # @raise [ValidationError] always raised with appropriate message
152
+ # @param is_not [Integer] Length that is not allowed
153
+ # @param options [Hash] Validation options containing custom messages
209
154
  #
210
- # @example
211
- # raise_is_not_validation_error!(5, {})
212
- # # raises ValidationError: "length must not be 5"
155
+ # @raise [ValidationError] Always raised with appropriate message
213
156
  def raise_is_not_validation_error!(is_not, options)
214
157
  message = options[:is_not_message] || options[:message]
215
158
  message %= { is_not: } unless message.nil?
216
159
 
217
- raise ValidationError, message || I18n.t(
218
- "cmdx.validators.length.is_not",
219
- is_not:,
220
- default: "length must not be #{is_not}"
221
- )
160
+ raise ValidationError, message || Locale.t("cmdx.validators.length.is_not", is_not:)
222
161
  end
223
162
 
224
163
  end