Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for overwriting
serializable_hash
This technique is useful when a user wants to generate super complicated JSON.
- Loading branch information
1 parent
02ba38e
commit 5e3430e
Showing
1 changed file
with
32 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require_relative '../test_helper' | ||
|
||
class UserDefinedSerializableHashTest < MiniTest::Test | ||
class Foo | ||
attr_accessor :id, :name | ||
|
||
def initialize(id, name) | ||
@id = id | ||
@name = name | ||
end | ||
end | ||
|
||
class FooResource | ||
include Alba::Resource | ||
|
||
def serializable_hash | ||
grouped = @_object.group_by { |foo| foo.id.even? ? 'even' : 'odd' } | ||
grouped.transform_values { |foos| foos.map { |foo| {name: foo.name} } } | ||
end | ||
end | ||
|
||
def test_resource_class_with_overwriting_works | ||
foo1 = Foo.new(1, 'name1') | ||
foo2 = Foo.new(2, 'name2') | ||
foo3 = Foo.new(3, 'name3') | ||
foo4 = Foo.new(4, 'name4') | ||
assert_equal( | ||
'{"foos":{"odd":[{"name":"name1"},{"name":"name3"}],"even":[{"name":"name2"},{"name":"name4"}]}}', | ||
FooResource.new([foo1, foo2, foo3, foo4]).serialize(with: proc { set key: :foos }) | ||
) | ||
end | ||
end |