{"id":15064647,"url":"https://github.com/coreyjs/ruby-mutant","last_synced_at":"2025-08-18T19:31:26.805Z","repository":{"id":56893575,"uuid":"152251169","full_name":"coreyjs/ruby-mutant","owner":"coreyjs","description":"Encapsulate your business logic into reusable mutations.","archived":false,"fork":false,"pushed_at":"2023-05-18T00:05:15.000Z","size":142,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-15T07:35:42.853Z","etag":null,"topics":["hacktoberfest","mutations","ruby","ruby-on-rails","rubygem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coreyjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-09T12:56:48.000Z","updated_at":"2024-11-18T15:46:23.000Z","dependencies_parsed_at":"2024-09-25T00:24:24.019Z","dependency_job_id":"763d733d-1e8b-4a88-92ed-d750ff30e399","html_url":"https://github.com/coreyjs/ruby-mutant","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreyjs%2Fruby-mutant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreyjs%2Fruby-mutant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreyjs%2Fruby-mutant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreyjs%2Fruby-mutant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coreyjs","download_url":"https://codeload.github.com/coreyjs/ruby-mutant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230267097,"owners_count":18199615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","mutations","ruby","ruby-on-rails","rubygem"],"created_at":"2024-09-25T00:23:30.499Z","updated_at":"2025-08-18T19:31:26.787Z","avatar_url":"https://github.com/coreyjs.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ruby Mutant\n\n[![Gem Version](https://badge.fury.io/rb/ruby-mutant.svg)](https://badge.fury.io/rb/ruby-mutant)\n\n\n## Installation\n\n\n```ruby\ngem 'ruby-mutant', '~\u003e 1.0.1'\n```\n\n---\n## Usage\n\nRuby Mutant is a lightweight mutations library to help you encapsulate your business logic into decoupled, testable mutations.  With Ruby Mutant you can easily add executable code with validation, helping you decouple important logic from your application.\n\nTo create a mutation from a ruby object, include the ruby module `require \"mutant\"`  \n\n\n\n```ruby\nrequire \"mutant\"\n\nclass RecipeCreatedMutation\n  include Mutant\n  \n  required_attr :recipe\n\n    #Define custom validators for our attributes\n    def validate_name?\n        name.nil? || name.empty?\n    end\n\n    # Required, this will execute our new mutation.\n    def execute(args)\n        # here, recipe is passe into out Class.run(recipe=Recipe.new) method\n        if recipe.difficulty == 'godlike'\n          recipe_service.send_alert_new_super_recipe_confirmation()\n        end \n    end\nend\n```\n\n---\nTo run a mutation definition:\n```ruby\n# run() excepts any number of parameters that you want to pass into your mutation\noutput = RecipeCreatedMutation.run(recipe: Recipe.new, obj2: obj2, ....)\n```\n\nEvery mutation execution will return an `output` object.  This object contains information on the\nmutation execution, and errors occurred or any metadata that's needed to be returned from the mutation, \nin the form of a hash.\n\nAny meta data that needs to be returned can be added to the output object using the\nhelper method inside your mutation:\n\n```ruby\nclass RecipeCreatedMutation\n  include Mutant\n...\n    def execute(args)\n      output.add_meta(:test, 'value')\n    end\n\n...\n\noutput = RecipeCreatedMutation.run()\noutput.meta[:test] # \u003e\u003e 'value'\n\n```\n\n```ruby\noutput = RecipeCreatedMutation.run(obj: obj1)\noutput.success? # \u003e\u003e true\noutput.errors # \u003e\u003e  [err1, err1, ...]\noutput.meta # \u003e {:my =\u003e 'value', :other =\u003e 'value1'}\n```\n\n---\n## How to use this library\n\n### Rails:\nWe could define a folder structure such as this, for our rails Recipe web app:\n```\n/lib/use_cases/recipes/recipe_created_mutation.rb\n/lib/use_cases/user/new_user_signed_up_mutation.rb\n```\n\n`recipe_created_mutation.rb`\n```ruby\nrequire 'mutant'\n\nmodule UseCases::Recipes\n  class RecipeCreatedMutation\n    include Mutant\n\n    required_attr :recipe\n\n    def execute(args)\n      if recipe.name.blank?\n        puts 'whoops this recipe is bad'\n      end\n    end\n  end\n\nend\n```\n\nAnd in a controller, we can execute the mutation like so:\n\n```ruby\nclass RecipesController \u003c ApplicationController\n  include UseCases::Recipes\n\n \n    def create\n        @recipe = Recipe.new(recipe_params)\n        \n        output = RecipeCreatedMutation.run(recipe: @recipe)\n    \n        if output.success?\n          puts 'everything went super duper good'\n        end\n      end \n\n...\nend\n```\n\n\n---\n\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/coreyjs/ruby-mutant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\nFor more information on contributing, here:  [How To Contribute](https://github.com/coreyjs/ruby-mutant/blob/master/CONTRIBUTING.md)\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n\n## Special thanks to the inspiration for this library!\n\nhttps://github.com/cypriss/mutations\n\nhttps://github.com/omarish/mutations\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreyjs%2Fruby-mutant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoreyjs%2Fruby-mutant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreyjs%2Fruby-mutant/lists"}