-
Notifications
You must be signed in to change notification settings - Fork 21
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
Conversation
There was a problem hiding this 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) | |||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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:
- You can default
root
argument to''
instead ofnil
and then validate it on not being anything except string or symbol. - You can leave default
root
argument as it is (nil
), and then validate it on being eithernil
, 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?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" => {...} }'
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice decision! 👍
lib/surrealist/copier.rb
Outdated
# | ||
# @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 |
There was a problem hiding this comment.
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? 🤔
lib/surrealist/copier.rb
Outdated
# @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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing parameter name.
spec/carriers/params.rb
Outdated
{ 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 }, |
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
@AlessandroMinali what do you think? |
…bol. Add support for root as symbol
spec/root_spec.rb
Outdated
end | ||
end | ||
|
||
class CatFood |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ?
spec/root_spec.rb
Outdated
|
||
it 'include_root' do | ||
expect(JSON.parse(instance.surrealize(root: 'guitar', include_root: true))) | ||
.to eq('guitar' => { 'guitar' => { 'brand_name' => 'Fender' } }) |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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' }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
…/surrealist into add_root_argument
@chrisatanasian look good! Please adjust README to reflect the changes that we made since PR was created (how |
1 similar comment
There was a problem hiding this 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