- 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