code_polish 0.1.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.
- checksums.yaml +7 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +8 -0
- data/lib/code_polish/version.rb +5 -0
- data/lib/code_polish.rb +114 -0
- data/sig/code_polish.rbs +4 -0
- metadata +54 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 27730de1db7c157ea7410f34802043ddf74b0219a987afd5279f69be5ce3b8d6
         | 
| 4 | 
            +
              data.tar.gz: c6ed7b9aa1f13e31d6b0b6b909b4b3d39a9872a2e802b8428d63988091ca9806
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 7bb1ae365081d9e22aca75a1a68fd8ad29811712be72e43627715e024a84aac117fbe45cad12c3016c84ecc0cef8a6cac39a322bf51383bd38bee6c1f404e9f8
         | 
| 7 | 
            +
              data.tar.gz: c3dd2ffd792ff042c5a513280d34695264b98b60efa9b36350b70e7bef4678c1ab21c1a85ca3c907cfbd9232e774433e5daf425a5944f5f99872ebff05ed5f01
         | 
    
        data/.rubocop.yml
    ADDED
    
    
    
        data/CHANGELOG.md
    ADDED
    
    
    
        data/CODE_OF_CONDUCT.md
    ADDED
    
    | @@ -0,0 +1,132 @@ | |
| 1 | 
            +
            # Contributor Covenant Code of Conduct
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            ## Our Pledge
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            We as members, contributors, and leaders pledge to make participation in our
         | 
| 6 | 
            +
            community a harassment-free experience for everyone, regardless of age, body
         | 
| 7 | 
            +
            size, visible or invisible disability, ethnicity, sex characteristics, gender
         | 
| 8 | 
            +
            identity and expression, level of experience, education, socio-economic status,
         | 
| 9 | 
            +
            nationality, personal appearance, race, caste, color, religion, or sexual
         | 
| 10 | 
            +
            identity and orientation.
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            We pledge to act and interact in ways that contribute to an open, welcoming,
         | 
| 13 | 
            +
            diverse, inclusive, and healthy community.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            ## Our Standards
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Examples of behavior that contributes to a positive environment for our
         | 
| 18 | 
            +
            community include:
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            * Demonstrating empathy and kindness toward other people
         | 
| 21 | 
            +
            * Being respectful of differing opinions, viewpoints, and experiences
         | 
| 22 | 
            +
            * Giving and gracefully accepting constructive feedback
         | 
| 23 | 
            +
            * Accepting responsibility and apologizing to those affected by our mistakes,
         | 
| 24 | 
            +
              and learning from the experience
         | 
| 25 | 
            +
            * Focusing on what is best not just for us as individuals, but for the overall
         | 
| 26 | 
            +
              community
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            Examples of unacceptable behavior include:
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            * The use of sexualized language or imagery, and sexual attention or advances of
         | 
| 31 | 
            +
              any kind
         | 
| 32 | 
            +
            * Trolling, insulting or derogatory comments, and personal or political attacks
         | 
| 33 | 
            +
            * Public or private harassment
         | 
| 34 | 
            +
            * Publishing others' private information, such as a physical or email address,
         | 
| 35 | 
            +
              without their explicit permission
         | 
| 36 | 
            +
            * Other conduct which could reasonably be considered inappropriate in a
         | 
| 37 | 
            +
              professional setting
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            ## Enforcement Responsibilities
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            Community leaders are responsible for clarifying and enforcing our standards of
         | 
| 42 | 
            +
            acceptable behavior and will take appropriate and fair corrective action in
         | 
| 43 | 
            +
            response to any behavior that they deem inappropriate, threatening, offensive,
         | 
| 44 | 
            +
            or harmful.
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            Community leaders have the right and responsibility to remove, edit, or reject
         | 
| 47 | 
            +
            comments, commits, code, wiki edits, issues, and other contributions that are
         | 
| 48 | 
            +
            not aligned to this Code of Conduct, and will communicate reasons for moderation
         | 
| 49 | 
            +
            decisions when appropriate.
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            ## Scope
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            This Code of Conduct applies within all community spaces, and also applies when
         | 
| 54 | 
            +
            an individual is officially representing the community in public spaces.
         | 
| 55 | 
            +
            Examples of representing our community include using an official email address,
         | 
| 56 | 
            +
            posting via an official social media account, or acting as an appointed
         | 
| 57 | 
            +
            representative at an online or offline event.
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            ## Enforcement
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            Instances of abusive, harassing, or otherwise unacceptable behavior may be
         | 
