-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from okuramasafumi/prefer-resource-method
Prefer resource method
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,6 +269,72 @@ class UserResource | |
end | ||
``` | ||
|
||
#### Prefer methods on resource | ||
|
||
By default, Alba prefers methods on the object to methods on the resource. This means if you have a following situation: | ||
|
||
```ruby | ||
class User | ||
attr_accessor :id, :name, :email | ||
|
||
def initialize(id, name, email) | ||
@id = id | ||
@name = name | ||
@email = email | ||
end | ||
|
||
def name_with_email | ||
"dummy!" | ||
end | ||
end | ||
|
||
class UserResource | ||
include Alba::Resource | ||
|
||
root_key :user, :users # Later is for plural | ||
|
||
attributes :id, :name, :name_with_email | ||
|
||
# Same method exists in `User` class! | ||
# This is not called | ||
def name_with_email(user) | ||
"#{user.name}: #{user.email}" | ||
end | ||
end | ||
|
||
user = User.new(1, 'Masafumi OKURA', '[email protected]') | ||
UserResource.new(user).serialize | ||
# => '{"user":{"id":1,"name":"Masafumi OKURA","name_with_email":"dummy!"}}' | ||
``` | ||
|
||
You can see that `name_with_email` is now `dummy!` from `User#name_with_email`. You cna change this behavior by using `prefer_resource_method!` DSL in a resource class: | ||
|
||
```ruby | ||
# With the same `User` class | ||
|
||
class UserResource | ||
include Alba::Resource | ||
|
||
prefer_resource_method! # This line is important | ||
|
||
root_key :user, :users # Later is for plural | ||
|
||
attributes :id, :name, :name_with_email | ||
|
||
# Same method exists in `User` class! | ||
# But now this is called! | ||
def name_with_email(user) | ||
"#{user.name}: #{user.email}" | ||
end | ||
end | ||
|
||
user = User.new(1, 'Masafumi OKURA', '[email protected]') | ||
UserResource.new(user).serialize | ||
# => '{"user":{"id":1,"name":"Masafumi OKURA","name_with_email":"Masafumi OKURA: [email protected]"}}' | ||
``` | ||
|
||
The next major version of Alba will change this default behavior to prefer resource methods. In case you want to preserve current behavior, there's `prefer_object_method!` DSL, which does that. | ||
|
||
#### Params | ||
|
||
You can pass a Hash to the resource for internal use. It can be used as "flags" to control attribute content. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require_relative '../test_helper' | ||
|
||
class ObjectMethodAndResourceMethodTest < Minitest::Test | ||
class Foo | ||
attr_reader :id | ||
def initialize(id) | ||
@id = id | ||
end | ||
end | ||
|
||
def setup | ||
@foo = Foo.new(1) | ||
end | ||
|
||
class FooResource | ||
include Alba::Resource | ||
|
||
prefer_resource_method! | ||
|
||
attributes :id | ||
|
||
def id(_) | ||
42 | ||
end | ||
end | ||
|
||
def test_prefer_resource_method | ||
assert_equal '{"id":42}', FooResource.new(@foo).serialize | ||
end | ||
|
||
class FooResource2 | ||
include Alba::Resource | ||
|
||
prefer_object_method! | ||
|
||
attributes :id | ||
|
||
def id(_) | ||
42 | ||
end | ||
end | ||
|
||
def test_prefer_object_method | ||
assert_equal '{"id":1}', FooResource2.new(@foo).serialize | ||
end | ||
|
||
class FooResource3 | ||
include Alba::Resource | ||
|
||
attributes :id | ||
|
||
def id(_) | ||
42 | ||
end | ||
end | ||
|
||
# TODO: perfer resource method by default from version 3 | ||
def test_default_behavior | ||
assert_equal '{"id":1}', FooResource3.new(@foo).serialize | ||
end | ||
|
||
class FooResource4 | ||
include Alba::Resource | ||
|
||
prefer_resource_method! | ||
|
||
attributes :id | ||
end | ||
|
||
def test_prefer_resource_method_but_it_is_not_there | ||
assert_equal '{"id":1}', FooResource4.new(@foo).serialize | ||
end | ||
end |