[cnpj-gen] Create package to generate valid CNPJ - #17
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR adds the Changescnpj-gen
cnpj-fmt
Package cleanup
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related issues
Possibly related PRs
Sequence Diagram(s)sequenceDiagram
participant Caller
participant CnpjGen
participant CnpjGenerator
participant CnpjDV
Caller->>CnpjGen: cnpj_gen(options, keywords)
CnpjGen->>CnpjGenerator: create generator and call generate
CnpjGenerator->>CnpjDV: compute check digits
CnpjDV-->>CnpjGenerator: digits or exception
CnpjGenerator-->>Caller: formatted or raw CNPJ
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 8
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cnpj-gen/src/cnpj-gen/cnpj_generator_options.rb`:
- Around line 93-100: apply_initial_options in CnpjGeneratorOptions is returning
before applying the format, prefix, and type keyword overrides when options is a
Hash or CnpjGeneratorOptions. Update the method so
copy_options/apply_hash_options load the base values first, then apply any
non-nil keyword args as overrides instead of dropping them. Also add coverage
around CnpjGeneratorOptions.new and CnpjGenerator#initialize for the
hash-plus-keywords case, e.g. when passing a Hash with format: true.
In `@packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rb`:
- Around line 54-61: The initializer in CnpjGenerator currently ignores keyword
overrides when options is already a CnpjGeneratorOptions instance. Update
CnpjGenerator#initialize to either merge any provided format/prefix/type
keywords into a copied options object when keywords are present, or explicitly
document in the constructor docs that keyword args are ignored in that case;
keep the behavior consistent with the `@param` docs and the CnpjGeneratorOptions
path.
- Around line 129-135: The with_check_digits retry path can recurse forever when
CnpjDV::CnpjCheckDigitsException keeps happening, so add a bounded retry
strategy instead of calling generate blindly. Update
CnpjGenerator#with_check_digits to track attempts and stop after a small
maximum, then fail explicitly or delegate to a private helper that enforces the
limit while preserving the existing generate/options/format/prefix/type flow.
Keep the change localized around with_check_digits and generate so the retry
behavior is safe and predictable.
In `@packages/cnpj-gen/src/cnpj-gen/exceptions.rb`:
- Around line 114-122: The CNPJGen::TypeError initializer currently duplicates
an already-frozen expected_values array, leaving the stored attr_reader
potentially mutable. Update the initialize method in CNPJGen::TypeError to
either keep the original frozen reference directly or, if defensive copying is
intended, freeze the duplicated array before assigning it to `@expected_values` so
callers cannot mutate it through the reader.
In `@packages/cnpj-gen/src/cnpj-gen/generator_options_validation.rb`:
- Around line 73-85: The naming in validate_prefix_non_repeated_digits! is
inconsistent with the sibling validate_prefix_* methods, which all use
partial_cnpj. Rename the parameter and update its local references in
generator_options_validation.rb so the method follows the same naming convention
as the other prefix validators, improving readability and consistency.
In `@packages/cnpj-gen/src/cnpj-gen/types.rb`:
- Around line 12-15: The CNPJ type constants are currently the same frozen
object, so CNPJ_TYPE_OPTIONS_ORDER is only an alias of CNPJ_TYPE_VALUES. Update
the constants in types.rb so CNPJ_TYPE_OPTIONS_ORDER is explicitly independent
if that is intended, using the CNPJ_TYPE_VALUES definition and the
CNPJ_TYPE_OPTIONS_ORDER constant as the reference points; otherwise keep only
one constant if no separate ordering is needed.
In `@packages/cnpj-gen/tests/cnpj_generator_options.spec.rb`:
- Around line 80-97: The current specs for CnpjGeneratorOptions only cover
hash-only and keyword-only initialization, so they miss the hash + keyword
combination that can trigger the apply_initial_options keyword-dropping bug. Add
a new example in cnpj_generator_options.spec.rb that initializes
CnpjGeneratorOptions with both a positional hash and keyword args, then asserts
all expected options are preserved via expect_options_match, using the existing
described_class and apply_initial_options behavior as the target path.
In `@packages/cnpj-gen/tests/exceptions.spec.rb`:
- Around line 1-5: TYPE_INVALID_EXPECTED_VALUES is duplicating the allowed type
list from CnpjGen::CNPJ_TYPE_VALUES, so update the spec to reference the source
constant directly or otherwise derive the expected values from it. If you keep
the local constant intentionally for independent verification in
exceptions.spec.rb, add a brief comment near TYPE_INVALID_EXPECTED_VALUES
explaining that the duplication is deliberate.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 105ccf96-0773-4550-b189-bd9b0962f424
📒 Files selected for processing (21)
packages/br-utilities/Gemfilepackages/cnpj-gen/CHANGELOG.mdpackages/cnpj-gen/Gemfilepackages/cnpj-gen/README.mdpackages/cnpj-gen/README.pt.mdpackages/cnpj-gen/cnpj-gen.gemspecpackages/cnpj-gen/src/cnpj-gen.rbpackages/cnpj-gen/src/cnpj-gen/cnpj_gen.rbpackages/cnpj-gen/src/cnpj-gen/cnpj_generator.rbpackages/cnpj-gen/src/cnpj-gen/cnpj_generator_option_properties.rbpackages/cnpj-gen/src/cnpj-gen/cnpj_generator_options.rbpackages/cnpj-gen/src/cnpj-gen/exceptions.rbpackages/cnpj-gen/src/cnpj-gen/generator_options_validation.rbpackages/cnpj-gen/src/cnpj-gen/types.rbpackages/cnpj-gen/tests/cnpj_gen.spec.rbpackages/cnpj-gen/tests/cnpj_generator.spec.rbpackages/cnpj-gen/tests/cnpj_generator_options.spec.rbpackages/cnpj-gen/tests/exceptions.spec.rbpackages/cnpj-utilities/Gemfilepackages/cnpj-val/Gemfilepackages/cnpj-val/cnpj-val.gemspec
💤 Files with no reviewable changes (4)
- packages/cnpj-gen/Gemfile
- packages/cnpj-val/Gemfile
- packages/br-utilities/Gemfile
- packages/cnpj-utilities/Gemfile
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
- Introduced the CNPJ generator with support for customizable options including prefix, character type, and formatting. - Added validation for generator options and exceptions for invalid configurations. - Created a comprehensive API for generating valid CNPJ identifiers, including helper methods for formatting and error handling. Co-authored-by: Cursor Agent <cursoragent@cursor.com>
- Updated tests for the CNPJ generator to validate generation with various options, including format and prefix. - Introduced new test files for CNPJ generator options and exceptions, ensuring comprehensive coverage of edge cases and error handling. - Added checks for invalid option types and prefixes, improving robustness of the CNPJ generation process. Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
…rposition - Updated the README and CHANGELOG to reflect strict options merging behavior in `CnpjFormatter` and `CnpjFmt.cnpj_fmt`, emphasizing that passing both an `options` argument and keyword arguments raises `InvalidArgumentCombinationError`. - Enhanced error handling documentation for `CnpjFormatterOptions`, detailing the consequences of passing invalid types or combinations. - Removed the `cnpj_formatter_option_properties.rb` file as its functionality has been integrated into `cnpj_formatter_options.rb`. Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
- Replaced the `exceptions` module with a new `errors` module to better categorize error types. - Updated error handling in the CNPJ generator to raise specific errors for type mismatches and invalid argument combinations. - Consolidated option management by integrating properties and resolution helpers into the `CnpjGeneratorOptions` class. - Removed the `cnpj_generator_option_properties.rb` file as its functionality has been merged into the main options class. Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
44453b2 to
835bad9
Compare
- Updated the `cnpj-dv` dependency version to `>= 2.0.0` and `< 2.1.0`. - Changed exception handling in the CNPJ generator from `CnpjCheckDigitsException` to `DomainError` for improved clarity and accuracy in error reporting. - Updated related test context to reflect the new error type. Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rb`:
- Around line 135-136: Update the CnpjDV::DomainError handling around generate
so retries are bounded: move recursive retry behavior into a private helper that
tracks attempts, preserves the existing options and keywords, and raises a
deterministic terminal error after the configured retry limit instead of
recursing indefinitely.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 69c2005c-77c0-4de7-a7a3-aa38a33f06c4
📒 Files selected for processing (4)
packages/cnpj-gen/cnpj-gen.gemspecpackages/cnpj-gen/src/cnpj-gen/cnpj_generator.rbpackages/cnpj-gen/tests/cnpj_generator.spec.rbpackages/cnpj-val/cnpj-val.gemspec
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cnpj-gen/tests/cnpj_generator_options.spec.rb`:
- Around line 46-49: Make invalid-type expectations independent of production
constants: in packages/cnpj-gen/tests/cnpj_generator_options.spec.rb lines
46-49, build CNPJ_GENERATOR_OPTIONS_TYPE_INVALID_MESSAGE from a test-owned
literal values list; in packages/cnpj-gen/tests/errors.spec.rb line 5, replace
CnpjGen::CNPJ_TYPE_VALUES with that same independent expected-values list. Keep
both tests aligned while ensuring implementation constant changes cannot
silently update the expected results.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 735655ee-12b2-4cc8-85a5-e464e50be1ff
📒 Files selected for processing (6)
packages/cnpj-fmt/src/cnpj-fmt/errors.rbpackages/cnpj-gen/src/cnpj-gen/errors.rbpackages/cnpj-gen/src/cnpj-gen/types.rbpackages/cnpj-gen/src/cnpj-gen/utils.rbpackages/cnpj-gen/tests/cnpj_generator_options.spec.rbpackages/cnpj-gen/tests/errors.spec.rb
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Adjustment as per @coderabbitai review comment at #17 (comment). Co-authored-by: CodeRabbit AI <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor Grok 4.5 <cursoragent@cursor.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Summary by CodeRabbit
cnpj-genwith CNPJ generation supportingnumeric,alphabetic, andalphanumerictypes, optional prefixes, masked formatting, and check-digit handling.cnpj-genand clarified option precedence and error behavior incnpj-fmt.InvalidArgumentCombinationError.nilinputs raiseTypeMismatchError.