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

Add root argument #32

Merged
merged 18 commits into from
Oct 17, 2017
Merged

Add root argument #32

merged 18 commits into from
Oct 17, 2017

Conversation

chrisatanasian
Copy link
Contributor

@nesaulov nesaulov self-requested a review October 14, 2017 19:12
Copy link
Owner

@nesaulov nesaulov left a comment

Choose a reason for hiding this comment

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

Please also fix rubocop errors, so we can see if test pass. You can run $ bundle exec rake before push to make sure that everything is okay.

README.md Outdated
@@ -5,7 +5,7 @@
[![Gem Version](https://badge.fury.io/rb/surrealist.svg)](https://rubygems.org/gems/surrealist)

![Surrealist](surrealist-icon.png)

Copy link
Owner

Choose a reason for hiding this comment

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

Could you please rollback changes (except actual root documentation) here? Those whitespaces are necessary for adequate readability of README.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my editor trims whitespaces on save. Fixed.

@coveralls
Copy link

coveralls commented Oct 14, 2017

Coverage Status

Coverage increased (+0.03%) to 99.576% when pulling e316b77 on chrisatanasian:add_root_argument into 34aa5bb on nesaulov:master.

Copy link
Owner

@nesaulov nesaulov left a comment

Choose a reason for hiding this comment

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

A few changes here and we should be ready to go!

@camelize = camelize
@include_root = include_root
@include_namespaces = include_namespaces
@root = root
Copy link
Owner

Choose a reason for hiding this comment

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

We need to add sanitization of root parameter to #sanitize! method and specs for it (spec/carrier_spec.rb). Right now there are specs only for strings and nil. It should work with symbols as well (Something.surrealize(root: :wrapper)). I suggest that we raise ArgumentError if root is anything except non-empty string or a symbol (Class, Integer and so on).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about nil? Seems like the way the code path is setup, Surrealist#call is always expecting something for all of its arguments. In the case of include_root, for example, this is fine because it just defaults to false, which does nothing. But if only non-empty string and a symbol are allowed for root, then there is no way to prevent the schema from being wrapped into root.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, good point. There are two ways here:

  1. You can default root argument to '' instead of nil and then validate it on not being anything except string or symbol.
  2. You can leave default root argument as it is (nil), and then validate it on being either nil, non-empty string or a symbol.

I think I prefer the second option, it feels more naturally to have nil if we don't want to do anything. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think '2' is better and less arbitrary than empty string. I think the validation/sanitation needs to considers blank strings/symbols. We need to disallow things like " " of :" ", and maybe even consider stripping white space from these args: " hello " => "hello".

This may be something that we should also go back and check on for other params potentially @nesaulov ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, nil seems more natural to me.

Copy link
Owner

Choose a reason for hiding this comment

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

@AlessandroMinali yes, you are right, we need to strip string argument. This is not relevant for other arguments, because they are either booleans or integers. @chrisatanasian could you please add a method in Carrier.sanitize! that will strip root argument?
And also add specs for this case:
Animal.surrealize(root: ' wat ') #=> '{ "wat" => {...} }'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -14,19 +14,39 @@ class << self
def deep_copy(hash:, klass: false, carrier:)
namespaces_condition = carrier.include_namespaces || carrier.namespaces_nesting_level != DEFAULT_NESTING_LEVEL # rubocop:disable Metrics/LineLength

return copy_hash(hash) unless carrier.include_root || namespaces_condition
if !klass && (carrier.include_root || namespaces_condition)
Copy link
Owner

Choose a reason for hiding this comment

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

Nice decision! 👍

#
# @return [Hash] a hash with schema wrapped inside the specified root key.
def wrap_schema_into_specified_root(schema:, carrier:)
root_key = if carrier.camelize
Copy link
Owner

Choose a reason for hiding this comment

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

This looks very similar to #wrap_schema_into_root. Maybe we can DRY it out a bit? 🤔

# @param [Object] hash object to be copied.
# @param [String] klass instance's class name.
# @param [Object] carrier instance of Carrier class that carries arguments passed to +surrealize+
# @param [Bool] whether to wrap into namespace.
Copy link
Owner

Choose a reason for hiding this comment

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

Missing parameter name.

{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 435 },
{ camelize: true, include_namespaces: false, include_root: true, namespaces_nesting_level: 666 },
{ camelize: true, include_namespaces: true, include_root: true,
root: '', namespaces_nesting_level: 3 },
Copy link
Owner

Choose a reason for hiding this comment

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

Again, I don't think that root: '' should be valid.

README.md Outdated
``` ruby
class Cat
include Surrealist

Copy link
Owner

Choose a reason for hiding this comment

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

Please add a whitespaces here and on line 368.

README.md Outdated
@@ -354,6 +354,34 @@ features (like associations, inheritance etc) are supported and covered. Other O
issues as well, tests are in progress. All optional arguments (`camelize`, `include_root` etc) are also supported.
Guides on where to use `#surrealize_collection` vs `#surrealize` for all ORMs are coming.

### Root
If you want to wrap the resulting JSON into a specified root key, you can pass optional `root` argument
to `#surrealize` or `#build_schema`. The root key in this case will be taken from the class name of the
Copy link
Owner

@nesaulov nesaulov Oct 14, 2017

Choose a reason for hiding this comment

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

The root key in this case will be taken from the class name of the
surrealizable object.

This sentence is irrelevant here (it is for include_root argument)
Also please add link to here from table of contents.

@nesaulov
Copy link
Owner

@AlessandroMinali what do you think?

@coveralls
Copy link

coveralls commented Oct 15, 2017

Coverage Status

Coverage increased (+0.01%) to 99.563% when pulling cb3ecd9 on chrisatanasian:add_root_argument into 34aa5bb on nesaulov:master.

@coveralls
Copy link

coveralls commented Oct 15, 2017

Coverage Status

Coverage increased (+0.02%) to 99.574% when pulling f5986c1 on chrisatanasian:add_root_argument into 04005d5 on nesaulov:master.

end
end

class CatFood
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to clean up the spec this can just be a Struct?

@camelize = camelize
@include_root = include_root
@include_namespaces = include_namespaces
@root = root
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think '2' is better and less arbitrary than empty string. I think the validation/sanitation needs to considers blank strings/symbols. We need to disallow things like " " of :" ", and maybe even consider stripping white space from these args: " hello " => "hello".

This may be something that we should also go back and check on for other params potentially @nesaulov ?


it 'include_root' do
expect(JSON.parse(instance.surrealize(root: 'guitar', include_root: true)))
.to eq('guitar' => { 'guitar' => { 'brand_name' => 'Fender' } })
Copy link
Owner

Choose a reason for hiding this comment

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

Actually, I find this really confusing. If I wrote instance.surrealize(include_root: true, root: 'guitar') I would expect only one root, e.g. { 'guitar' => { 'brand_name' => 'Fender' } } in this case. But I don't know if I am right on this. @AlessandroMinali ?

Copy link
Collaborator

@AlessandroMinali AlessandroMinali Oct 15, 2017

Choose a reason for hiding this comment

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

For what makes sense for me is that root param overrides include_root.

instance.surrealize(include_root: true, root: 'car')
# => { 'car' => { 'brand_name' => 'Fender' } }

instance.surrealize(root: 'car')
# => { 'car' => { 'brand_name' => 'Fender' } }

instance.surrealize(include_root: true)
# => { 'guitar' => { 'brand_name' => 'Fender' } }

instance.surrealize(include_root: false, root: 'car')
# =>  { 'car' => { 'brand_name' => 'Fender' } }

instance.surrealize(include_root: false)
# =>  { 'brand_name' => 'Fender' }

instance.surrealize()
# => { 'brand_name' => 'Fender' }

Copy link
Owner

Choose a reason for hiding this comment

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

Exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So would root override include_namespaces too?

Copy link
Owner

Choose a reason for hiding this comment

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

@chrisatanasian yeah, I guess so.

@coveralls
Copy link

coveralls commented Oct 17, 2017

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling da1054a on chrisatanasian:add_root_argument into 04005d5 on nesaulov:master.

@nesaulov
Copy link
Owner

@chrisatanasian look good! Please adjust README to reflect the changes that we made since PR was created (how root works with include_namespaces and include_root) and I think this is it.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 78375c7 on chrisatanasian:add_root_argument into 04005d5 on nesaulov:master.

1 similar comment
@coveralls
Copy link

coveralls commented Oct 17, 2017

Coverage Status

Coverage increased (+0.03%) to 99.578% when pulling 78375c7 on chrisatanasian:add_root_argument into 04005d5 on nesaulov:master.

Copy link
Owner

@nesaulov nesaulov left a comment

Choose a reason for hiding this comment

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

@chrisatanasian Great job! Thank you for your contribution! 🎉

@nesaulov nesaulov merged commit f2a066e into nesaulov:master Oct 17, 2017
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.

4 participants