Giter Site home page Giter Site logo

decoradar's Introduction

Decoradar

Travis RubyGems

As the name might have implied, Decoradar is a simple JSON serializer + decorator in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'decoradar', '~> 0.1.1'

And then execute:

$ bundle

Why Decoradar?

Decoradar is really simple and magic-free (zero monkey patch, zero auto coercion) and aims to bring ActiveModel::Serializer to Plain Ruby Object.

What will Decoradar brings you?

  • ActiveModel::Serializer DSL style.
  • Magic free and explicit: there is no monkey patching or auto coercion.
  • Isolated testing: it's just Ruby objects without Rails dependence so feel free to unit test it. (when I said unit-test I meant it)

Usage

class UserSerializer
  include Decoradar

  attributes :id, :username
  attribute :full_name, as: :name

  collection :posts, serializer: PostSerializer
end

UserSerializer.new(@user).as_json
# => { id: 1, username: "huynhquancam", name: "Cam Huynh", posts: [{...}] }

Let's say you want to tweak full_name a bit.

class UserSerializer
  include Decoradar

  attributes :id, :name
  attribute :full_name, as: :name

  def full_name
    model.full_name.capitalize
  end
end

UserSerializer.new(@user).as_json
# => { id: 1, username: "huynhquancam", name: "CAM HUYNH" }

If we want the serialized hash to include an attribute as long as it exists, we can use include_if option.

class UserSerializer
  include Decoradar

  attribute :id
  attribute :username
  attribute :is_admin, include_if: ->(model) { model.admin? }
end

UserSerializer.new(@mark).as_json
# => { id: 1, username: "zuck", is_admin: true }

UserSerializer.new(@trump).as_json
# => { id: 1, username: "realDonaldTrump" }

If you want to serialize a collection, there's NO AUTO-MAGIC COERCION, just uses .decorate_collection.

UserSerializer.decorate_collection(@users).map(&:as_json)

Development

  1. Fork this repo.
  2. Make your change and submit pull request (with specs please ๐Ÿ˜„)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/qcam/decoradar. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

decoradar's People

Contributors

qcam avatar

Watchers

Huy avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.