{"id":13878382,"url":"https://github.com/Code-Vedas/mutils","last_synced_at":"2025-07-16T14:32:04.872Z","repository":{"id":37590484,"uuid":"179230208","full_name":"Code-Vedas/mutils","owner":"Code-Vedas","description":"Mutils is a collection of utilities microservices","archived":false,"fork":false,"pushed_at":"2024-10-28T02:51:33.000Z","size":570,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T14:18:26.801Z","etag":null,"topics":["json-serialization","json-serialization-library","json-serializer","mutils","rails-generator","root-serializer","ruby-gem","ruby-library","ruby-on-rails","serializer","transformations","xml-serialization"],"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/Code-Vedas.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-03T07:02:39.000Z","updated_at":"2024-10-18T03:05:47.000Z","dependencies_parsed_at":"2023-02-14T13:15:19.200Z","dependency_job_id":"9ef7a83a-3ea8-4fed-b754-84dc3081d317","html_url":"https://github.com/Code-Vedas/mutils","commit_stats":{"total_commits":361,"total_committers":4,"mean_commits":90.25,"dds":"0.44321329639889195","last_synced_commit":"4f0f8defaf327d8018dfcbba21bf0eda9bb0ff8b"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Vedas%2Fmutils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Vedas%2Fmutils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Vedas%2Fmutils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Vedas%2Fmutils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Code-Vedas","download_url":"https://codeload.github.com/Code-Vedas/mutils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"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":["json-serialization","json-serialization-library","json-serializer","mutils","rails-generator","root-serializer","ruby-gem","ruby-library","ruby-on-rails","serializer","transformations","xml-serialization"],"created_at":"2024-08-06T08:01:47.941Z","updated_at":"2024-11-24T07:30:59.367Z","avatar_url":"https://github.com/Code-Vedas.png","language":"Ruby","readme":"[![Maintainability](https://api.codeclimate.com/v1/badges/42e8a34f839ca0c5ec45/maintainability)](https://codeclimate.com/github/code-vedas/mutils/maintainability)\n![](https://ruby-gem-downloads-badge.herokuapp.com/mutils?type=total\u0026color=brightgreen)\n[![Gem Version](https://badge.fury.io/rb/mutils.svg)](https://badge.fury.io/rb/mutils)\n[![Coverage Status](https://badge.coveralls.io/repos/github/Code-Vedas/mutils/badge.svg?branch=master)](https://badge.coveralls.io/github/Code-Vedas/mutils?branch=master)\n\n# Mutils\n## Introduction\n`mutils` is collection of useful modules for `ruby on rails` which is tested and benchmarked against high load.\n\nThese collection of modules are built by developer for developers :-)\n# Table of Contents\n\n* [Features](#features)\n* [Installation](#installation)\n* [Usage](#usage)\n  * [Rails Generator](#rails-generator)\n  * [Attributes](#attributes)\n  * [Relations](#relations)\n  * [Conditional Attributes](#conditional-attributes)\n  * [Conditional Relations](#conditional-relations)\n  * [Attributes Block](#attributes-blocks)\n  * [Attributes Block with Params](#attributes-blocks-with-params)\n  * [Custom Methods](#custom-methods)\n  * [Name Tag](#name-tag)\n  * [Sample Usage](#sample-usage)\n\n## Features\n* Simple declaration syntax similar to Active Model Serializer\n* Realtionships support `belongs_to`, `has_many`, `has_one`\n* Block style attributes with params\n\n## Installation\n\nAdd this line to your application's Gemfile:\n```ruby\ngem 'mutils'\n```\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install mutils\n\n## Usage \n\n### Rails Generator\n```shell script\nrails g mutils:serializer User id first_name last_name email\n\nOUTPUT\nRunning via Spring preloader in process xxxxx\n      create  app/serializers/user_serializer.rb\n```\nYou will get serializer in app/serializers/user_serializer.rb\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\nend\n```\n\n### Attributes\nAttributes are fields in the model itself. You can reference them by below example\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\n  ## OR\n  attribute :email, {always_include: true} ## this will allow to selectively include email\n  ## OR\n  attribute :email, \u0026:email ## this will call email attribute from User\nend\n```\n### Relations\nRelations such as `has_many`, `belongs_to`, `has_one` can be used as follows\n1. Every relation must be provided with their own serializer\n2. `always_include` option can be used to instruct `Serializer` to always include this relation\n3. `always_include` by default is disabled, relations which are not `always_include` can be included while using the serializer. Refer to next section for this usage\n4. `label` option can be used to override model class name while serializing\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\n  \n  belongs_to :company, serializer: CompanySerializer, always_include: true\n  ## OR\n  belongs_to :company, serializer: CompanySerializer, always_include: true, label: 'organization'   ##\u003c== important to give singular name\n  \n  has_many :comments, serializer: CommentSerializer\n  has_one :account, serializer: AccountSerializer\n  \n  def full_name\n    \"#{scope.first_name} #{scope.last_name}\"\n  end\nend\n```\n### Conditional Attributes\nSerializer can have conditional attributes with `if: Proc` \n`if: Proc` block can receive `scope` and `params` as arguments\n\n- __in proc {|scope|}__, scope is object which is being serialized\n- __in proc {|scope,params|}__, scope is object which is being serialized and params is hash given to Serializer as second arguments in {params:anything}\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name\n  attribute :email, if: proc { |scope| scope.name == 'mutils' } ## Email will only serialize if user's name is 'mutils'\n  # OR with Params\n  attribute :email, if: proc { |scope,params| params \u0026\u0026 params[:show_email] == true } ## Email will only serialize if params[:show_email] is true\nend\n\nUserSerializer.new(user) # Without params\nUserSerializer.new(user,{params:{show_email:true}}) # With params\n```\n\n### Conditional Relations\nSerializer can have conditional relations with `if: Proc` \n`if: Proc` block can receive `scope` and `params` as arguments\n\n- __in proc {|scope|}__, scope is object which is being serialized\n- __in proc {|scope,params|}__, scope is object which is being serialized and params is hash given to Serializer as second arguments in {params:anything}\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name\n  has_many :comments, serializer: CommentSerializer, if: proc { |scope| scope.name == 'mutils' } ## comments will only serialize if user's name is 'mutils'\n  belongs_to :account, serializer: AccountSerializer, if: proc { |scope| scope.name != 'mutils' } ## account will only serialize if user's name is not 'mutils'\n  # OR with Params\n  belongs_to :account, serializer: AccountSerializer, if: proc { |scope,params| params \u0026\u0026 params[:show_account] == true } ## account will only serialize if params[:show_account] is true\nend\n\nUserSerializer.new(user) # Without params\nUserSerializer.new(user,{params:{show_account:true}}) # With params\n\n```\n    \n### Attributes Blocks\nWhile writting attribute a block can be provided for useful transformations like `full_name` as shown below\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\n  attribute :full_name do |object|\n    \"#{object.first_name} #{object.last_name}\"\n  end\nend\n```\n### Attributes Blocks with Params\nWhile writting attribute a block can be provided for useful transformations like `full_name` as shown below\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\n  attribute :is_owner do |object,params|\n    params[:owner].id == object.id ? true:false\n  end\nend\n```\n```ruby\n# in controller\n\nuser = current_user\nowner = owner_user\nrender json: UserSerializer.new(user,{params:{owner:owner}})\n```\n### Custom Methods\nCustom methods used in Serializer can be useful for cases as below.\n`scope` will be available to reference object in Serializer in below case its `user`\n\n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  attributes :id, :first_name, :last_name, :email\n  ###\n  custom_methods :full_name\n  ## OR\n  custom_method :full_name, {always_include: true}   ## this will allow to selectively include full_name\n  ### \n  \n  def full_name\n    \"#{scope.first_name} #{scope.last_name}\"\n  end\nend\n```\n### Name Tag\nname_tag is used to provide custom name to serializer output keys for json\n\n**Options**\n  - ``name_tag 'Person', true`` # Include Person or People in JSON serialization as root, true|false this only implies to root serializer\n  - ``name_tag 'Person', false`` # not Include Person or People in JSON serialization as root, true|false this only implies to root serializer\n  - ``name_tag 'Person'`` # same as ``name_tag 'Person', false``\n  - without name_tag, actual class name of scope object inside serializer will be used \n```ruby\n# frozen_string_literal: true\n\n# User Serializer\nclass UserSerializer \u003c Mutils::Serialization::BaseSerializer\n  name_tag 'Person', true\n  attributes :id, :first_name, :last_name, :email\n  custom_methods :full_name\n  \n  def full_name\n    \"#{scope.first_name} #{scope.last_name}\"\n  end\nend\n```\n\n### Sample Usage\n\n```ruby\nuser = User.first\noptions = {includes: [:comments,:account]}\nUserSerializer.new(user,options).to_h\n```\n### or\n```ruby\nusers = User.all\noptions = {includes: [:account]}\nUserSerializer.new(users,options).to_json\n```\n### or in controllers\n```ruby\nusers = User.all\noptions = {includes: [:account]}\nusers_serializer =UserSerializer.new(users,options)\nrender json: users_serializer\n```\n\n## Contributing\n\nBug Reports and PR's are welcomed in this repository kindly follow guidelines from `.github` directory.\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## Code of Conduct\n\nEveryone interacting in the Mutils project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/code-vedas/mutils/blob/master/CODE_OF_CONDUCT.md).\n\n## Security\n\nFor security refer to [security](https://github.com/Code-Vedas/mutils/blob/master/SECURITY.md) document.\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Vedas%2Fmutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCode-Vedas%2Fmutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Vedas%2Fmutils/lists"}