Skip to content

Reserved keywords in serializer #23

@floriandejonckheere

Description

@floriandejonckheere

Due to the way the serialization mechanism is written, attributes in a serializer cannot be named either model or options, because these methods are already defined on CacheCrispies::Base. No warning or error is thrown either.

The following example demonstrates this:

#!/usr/bin/env ruby

require "cache_crispies"

class MySerializer < CacheCrispies::Base
  key :my_serializer

  serialize :id

  serialize :options do |model, options|
    # the passed `model` will not be the model, but rather the instance
    # of the serializer. To retrieve the model, we have to call
    # `model.model`.
    model.model.options
  end
end

model = OpenStruct.new(id: "my_id", options: { my_option: true })

puts MySerializer.new(model, {}).as_json

=> {:id=>"my_id", :options=>{}}

The logic that exhibits this can be found on https://github.com/codenoble/cache-crispies/blob/master/lib/cache_crispies/hash_builder.rb#L63.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions