nesaulov/surrealist

DSL for serializer contexts

nesaulov opened this issue · 4 comments

As @AlessandroMinali suggested in #61:

Maybe this could be another DSL (at the cost of adding more complexity for a low use case, maybe not worth)?

Right now we have to access context variables via context[:variable_name]. Suggested is the following DSL:

class IncomeSerializer < Surrealist::Serializer
  json_schema { { amount: Integer } }
  serializer_context :current_user, :something, :else
  
  def amount
    current_user.guest? ? 100000000 : object.amount
  end
end

I suggest to implement this feature (if we decide to implement it in the first place) after 1.0.

Sorry, my mistake )

It seems not cool, here we write json_schema, there - serializer_context. Maybe we should use json_context?

I think that still gets the point across, I don't feel strongly about either. Should we add an method alias here as well for a plural version :json_contexts to be dev friendly?

I think that json_context is a bit misleading here. It's a context which is available inside a serializer instance, not inside JSON object. I am with the plural version idea though.