README.md in alba-2.4.3 vs README.md in alba-3.0.0

- old
+ new

@@ -81,10 +81,16 @@ ### Feature rich While Alba's core is simple, it provides additional features when you need them, For example, Alba provides [a way to control circular associations](#circular-associations-control), [root key and association resource name inference](#root-key-and-association-resource-name-inference) and [supports layouts](#layout). +### Other reasons + +- Dependency free, no need to install `oj` or `activesupport` while Alba works well with them +- Well tested, the test coverage is 99% +- Well maintained, gettings frequent update and new releases (see [version history](https://rubygems.org/gems/alba/versions)) + ## Installation Add this line to your application's Gemfile: ```ruby @@ -99,11 +105,11 @@ $ gem install alba ## Supported Ruby versions -Alba supports CRuby 2.7 and higher and latest JRuby and TruffleRuby. +Alba supports CRuby 3.0 and higher and latest JRuby and TruffleRuby. ## Documentation You can find the documentation on [RubyDoc](https://rubydoc.info/github/okuramasafumi/alba). @@ -128,24 +134,30 @@ #### Backend configuration Backend is the actual part serializing an object into JSON. Alba supports these backends. -|name|description|requires_external_gem| -|--|--|--| -|`oj`, `oj_strict`|Using Oj in `strict` mode|Yes(C extension)| -|`oj_rails`|It's `oj` but in `rails` mode|Yes(C extension)| -|`oj_default`|It's `oj` but respects mode set by users|Yes(C extension)| -|`active_support`|For Rails compatibility|Yes| -|`default`, `json`|Using `json` gem|No| +|name|description|requires_external_gem| encoder| +|--|--|--|--| +|`oj`, `oj_strict`|Using Oj in `strict` mode|Yes(C extension)|`Oj.dump(object, mode: :strict)`| +|`oj_rails`|It's `oj` but in `rails` mode|Yes(C extension)|`Oj.dump(object, mode: :rails)`| +|`oj_default`|It's `oj` but respects mode set by users|Yes(C extension)|`Oj.dump(object)`| +|`active_support`|For Rails compatibility|Yes|`ActiveSupport::JSON.encode(object)`| +|`default`, `json`|Using `json` gem|No|`JSON.generate(object)`| You can set a backend like this: ```ruby Alba.backend = :oj ``` +This is equivalent as: + +```ruby +Alba.encoder = ->(object) { Oj.dump(object, mode: :strict) } +``` + #### Encoder configuration You can also set JSON encoder directly with a Proc. ```ruby @@ -282,11 +294,11 @@ @name = name @email = email end def name_with_email - "dummy!" + 'dummy!' end end class UserResource include Alba::Resource @@ -1229,11 +1241,11 @@ You can control circular associations with `within` option. `within` option is a nested Hash such as `{book: {authors: books}}`. In this example, Alba serializes a book's authors' books. This means you can reference `BookResource` from `AuthorResource` and vice versa. This is really powerful when you have a complex data structure and serialize certain parts of it. For more details, please refer to [test code](https://github.com/okuramasafumi/alba/blob/main/test/usecases/circular_association_test.rb) -### Experimental support of types +### Types You can validate and convert input with types. ```ruby class User @@ -1268,12 +1280,31 @@ user = User.new(1, 'Masafumi OKURA', '32', nil) # bio is nil and auto conversion is disabled for bio UserResource.new(user).serialize # => TypeError, 'Attribute bio is expected to be String but actually nil.' ``` -Note that this feature is experimental and interfaces are subject to change. +#### Custom types +You can define custom types to abstract data conversion logic. To define custom types, you can use `Alba.register_type` like below. + +```ruby +# Typically in initializer +Alba.register_type :iso8601, converter: ->(time) { time.iso8601(3) }, auto_convert: true +``` + +Then use it as regular types. + +```rb +class UserResource + include Alba::Resource + + attributes :id, created_at: :iso8601 +end +``` + +You now get `created_at` attribute with `iso8601` format! + ### Collection serialization into Hash Sometimes we want to serialize a collection into a Hash, not an Array. It's possible with Alba. ```ruby @@ -1374,11 +1405,11 @@ ```ruby class ApplicationResource include Alba::Resource def self.with_id - attributes :id + attributes(:id) end end class LibraryResource < ApplicationResource with_id @@ -1399,11 +1430,11 @@ class ApplicationResource include Alba::Resource helper do def with_id - attributes :id + attributes(:id) end end end # Now `LibraryResource` works! ``` @@ -1559,10 +1590,10 @@ FooResource.prepend(Logging) FooResource.new(foo).serialize # => "{:id=>1}" is printed ``` -Here, we override `serialize` method with `prepend`. In overridden method we print the result of `serializable_hash` that gives the basic hash for serialization to `serialize` method. Using `...` allows us to override without knowing method signiture of `serialize`. +Here, we override `serialize` method with `prepend`. In overridden method we print the result of `serializable_hash` that gives the basic hash for serialization to `serialize` method. Using `...` allows us to override without knowing method signature of `serialize`. Don't forget calling `super` in this way. ## Rails