| 62 | 
            +
            reported to the community leaders responsible for enforcement at
         | 
| 63 | 
            +
            [INSERT CONTACT METHOD].
         | 
| 64 | 
            +
            All complaints will be reviewed and investigated promptly and fairly.
         | 
| 65 | 
            +
             | 
| 66 | 
            +
            All community leaders are obligated to respect the privacy and security of the
         | 
| 67 | 
            +
            reporter of any incident.
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            ## Enforcement Guidelines
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            Community leaders will follow these Community Impact Guidelines in determining
         | 
| 72 | 
            +
            the consequences for any action they deem in violation of this Code of Conduct:
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            ### 1. Correction
         | 
| 75 | 
            +
             | 
| 76 | 
            +
            **Community Impact**: Use of inappropriate language or other behavior deemed
         | 
| 77 | 
            +
            unprofessional or unwelcome in the community.
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            **Consequence**: A private, written warning from community leaders, providing
         | 
| 80 | 
            +
            clarity around the nature of the violation and an explanation of why the
         | 
| 81 | 
            +
            behavior was inappropriate. A public apology may be requested.
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            ### 2. Warning
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            **Community Impact**: A violation through a single incident or series of
         | 
| 86 | 
            +
            actions.
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            **Consequence**: A warning with consequences for continued behavior. No
         | 
| 89 | 
            +
            interaction with the people involved, including unsolicited interaction with
         | 
| 90 | 
            +
            those enforcing the Code of Conduct, for a specified period of time. This
         | 
| 91 | 
            +
            includes avoiding interactions in community spaces as well as external channels
         | 
| 92 | 
            +
            like social media. Violating these terms may lead to a temporary or permanent
         | 
| 93 | 
            +
            ban.
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            ### 3. Temporary Ban
         | 
| 96 | 
            +
             | 
| 97 | 
            +
            **Community Impact**: A serious violation of community standards, including
         | 
| 98 | 
            +
            sustained inappropriate behavior.
         | 
| 99 | 
            +
             | 
| 100 | 
            +
            **Consequence**: A temporary ban from any sort of interaction or public
         | 
| 101 | 
            +
            communication with the community for a specified period of time. No public or
         | 
| 102 | 
            +
            private interaction with the people involved, including unsolicited interaction
         | 
| 103 | 
            +
            with those enforcing the Code of Conduct, is allowed during this period.
         | 
| 104 | 
            +
            Violating these terms may lead to a permanent ban.
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            ### 4. Permanent Ban
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            **Community Impact**: Demonstrating a pattern of violation of community
         | 
| 109 | 
            +
            standards, including sustained inappropriate behavior, harassment of an
         | 
| 110 | 
            +
            individual, or aggression toward or disparagement of classes of individuals.
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            **Consequence**: A permanent ban from any sort of public interaction within the
         | 
| 113 | 
            +
            community.
         | 
| 114 | 
            +
             | 
| 115 | 
            +
            ## Attribution
         | 
| 116 | 
            +
             | 
| 117 | 
            +
            This Code of Conduct is adapted from the [Contributor Covenant][homepage],
         | 
| 118 | 
            +
            version 2.1, available at
         | 
