MoskitoHero / barley

Barley is a fast and efficient ActiveModel serializer
MIT License
80 stars 3 forks source link

Add context argument to Serializer initializer #6

Closed MoskitoHero closed 4 months ago

MoskitoHero commented 4 months ago

Description

This MR adds the possibility to define a custom context object to pass to the initializer of the Serializer.

This change also allows the context to be available in nested one or many association blocks inside the serializer definition.

So now you can do

class ProductSerializer < Barley::Serializer
  attribute :name do
    object.localized_name(context.locale)
   end

  many :storages do
    many :shelves do
      object.localized_name(context.locale) # context is available here too!
    end
  end
end

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

Benchmarks

Barley is fast, and should be kept as fast as possible. Provide benchmarks related to speed and memory footprint.

Checklist: