README.md in bright_serializer-0.2.5 vs README.md in bright_serializer-0.3.0

- old
+ new

@@ -1,7 +1,8 @@ [![Actions Status](https://github.com/petalmd/bright_serializer/workflows/Build/badge.svg)](https://github.com/petalmd/bright_serializer/actions?query=workflow%3ABuild) [![Gem Version](https://badge.fury.io/rb/bright_serializer.svg)](https://badge.fury.io/rb/bright_serializer) +[![Coverage Status](https://coveralls.io/repos/github/petalmd/bright_serializer/badge.svg?branch=master)](https://coveralls.io/github/petalmd/bright_serializer?branch=master) # BrightSerializer This is a very light and fast gem to serialize object in a Ruby project. @@ -29,16 +30,16 @@ ```ruby class AccountSerializer include BrightSerializer::Serializer attributes :id, :first_name, :last_name - + # With a block attribute :name do |object| "#{object.first_name} #{object.last_name}" end - + # With a block shorter attribute :created_at, &:to_s end AccountSerializer.new(Account.first).to_h @@ -51,11 +52,11 @@ ```ruby class AccountSerializer include BrightSerializer::Serializer attributes :id, :first_name, :last_name - + attribute :friend do |object, params| object.is_friend_with? params[:current_user] end end @@ -63,18 +64,18 @@ AccountSerializer.new(Account.first, params: { current_user: current_user }).to_json ``` ### Conditional Attributes -Attribute can be remove from serialization by passing a `proc` to the option `if`. If the proc return `true` the attibute - will be serialize. `object` and `params` or accessible. +Attribute can be remove from serialization by passing a `proc` to the option `if`. If the proc return `true` the attibute + will be serialize. `object` and `params` or accessible. ```ruby class AccountSerializer include BrightSerializer::Serializer attributes :id, :first_name, :last_name - + attribute :email, if: -> { |object, params| params[:current_user].is_admin? } end ``` ### Transform keys @@ -116,33 +117,58 @@ end class AccountSerializer include BrightSerializer::Serializer attributes :id, :first_name, :last_name - + attribute :friends do |object| FriendSerializer.new(object.friends) end end ``` ### Entity You can define the entity of your serializer to generate documentation with the option `entity`. -The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger). +The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger). For more information about defining a model entity see the [Swagger documentation](https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object). ```ruby class AccountSerializer include BrightSerializer::Serializer attribute :id, entity: { type: :string, description: 'The id of the account' } attribute :name attribute :friends, - entity: { + entity: { type: :array, items: { ref: 'FriendSerializer' }, description: 'The list the account friends.' } do |object| FriendSerializer.new(object.friends) + end +end + +``` +Callable values are supported. + +```ruby +{ entity: { type: :string, enum: -> { SomeModel::ENUMVALUES } } } +``` + +### Instance + +If you have defined instance methods inside your serializer you can access them inside block attribute. + +```ruby +class AccountSerializer + include BrightSerializer::Serializer + attributes :id, :name + + attribute :print do |object| + print_account(object) + end + + def print_account(object) + "Account: #{object.name}" end end ``` ## Development