| 119 | 
            +
            [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
         | 
| 120 | 
            +
             | 
| 121 | 
            +
            Community Impact Guidelines were inspired by
         | 
| 122 | 
            +
            [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
         | 
| 123 | 
            +
             | 
| 124 | 
            +
            For answers to common questions about this code of conduct, see the FAQ at
         | 
| 125 | 
            +
            [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
         | 
| 126 | 
            +
            [https://www.contributor-covenant.org/translations][translations].
         | 
| 127 | 
            +
             | 
| 128 | 
            +
            [homepage]: https://www.contributor-covenant.org
         | 
| 129 | 
            +
            [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
         | 
| 130 | 
            +
            [Mozilla CoC]: https://github.com/mozilla/diversity
         | 
| 131 | 
            +
            [FAQ]: https://www.contributor-covenant.org/faq
         | 
| 132 | 
            +
            [translations]: https://www.contributor-covenant.org/translations
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            The MIT License (MIT)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2025 TODO: Write your name
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy
         | 
| 6 | 
            +
            of this software and associated documentation files (the "Software"), to deal
         | 
| 7 | 
            +
            in the Software without restriction, including without limitation the rights
         | 
| 8 | 
            +
            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 9 | 
            +
            copies of the Software, and to permit persons to whom the Software is
         | 
| 10 | 
            +
            furnished to do so, subject to the following conditions:
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            The above copyright notice and this permission notice shall be included in
         | 
| 13 | 
            +
            all copies or substantial portions of the Software.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         | 
| 16 | 
            +
            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         | 
| 17 | 
            +
            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         | 
| 18 | 
            +
            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         | 
| 19 | 
            +
            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         | 
| 20 | 
            +
            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         | 
| 21 | 
            +
            THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            # CodePolish 🚀
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            **CodePolish** is a Ruby gem that provides automated code refactoring suggestions to improve code readability and performance.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## 📌 Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ```ruby
         | 
| 10 | 
            +
            gem 'code_polish'
         | 
| 11 | 
            +
            ```
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            Then install it:
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            ```sh
         | 
| 16 | 
            +
            bundle install
         | 
| 17 | 
            +
            ```
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Or install it manually:
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ```sh
         | 
| 22 | 
            +
            gem install code_polish
         | 
| 23 | 
            +
            ```
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            ---
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            ## 📖 Usage
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            To analyze a Ruby file and get refactoring suggestions:
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            ```ruby
         | 
| 32 | 
            +
            require 'code_polish'
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            CodePolish::Refactor.analyze_code("path/to/your_file.rb")
         | 
| 35 | 
            +
            ```
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            For IRB (Interactive Ruby):
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            ```sh
         | 
| 40 | 
            +
            irb
         | 
| 41 | 
            +
            ```
         | 
| 42 | 
            +
            ```ruby
         | 
| 43 | 
            +
            require 'code_polish'
         | 
| 44 | 
            +
            CodePolish::Refactor.analyze_code("example.rb")
         | 
| 45 | 
            +
            ```
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            ---
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            ## 🔖 Versioning
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            This gem follows **Semantic Versioning (SemVer)**:
         | 
| 52 | 
            +
            - **Major (`1.0.0`)** → Breaking changes
         | 
| 53 | 
            +
            - **Minor (`0.2.0`)** → New features
         | 
| 54 | 
            +
            - **Patch (`0.1.1`)** → Bug fixes
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            ---
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            ## 🤝 Contributing
         | 
| 59 | 
            +
             | 
| 60 | 
            +
            1. Fork the repository.
         | 
| 61 | 
            +
            2. Create a new branch: `git checkout -b feature-branch`
         | 
| 62 | 
            +
            3. Commit your changes: `git commit -m "Added a new feature"`
         | 
| 63 | 
            +
            4. Push to GitHub: `git push origin feature-branch`
         | 
| 64 | 
            +
            5. Create a **Pull Request**.
         | 
| 65 | 
            +
             | 
| 66 | 
            +
            ---
         | 
| 67 | 
            +
             | 
| 68 | 
            +
            ## 📝 License
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            This project is licensed under the **MIT License**. See the `LICENSE` file for details.
         | 
| 71 | 
            +
             | 
| 72 | 
            +
            ---
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            ## 📬 Contact
         | 
| 75 | 
            +
             | 
| 76 | 
            +
            For issues or feedback, open an issue on [GitHub Issues](https://github.com/rahulkush1/code_polish/issues).
         | 
| 77 | 
            +
             | 
    
        data/Rakefile
    ADDED
    
    
    
        data/lib/code_polish.rb
    ADDED
    
    | @@ -0,0 +1,114 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative "code_polish/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module CodePolish
         | 
| 6 | 
            +
              class Refactor
         | 
| 7 | 
            +
                def self.analyze_code(file_or_code)
         | 
| 8 | 
            +
                  if File.exist?(file_or_code)
         | 
| 9 | 
            +
                    code = File.read(file_or_code)
         | 
| 10 | 
            +
                  else
         | 
| 11 | 
            +
                    code = file_or_code  # Handle direct Ruby code
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
                  suggestions = suggest_refactoring(code)
         | 
| 14 | 
            +
                  puts "Refactoring Suggestions:\n#{suggestions}"
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                private
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def self.suggest_refactoring(code)
         | 
| 20 | 
            +
                  suggestions = []
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  # 1️⃣ Use `map` instead of `each` + `push`
         | 
| 23 | 
            +
                  suggestions << "Use `map` instead of `each` + `push`" if code.match?(/\.each\s*do\s*\|.*\|\s*.*\.push/)
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  # 2️⃣ Use safe navigation operator `&.` for nil checks
         | 
| 26 | 
            +
                  suggestions << "Use `&.` (safe navigation operator) for nil checks" if code.match?(/if .*\.nil\?/)
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  # 3️⃣ Use `empty?` instead of `length == 0`
         | 
| 29 | 
            +
                  suggestions << "Use `.empty?` instead of `.length == 0`" if code.match?(/\.length\s*==\s*0/)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  # 4️⃣ Use `present?` instead of `!blank?`
         | 
| 32 | 
            +
                  suggestions << "Use `.present?` instead of `!blank?`" if code.match?(/!\s*.*\.blank\?/)
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  # 5️⃣ Use `one?` instead of `select.count == 1`
         | 
| 35 | 
            +
                  suggestions << "Use `.one?` instead of `.select.count == 1`" if code.match?(/\.select\{.*\}\.count\s*==\s*1/)
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  # 6️⃣ Prefer `find_each` over `each` for large data sets
         | 
| 38 | 
            +
                  suggestions << "Use `.find_each` instead of `.each` for large ActiveRecord queries" if code.match?(/\.each\s*\{\s*\|/)
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  # 7️⃣ Avoid `Time.now`, prefer `Time.current` (Rails best practice)
         | 
| 41 | 
            +
                  suggestions << "Use `Time.current` instead of `Time.now` for Rails apps" if code.match?(/Time\.now/)
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                  # 8️⃣ Avoid using `eval`, use `send` or `public_send`
         | 
| 44 | 
            +
                  suggestions << "Avoid `eval`, use `send` or `public_send` instead" if code.match?(/eval\s*\(/)
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  # 9️⃣ Avoid `puts` in production code, use `Rails.logger`
         | 
| 47 | 
            +
                  suggestions << "Use `Rails.logger` instead of `puts` in production code" if code.match?(/puts/)
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                  # 🔟 Use `String#casecmp?` instead of `.downcase ==`
         | 
| 50 | 
            +
                  suggestions << "Use `.casecmp?` instead of `.downcase ==` for case-insensitive string comparison" if code.match?(/\.downcase\s*==/)
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                  # 1️⃣1️⃣ Avoid `select.first`, use `detect` instead
         | 
| 53 | 
            +
                  suggestions << "Use `.detect` instead of `.select.first`" if code.match?(/\.select\{.*\}\.first/)
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                  # 1️⃣2️⃣ Avoid using `for` loops, prefer `.each`
         | 
| 56 | 
            +
                  suggestions << "Avoid `for` loops, use `.each` instead" if code.match?(/for\s+\w+\s+in\s+/)
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  # 1️⃣3️⃣ Use `dig` instead of nested hash access
         | 
| 59 | 
            +
                  suggestions << "Use `.dig` instead of nested hash access" if code.match?(/\[\s*:.*\]\s*\[\s*:.*\]/)
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  # 1️⃣4️⃣ Prefer `pluck` over `map(&:attr)` for ActiveRecord
         | 
| 62 | 
            +
                  suggestions << "Use `.pluck(:attr)` instead of `.map(&:attr)` for ActiveRecord queries" if code.match?(/\.map\(&:\w+\)/)
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  # 1️⃣5️⃣ Avoid `each_with_index` when index is not needed
         | 
| 65 | 
            +
                  suggestions << "Use `.each` instead of `.each_with_index` if index is not used" if code.match?(/\.each_with_index\s*\{.*\|\w+,\s*\|/)
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  # 1️⃣6️⃣ Use `!exist?` instead of `nil?` for ActiveRecord
         | 
| 68 | 
            +
                  suggestions << "Use `!record.exist?` instead of `record.nil?` in ActiveRecord" if code.match?(/\.nil\?\s*&&\s*.*\.exist\?/)
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                  # 1️⃣7️⃣ Prefer `unless` instead of `if !condition`
         | 
| 71 | 
            +
                  suggestions << "Use `unless` instead of `if !condition`" if code.match?(/if\s+!\s+/)
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                  # 1️⃣8️⃣ Use `compact` to remove nil values instead of `select`
         | 
| 74 | 
            +
                  suggestions << "Use `.compact` instead of `.select { |x| !x.nil? }`" if code.match?(/\.select\s*\{\s*\|.*\|\s*!.*\.nil\?\s*\}/)
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                  # 1️⃣9️⃣ Use `Array.wrap(value)` instead of `[value].flatten`
         | 
| 77 | 
            +
                  suggestions << "Use `Array.wrap(value)` instead of `[value].flatten`" if code.match?(/\[.*\]\.flatten/)
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                  # 2️⃣0️⃣ Prefer `tap` for method chaining instead of `self`
         | 
| 80 | 
            +
                  suggestions << "Use `.tap` instead of explicit `self` in method chaining" if code.match?(/self\./)
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                  # 🆕 **Future Enhancements**
         | 
| 83 | 
            +
                  # ✅ Add more Rails-specific rules
         | 
| 84 | 
            +
                  # ✅ Add more object-oriented programming (OOP) best practices
         | 
| 85 | 
            +
                  # ✅ Add performance optimization suggestions
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  # AI-based analysis (Optional)
         | 
| 88 | 
            +
                  if ENV['OPENAI_API_KEY']
         | 
| 89 | 
            +
                    response = call_openai(code)
         | 
| 90 | 
            +
                    suggestions << response if response
         | 
| 91 | 
            +
                  end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                  suggestions.empty? ? "No improvements found." : suggestions.join("\n")
         | 
| 94 | 
            +
                end
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                def self.call_openai(code)
         | 
| 97 | 
            +
                  require 'openai'
         | 
| 98 | 
            +
                  client = OpenAI::Client.new(api_key: ENV['OPENAI_API_KEY'])
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                  response = client.chat(
         | 
| 101 | 
            +
                    parameters: {
         | 
| 102 | 
            +
                      model: "gpt-4",
         | 
| 103 | 
            +
                      messages: [{ role: "user", content: "Suggest refactoring for this Ruby code:\n#{code}" }],
         | 
| 104 | 
            +
                      temperature: 0.7
         | 
| 105 | 
            +
                    }
         | 
| 106 | 
            +
                  )
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                  response.dig("choices", 0, "message", "content")
         | 
| 109 | 
            +
                rescue => e
         | 
| 110 | 
            +
                  "AI analysis failed: #{e.message}"
         | 
| 111 | 
            +
                end
         | 
| 112 | 
            +
              end
         | 
| 113 | 
            +
            end
         | 
| 114 | 
            +
             | 
    
        data/sig/code_polish.rbs
    ADDED
    
    
    
        metadata
    ADDED
    
    | @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: code_polish
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Rahul Kushwaha
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: exe
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2025-02-05 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: CodePolish helps developers analyze and refactor Ruby code by detecting
         | 
| 14 | 
            +
              common code smells, formatting issues, and performance inefficiencies.
         | 
| 15 | 
            +
            email:
         | 
| 16 | 
            +
            - rahulkush2023@gmail.com
         | 
| 17 | 
            +
            executables: []
         | 
| 18 | 
            +
            extensions: []
         | 
| 19 | 
            +
            extra_rdoc_files: []
         | 
| 20 | 
            +
            files:
         | 
| 21 | 
            +
            - ".rubocop.yml"
         | 
| 22 | 
            +
            - CHANGELOG.md
         | 
| 23 | 
            +
            - CODE_OF_CONDUCT.md
         | 
| 24 | 
            +
            - LICENSE.txt
         | 
| 25 | 
            +
            - README.md
         | 
| 26 | 
            +
            - Rakefile
         | 
| 27 | 
            +
            - lib/code_polish.rb
         | 
| 28 | 
            +
            - lib/code_polish/version.rb
         | 
| 29 | 
            +
            - sig/code_polish.rbs
         | 
| 30 | 
            +
            homepage: https://github.com/rahulkush1/code_polish
         | 
| 31 | 
            +
            licenses:
         | 
| 32 | 
            +
            - MIT
         | 
| 33 | 
            +
            metadata:
         | 
| 34 | 
            +
              homepage_uri: https://github.com/rahulkush1/code_polish
         | 
| 35 | 
            +
            post_install_message: 
         | 
| 36 | 
            +
            rdoc_options: []
         | 
| 37 | 
            +
            require_paths:
         | 
| 38 | 
            +
            - lib
         | 
| 39 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 40 | 
            +
              requirements:
         | 
| 41 | 
            +
              - - ">="
         | 
| 42 | 
            +
                - !ruby/object:Gem::Version
         | 
| 43 | 
            +
                  version: 3.0.0
         | 
| 44 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 45 | 
            +
              requirements:
         | 
| 46 | 
            +
              - - ">="
         | 
| 47 | 
            +
                - !ruby/object:Gem::Version
         | 
| 48 | 
            +
                  version: '0'
         | 
| 49 | 
            +
            requirements: []
         | 
| 50 | 
            +
            rubygems_version: 3.5.22
         | 
| 51 | 
            +
            signing_key: 
         | 
| 52 | 
            +
            specification_version: 4
         | 
| 53 | 
            +
            summary: A lightweight analysis tool for Ruby.
         | 
| 54 | 
            +
            test_files: []
         |