- Added the
only
andexcept
options to theattributes
method. This allows you to specify which attributes to include or exclude from the serialization. Will also work with nested associations.class UserSerializer < Barley::Serializer attributes :id, :name, :email end Serializer.new(User.last, only: [:id, :name]).serializable_hash # => { id: 1, name: "John Doe" } User.last.as_json(only: [:id, :name]) # => { id: 1, name: "John Doe" }
See the README for more details.class UserSerializer < Barley::Serializer attributes :id, :name, :email end Serializer.new(User.last, except: [:email]).serializable_hash # => { id: 1, name: "John Doe" }
- Added the possibility to pass the context to a
scope
proc / lambda in amany
association.See the README for more details.class UserSerializer < Barley::Serializer attributes :id, :name many :posts, scope: ->(context) { where("language > ?", context[:language]) } do attributes :id, :title end end Serializer.new(User.last, context: {language: "en_US"}).serializable_hash # => { id: 1, name: "John Doe", posts: [{ id: 1, title: "My first post" }] }
- Improved error handling of type inconsistencies: now the serializer will raise a
Barley::InvalidAttributeError
if the object's attribute is not of the specified type. This error will include the attribute name, the expected type, and the actual type of the attribute.class UserSerializer < Barley::Serializer attributes name: Types::Strict::Integer end Serializer.new(User.last).serializable_hash # => Barley::InvalidAttributeError: Invalid value type found for attribute name::Integer: Bob::String"
v0.6.1
is a patch release that fixes a bug in the scope
argument of the many
method.
- Added the
context
argument to the serializer'sinitialize
method. This allows you to pass a context hash to the serializer, which can be used to pass arguments to the serializer, that can be used with acontext
object within the serializer definition.As a side effect, the context is now available in nested associations as well.class UserSerializer < Barley::Serializer attributes :id, :name def name if context[:upcase] object.name.upcase else object.name end end end Serializer.new(User.last, context: {upcase: true}).serializable_hash # => { id: 1, name: "JOHN DOE" }
- Added the
scope
argument to themany
method. This scope can either be a keyword referring to a named scope available on the object, or a lambda that will be called with the object as an argument. This allows you to filter the associated objects.See the README for more details.class UserSerializer < Barley::Serializer attributes :id, :name many :posts, scope: :published do attributes :id, :title end many :posts, key: :popular, scope: -> { where("views > 10_000").order(views: :desc).limit(5) } do attributes :id, :title end end
- Updated the README to include the new
context
andscope
arguments.
- Added tests for the
context
andscope
arguments
- Updated github actions image to v4
- Added the
with_context
method to the serializer. This method allows you to pass a context hash to the serializer, which can be used to pass arguments to the serializer, that can be used with acontext
object within the serializer definition.See the README for more details.class UserSerializer < Barley::Serializer attributes :id, :name def name if context[:upcase] object.name.upcase else object.name end end end Serializer.new(User.last).with_context(upcase: true).serializable_hash # => { id: 1, name: "JOHN DOE" }
- Updated the README to include the new
with_context
method.
- Added tests for the
with_context
method - Added benchmark tests
- Fixed the
as_json
method to comply with rails standards. It now accepts anoption
hash as an argument, which allows usage on anArray
- and therefore onActiveRecord::Relation
- as well as on a single object. Updated the documentation to reflect this change. This does not break compatibility with previous versions.
- Added type-checking to the
attributes
andattribute
methods. Now you can do:and the serializer will raise an error if the object's attribute is not of the specified type. See the README for more details.attributes id: Types::Strict::Integer, name: Types::Strict::String attribute :created_at, type: Types::Strict::Time
- Updated the README to include the new type-checking feature.
- Added YARD documentation. API documentation is available here
- Expanded Barley::Serializer and Barley::Serializable to handle optional root serialization.
User.last.as_json(root: true)
# => { user: { id: 1, name: "John Doe", email: "john.doe@example" } }
- Fix the cache key when no
updated_at
column is defined
- Also updated the relevant test cases to verify the new functionality.
- The 'as_json' method was replaced with 'serializable_hash' in the Serializer.
-
Added the ability to define associations with blocks. They can be nested all the way, so you can do:
one :group do attributes :id, :name end
many :posts do attributes :id, :title, :body one :author do attributes :name, :email one :profile, key_name: :author_profile do attributes :id, :bio end end end
-
Added the ability to define a key name on
attribute
,one
andmany
associations.attribute :created_at, key_name: :post_created_at one :author, key_name: :post_author many :comments, key_name: :post_comments do attributes :id, :body end
- Changed the cache key to use the object class name.
- Updated README and CHANGELOG
- Added tests on serializer.rb
- Reorganized the code in serializer.rb
- Updated RBS method signatures
- Initial release