Skip to content

Commit

Permalink
remove the symbolizeri by default for a big perf win
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinJoyce committed May 9, 2016
1 parent 5a0c7ee commit 6d73d7a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/restpack_serializer/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def as_json(model, context = {})
add_custom_attributes(data)
add_links(model, data) unless self.class.associations.empty?

data
end

def as_json_symbolized(model, context = {})
data = as_json(model, context)
Symbolizer.recursive_symbolize(data)
end

Expand Down Expand Up @@ -99,6 +104,10 @@ def as_json(model, context = {})
new.as_json(model, context)
end

def as_json_symbolized(model, context = {})
new.as_json_symbolized(model, context)
end

def serialize(models, context = {})
models = [models] unless models.kind_of?(Array)

Expand Down
38 changes: 34 additions & 4 deletions spec/serializable/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class EmptySerializer

class PersonSerializer
include RestPack::Serializer
attributes :id, :name, :description, :href, :admin_info
attributes :id, :name, :description, :href, :admin_info, :string_keys

def description
"This is person ##{id}"
end

def admin_info
{
"key" => "super_secret_sauce",
"array" => [
{ "name" => "Alex" }
key: "super_secret_sauce",
array: [
{ name: "Alex" }
]
}
end
Expand All @@ -52,6 +52,20 @@ def include_admin_info?
@context[:is_admin?]
end

def string_keys
{
"kid_b" => "Ben",
"likes" => {
"foods" => ["crackers", "stawberries"],
"books" => ["Dumpy", "That's Not My Tiger"]
}
}
end

def include_string_keys?
@context[:is_ben?]
end

def custom_attributes
{
:custom_key => "custom value for model id #{@model.id}"
Expand Down Expand Up @@ -187,6 +201,22 @@ def custom_attributes
end
end
end

describe ".as_json_symbolized" do
it "symbolizes keys recursively" do
serializer.as_json_symbolized(person, { is_ben?: true }).should == {
id: '123', name: 'Gavin', description: 'This is person #123',
href: '/people/123', custom_key: 'custom value for model id 123',
string_keys: {
kid_b: "Ben",
likes: {
foods: ["crackers", "stawberries"],
books: ["Dumpy", "That's Not My Tiger"]
}
}
}
end
end
end

describe "#model_class" do
Expand Down

0 comments on commit 6d73d7a

Please sign in to comment.