wmakley / tiny_serializer

Simple Ruby JSON Serialization DSL. Replaces active_model_serializers.
MIT License
53 stars 2 forks source link

Options API #5

Closed trevorrjohn closed 6 years ago

trevorrjohn commented 6 years ago

I started to try to transition from AMS 0.8, however I am running into issues where I want to pass in options to the serializer. If you are not familiar, options is just metadata that you can use when you are serializing the object.

I was thinking something like:

class ItemSerializer < SimpleSerializer
  attribute :foo do |object, options|
    object.foo if options[:include_foo]
  end
end

> item = Struct.new(:foo).new('foo')
> ItemSerializer.new(item, options: { include_foo: true }).serialize
{ foo: 'foo' }
> ItemSerializer.new(item, options: { include_foo: false }).serialize
{ foo: nil }

Thoughts? I am open to working on this if this is something you would consider.

wmakley commented 6 years ago

I never used that feature, but you are welcome to submit a PR if you have time to work on it! I probably won't for a week or two. Just add an options parameter to the constructor, and pass it to instance_exec.