Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce an abstract serializer class #61

Merged
merged 16 commits into from
Jan 17, 2018
Merged

Introduce an abstract serializer class #61

merged 16 commits into from
Jan 17, 2018

Conversation

nesaulov
Copy link
Owner

#59

@nesaulov nesaulov added the WIP label Jan 10, 2018
@nesaulov nesaulov self-assigned this Jan 10, 2018
@nesaulov
Copy link
Owner Author

@AlessandroMinali haven't I missed something?

@nesaulov
Copy link
Owner Author

I have added possibility to pass context inside serializer constructor: for objects that are not coupled with serializable model, but still have to be accessed from the inside (I guess it will be current_user in most cases)
Usage:

class IncomeSerializer < Surrealist::Serializer
  json_schema { { amount: Integer } }
  
  def amount
    current_user.guest? ? 100000000 : object.amount
  end
  
  def current_user
    context[:current_user]
  end
end

class Income
  include Surrealist
  surrealize_with IncomeSerializer
  
  attr_reader :amount
  
  def initialize(amount)
    @amount = amount
  end  
end

income = Income.new(200)
IncomeSerializer.new(income, current_user: GuestUser.new).surrealize
# => '{ "amount": 100000000 }'

IncomeSerializer.new(income, current_user: User.find(3)).surrealize
# => '{ "amount": 200 }'

Also I will write good docs for all of this, but a bit after the feature itself.

@AlessandroMinali
Copy link
Collaborator

Looks good. Context passing seems a little weird (but functional), but the only alternatives I can think of is using method missing or a branch condition on every method call to check a hash of valid functions which will just be slow.

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

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

end



Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

json_schema { { name: String } }
end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

@nesaulov
Copy link
Owner Author

@AlessandroMinali I think this DSL might be useful, but we should give it a thought. For now I suggest to merge this PR, if there are no problems or suggestions, and to file an issue concerning the DSL you proposed where we could cover all pros and cons.

@nesaulov nesaulov removed the WIP label Jan 12, 2018
@nesaulov nesaulov changed the title [WIP] Introduce an abstract serializer class Introduce an abstract serializer class Jan 12, 2018
@nesaulov nesaulov requested review from AlessandroMinali and removed request for AlessandroMinali January 15, 2018 18:09
@nesaulov nesaulov merged commit 44f9003 into master Jan 17, 2018
@nesaulov nesaulov mentioned this pull request Jan 26, 2018
This was referenced Feb 3, 2018
@nesaulov nesaulov deleted the serializer_class branch February 8, 2018 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants