Skip to content

Commit 562d85c

Browse files
committed
Serializer#attributes => Serializer#serialized
`attributes` sort of implies that it is a collection of things, when in truth serialized should be able to return a String or `true` or `nil` as the primitive type for a serialized entity (even though *most* of the time the return value of Serializer#serialized will be a Hash)
1 parent 47dadee commit 562d85c

File tree

8 files changed

+22
-20
lines changed

8 files changed

+22
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ gets out of your way. You just write plain ruby.
1111

1212
``` ruby
1313
class PersonSerializer < Granola::Serializer
14-
def attributes
14+
def serialized
1515
{
1616
"name" => object.name,
1717
"email" => object.email,
@@ -99,7 +99,7 @@ generating the JSON response altogether. For example, using Cuba:
9999

100100
``` ruby
101101
class UserSerializer < Granola::Serializer
102-
def attributes
102+
def serialized
103103
{ "id" => object.id, "name" => object.name, "email" => object.email }
104104
end
105105

lib/granola.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ def initialize(object)
4747
@object = object
4848
end
4949

50-
# Public: Returns the Hash of attributes that should be serialized into
51-
# JSON.
50+
# Public: Returns a primitive Object that can be serialized into JSON,
51+
# meaning one of `nil`, `true`, `false`, a String, a Numeric, an Array of
52+
# primitive objects, or a Hash with String keys and primitive objects as
53+
# values.
5254
#
5355
# Raises NotImplementedError unless you override in subclasses.
54-
def attributes
56+
def serialized
5557
fail NotImplementedError
5658
end
5759

@@ -61,7 +63,7 @@ def attributes
6163
#
6264
# Returns a String.
6365
def to_json(**options)
64-
Granola.json.(attributes, options)
66+
Granola.json.(serialized, options)
6567
end
6668

6769
# Public: Returns the MIME type generated by this serializer. By default
@@ -102,8 +104,8 @@ def initialize(list, *args, with: serializer)
102104
end
103105

104106
# Public: Returns an Array of Hashes that can be serialized into JSON.
105-
def attributes
106-
@list.map(&:attributes)
107+
def serialized
108+
@list.map(&:serialized)
107109
end
108110
end
109111
end

lib/granola/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def self.serializer_class_for(object)
4141
# Internal: Null serializer that transparently handles rendering `nil` in case
4242
# it's passed.
4343
class NilClassSerializer < Granola::Serializer
44-
def attributes
44+
def serialized
4545
{}
4646
end
4747
end

lib/granola/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def schema
2626
def valid?
2727
validation_errors.clear
2828
validation_errors.concat(
29-
JSON::Validator.fully_validate(self.class.schema, attributes)
29+
JSON::Validator.fully_validate(self.class.schema, serialized)
3030
)
3131
validation_errors.empty?
3232
end
@@ -52,7 +52,7 @@ def validation_errors
5252
# serializer = SchemaSerializer.new(PersonSerializer.schema)
5353
# serializer.to_json
5454
class SchemaSerializer < Serializer
55-
def attributes
55+
def serialized
5656
{
5757
"$schema".freeze => "http://json-schema.org/schema#".freeze,
5858
"type".freeze => "object".freeze

test/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Person = Struct.new(:name, :age, :updated_at)
55

66
class PersonSerializer < Granola::Serializer
7-
def attributes
7+
def serialized
88
{ "name" => object.name, "age" => object.age }
99
end
1010

test/helper_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include Granola::Helper
44

55
class CustomSerializer < Granola::Serializer
6-
def attributes
6+
def serialized
77
{ "name" => object.name }
88
end
99
end

test/schema_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def self.schema
3030

3131
assert_equal \
3232
"http://json-schema.org/schema#",
33-
serializer.attributes["$schema"]
33+
serializer.serialized["$schema"]
3434

35-
assert_equal "object", serializer.attributes["type"]
36-
assert_equal ["name"], serializer.attributes["required"]
35+
assert_equal "object", serializer.serialized["type"]
36+
assert_equal ["name"], serializer.serialized["required"]
3737
assert_equal(
3838
{ "type" => "integer" },
39-
serializer.attributes["properties"]["age"]
39+
serializer.serialized["properties"]["age"]
4040
)
4141
end
4242

test/serialize_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "ostruct"
22

33
class OpenStructSerializer < Granola::Serializer
4-
def attributes
4+
def serialized
55
object.send(:table).each.with_object({}) do |(key, val), hash|
66
hash[key.to_s] = val
77
end
@@ -21,7 +21,7 @@ def mime_type
2121

2222
test "serializes the properties" do |object|
2323
serializer = OpenStructSerializer.new(object)
24-
assert_equal({ "a" => 1, "b" => 2, "c" => 3 }, serializer.attributes)
24+
assert_equal({ "a" => 1, "b" => 2, "c" => 3 }, serializer.serialized)
2525
end
2626

2727
test "converts to json" do |object|
@@ -71,7 +71,7 @@ def initialize(object, another)
7171
@another = another
7272
end
7373

74-
def attributes
74+
def serialized
7575
{ "foo" => object.foo, "bar" => another.bar }
7676
end
7777
end

0 commit comments

Comments
 (0)