README.md in alba-0.13.0 vs README.md in alba-0.13.1

- old
+ new

@@ -17,15 +17,15 @@ ### Easy to understand DSL is great. It makes the coding experience natural and intuitive. However, remembering lots of DSL requires us a lot of effort. Unfortunately, most of the existing libraries have implemented their features via DSL and it's not easy to understand how they behave entirely. Alba's core DSL are only four (`attributes`, `attribute`, `one` and `many`) so it's easy to understand how to use. -Alba is also understandable internally. The codebase is much smaller than the alternatives. In fact, it's less than 300 lines of code. Look at the code on [GitHub](https://github.com/okuramasafumi/alba/tree/master/lib) and you'll be surprised how simple it is! +Alba is also understandable internally. The codebase is much smaller than the alternatives. In fact, it's about 330 lines of code. Look at the code on [GitHub](https://github.com/okuramasafumi/alba/tree/master/lib) and you'll be surprised how simple it is! ### Performance -Alba is faster than most of the alternatives. We have a [benchmark](https://gist.github.com/okuramasafumi/4e375525bd3a28e4ca812d2a3b3e5829). +Alba is faster than most of the alternatives. We have a [benchmark](https://github.com/okuramasafumi/alba/tree/master/benchmark). ## Installation Add this line to your application's Gemfile: @@ -45,11 +45,11 @@ Alba supports CRuby 2.5.7 and higher and latest TruffleRuby. ## Documentation -You can find the documentation on [RubyDoc](https://rubydoc.info/gems/alba). +You can find the documentation on [RubyDoc](https://rubydoc.info/github/okuramasafumi/alba). ## Features * Resource-based serialization * Arbitrary attribute definition @@ -57,10 +57,11 @@ * Adding condition and filter to association * Parameters can be injected and used in attributes and associations * Setting root key separately in Serializer * Adding metadata * Selectable backend +* Key transformation * No runtime dependencies ## Anti features * Sorting keys @@ -250,9 +251,47 @@ UserResourceCamel.new(user).serialize # => '{"id":1,"firstName":"Masafumi","lastName":"Okura"}' ``` Supported transformation types are :camel, :lower_camel and :dash. + +### Filtering attributes + +You can filter attributes by overriding `Alba::Resource#converter` method, but it's a bit tricky. + +```ruby +class User + attr_accessor :id, :name, :email, :created_at, :updated_at + + def initialize(id, name, email) + @id = id + @name = name + @email = email + end +end + +class UserResource + include Alba::Resource + + attributes :id, :name, :email + + private + + # Here using `Proc#>>` method to compose a proc from `super` + def converter + super >> proc { |hash| hash.compact } + end +end + +user = User.new(1, nil, nil) +UserResource.new(user).serialize # => '{"id":1}' + + +``` + +The key part is the use of `Proc#>>` since `Alba::Resource#converter` returns a `Proc` which contains the basic logic and it's impossible to change its behavior by just overriding the method. + +It's not recommended to swap the whole conversion logic. It's recommended to always call `super` when you override `converter`. ## Comparison Alba is faster than alternatives. For a performance benchmark, see https://gist.github.com/okuramasafumi/4e375525bd3a28e4ca812d2a3b3e5829.