Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/coreyjs/ruby-mutant
Encapsulate your business logic into reusable mutations.
https://github.com/coreyjs/ruby-mutant
hacktoberfest mutations ruby ruby-on-rails rubygem
Last synced: 25 days ago
JSON representation
Encapsulate your business logic into reusable mutations.
- Host: GitHub
- URL: https://github.com/coreyjs/ruby-mutant
- Owner: coreyjs
- License: mit
- Created: 2018-10-09T12:56:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-18T00:05:15.000Z (over 1 year ago)
- Last Synced: 2024-09-30T09:41:40.109Z (about 1 month ago)
- Topics: hacktoberfest, mutations, ruby, ruby-on-rails, rubygem
- Language: Ruby
- Homepage:
- Size: 139 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Ruby Mutant
[![Gem Version](https://badge.fury.io/rb/ruby-mutant.svg)](https://badge.fury.io/rb/ruby-mutant)
## Installation
```ruby
gem 'ruby-mutant', '~> 1.0.1'
```---
## UsageRuby 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.
To create a mutation from a ruby object, include the ruby module `require "mutatant"`
```ruby
require "mutant"class RecipeCreatedMutation
include Mutant
required_attr :recipe#Define custom validators for our attributes
def validate_name?
true
end# Required, this will execute our new mutation.
def execute(args)
# here, recipe is passe into out Class.run(recipe=Recipe.new) method
if recipe.difficulty == 'godlike'
recipe_service.send_alert_new_super_recipe_confirmation()
end
end
end
```---
To run a mutation definition:
```ruby
# run() excepts any number of parameters that you want to pass into your mutation
output = RecipeCreatedMutation.run(recipe: Recipe.new, obj2: obj2, ....)
```Every mutation execution will return an `output` object. This object contains information on the
mutation execution, and errors occurred or any metadata that's needed to be returned from the mutation,
in the form of a hash.Any meta data that needs to be returned can be added to the output object using the
helper method inside your mutation:```ruby
class RecipeCreatedMutation
include Mutant
...
def execute(args)
output.add_meta(:test, 'value')
end...
output = RecipeCreatedMutation.run()
output.meta[:test] # >> 'value'```
```ruby
output = RecipeCreatedMutation.run(obj: obj1)
output.success? # >> true
output.errors # >> [err1, err1, ...]
output.meta # > {:my => 'value', :other => 'value1'}
```---
## How to use this library### Rails:
We could define a folder structure such as this, for our rails Recipe web app:
```
/lib/use_cases/recipes/recipe_created_mutation.rb
/lib/use_cases/user/new_user_signed_up_mutation.rb
````recipe_created_mutation.rb`
```ruby
require 'mutant'module UseCases::Recipes
class RecipeCreatedMutation
include Mutantrequired_attr :recipe
def execute(args)
if recipe.name.blank?
puts 'whoops this recipe is bad'
end
end
endend
```And in a controller, we can execute the mutation like so:
```ruby
class RecipesController < ApplicationController
include UseCases::Recipes
def create
@recipe = Recipe.new(recipe_params)
output = RecipeCreatedMutation.run(recipe: @recipe)
if output.success?
puts 'everything went super duper good'
end
end...
end
```---
## Contributing
Bug 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.
For more information on contributing, here: [How To Contribute](https://github.com/coreyjs/ruby-mutant/blob/master/CONTRIBUTING.md)
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Special thanks to the inspiration for this library!
https://github.com/cypriss/mutations
https://github.com/omarish/mutations