Project

feint

0.0
No commit activity in last 3 years
No release in over 3 years
A Presenter Layer for ORM(s)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
>= 0
>= 0
~> 10.0
~> 3.0
 Project Readme

Feint

Feint is a Presenter Layer for models. When developing APIs, attributes change from version to version. Controllers contain lots of code to transform attribute names of models and parameter content to fit clients. Feint is aiming at wrapping your models for serialization in different situations. Major features of Feint is to

  • Transform attributes of models from one name to another name for serialization.
  • Convert keys of parameters from one name to another name for model initialization.
  • Lock mandatory and optional columns used in a presenter.

Advanced features will include

  • Data Conversion. Pass a callback to convert data to a certain format
  • Type Coercion. Ensure the result is a certain type. Implicit data conversion.
  • Nested Presentation. If some attributes are from associations, delegate them to presenters of associations.
  • GraphQL Support. Being the glue of GraphQL type definitions and legacy models

From perspective of a serializer, a Feint preseter acts as the model it presents except for different attribute names. You can replace a model instance with its Feint presenter and it will work seamelessly.

Installation

Add this line to your application's Gemfile:

gem 'feint'

And then execute:

$ bundle

Or install it yourself as:

$ gem install feint

Usage

Consider we have a model User.

class User < ActiveRecord::Base
  connection.create_table table_name do |f|
    f.string  :name
    f.string  :email
    f.string  :country
    f.integer :age
    f.integer :level
  end
end

This presenter will take its attributes name and age

class UserPresenter < Feint::Presenter
  attributes :name, :age
  attribute :email, type: String
  transform country: :nation, level: :rank
end

This presenter could be passed to the serializer

class UsersController < ApplicationController
  def show
    @user = User.find_by(id: params[:id])
    render json: UserPresenter.new(@user)
  end
end

And render expected result

{
  "name":   "NAME",
  "age":    "AGE",
  "email":  "EMAIL",
  "nation": "COUNTRY",
  "rank":   "LEVEL",
}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/shelling/feint. 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.