Skip to content

Commit

Permalink
Serializer#attributes => Serializer#serialized
Browse files Browse the repository at this point in the history
`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)
  • Loading branch information
foca committed Mar 14, 2015
1 parent 47dadee commit 562d85c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gets out of your way. You just write plain ruby.

``` ruby
class PersonSerializer < Granola::Serializer
def attributes
def serialized
{
"name" => object.name,
"email" => object.email,
Expand Down Expand Up @@ -99,7 +99,7 @@ generating the JSON response altogether. For example, using Cuba:

``` ruby
class UserSerializer < Granola::Serializer
def attributes
def serialized
{ "id" => object.id, "name" => object.name, "email" => object.email }
end

Expand Down
14 changes: 8 additions & 6 deletions lib/granola.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def initialize(object)
@object = object
end

# Public: Returns the Hash of attributes that should be serialized into
# JSON.
# Public: Returns a primitive Object that can be serialized into JSON,
# meaning one of `nil`, `true`, `false`, a String, a Numeric, an Array of
# primitive objects, or a Hash with String keys and primitive objects as
# values.
#
# Raises NotImplementedError unless you override in subclasses.
def attributes
def serialized
fail NotImplementedError
end

Expand All @@ -61,7 +63,7 @@ def attributes
#
# Returns a String.
def to_json(**options)
Granola.json.(attributes, options)
Granola.json.(serialized, options)
end

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

# Public: Returns an Array of Hashes that can be serialized into JSON.
def attributes
@list.map(&:attributes)
def serialized
@list.map(&:serialized)
end
end
end
2 changes: 1 addition & 1 deletion lib/granola/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.serializer_class_for(object)
# Internal: Null serializer that transparently handles rendering `nil` in case
# it's passed.
class NilClassSerializer < Granola::Serializer
def attributes
def serialized
{}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/granola/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def schema
def valid?
validation_errors.clear
validation_errors.concat(
JSON::Validator.fully_validate(self.class.schema, attributes)
JSON::Validator.fully_validate(self.class.schema, serialized)
)
validation_errors.empty?
end
Expand All @@ -52,7 +52,7 @@ def validation_errors
# serializer = SchemaSerializer.new(PersonSerializer.schema)
# serializer.to_json
class SchemaSerializer < Serializer
def attributes
def serialized
{
"$schema".freeze => "http://json-schema.org/schema#".freeze,
"type".freeze => "object".freeze
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Person = Struct.new(:name, :age, :updated_at)

class PersonSerializer < Granola::Serializer
def attributes
def serialized
{ "name" => object.name, "age" => object.age }
end

Expand Down
2 changes: 1 addition & 1 deletion test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include Granola::Helper

class CustomSerializer < Granola::Serializer
def attributes
def serialized
{ "name" => object.name }
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def self.schema

assert_equal \
"http://json-schema.org/schema#",
serializer.attributes["$schema"]
serializer.serialized["$schema"]

assert_equal "object", serializer.attributes["type"]
assert_equal ["name"], serializer.attributes["required"]
assert_equal "object", serializer.serialized["type"]
assert_equal ["name"], serializer.serialized["required"]
assert_equal(
{ "type" => "integer" },
serializer.attributes["properties"]["age"]
serializer.serialized["properties"]["age"]
)
end

Expand Down
6 changes: 3 additions & 3 deletions test/serialize_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "ostruct"

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

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

test "converts to json" do |object|
Expand Down Expand Up @@ -71,7 +71,7 @@ def initialize(object, another)
@another = another
end

def attributes
def serialized
{ "foo" => object.foo, "bar" => another.bar }
end
end
Expand Down

0 comments on commit 562d85c

Please sign in to comment.