varvet / serial

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

Merge serializer into other serializer #6

Closed ramhoj closed 9 years ago

ramhoj commented 9 years ago

I would like to do something like this:

ReportSerializer = Serial::Serializer.new do |h, report|
  h.attribute(:foo, "bar")
end

FancyReportSerializer = Serial::Serializer.new do |h, report|
  h.merge(report, &ReportSerializer)
  h.attribute(:quox, "monkey")
end

The result of FancyReportSerializer would then look like this:

{ "foo": "bar", "quox": "monkey" }

That is, it does not introduce a new level of nesting. There should probably be an equivalent merge! method if #3 is merged.

Also, it no block is passed to merge then the first parameter is assumed to be a hash (or we call #to_h on it), so it is equivalent to Ruby's Hash#merge.

Burgestrand commented 9 years ago

:+1: