varvet / serial

Plain old Ruby for generating primitive data structures from object graphs.
108 stars 2 forks source link

Protection against overriding attributes #3

Closed Burgestrand closed 9 years ago

Burgestrand commented 9 years ago
ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:id, project.id)
  h.attribute(:id, project.owner.id) # => boom?
end

We could have one method for disallowing it, and one method for allowing it.

Burgestrand commented 9 years ago

4b46db5 adds bang-alternatives to attribute, collection and map. @jnicklas what do you think?

Burgestrand commented 9 years ago

This pull request should have an accompanying update to the README.

jnicklas commented 9 years ago

I think this makes sense. As we discussed, there are cases where it does make sense to override the same key, but I think it's very rare.

If we compare this to Ruby, the precedent is to print a warning on key collision:

[1] pry(main)> { foo: 123, foo: 321 }
(pry):1: warning: duplicated key at line 1 ignored: :foo
=> {:foo=>321}

But I don't really think that makes sense in this case.