-
Notifications
You must be signed in to change notification settings - Fork 1
Refactoring #1
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
base: master
Are you sure you want to change the base?
Refactoring #1
Conversation
rafasoares
left a 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.
Some comments on the Data::Base class
lib/trustly/data/base.rb
Outdated
| ret = data.each_with_object([]) do |element, acc| | ||
| processed_element = vacuum(element) unless element.nil? | ||
| next if processed_element.nil? | ||
|
|
||
| acc.push(processed_element) | ||
| end |
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 like it could be replaced by data.compact.
Or filter_map, since you're targeting Ruby 2.7+
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.
not exactly, compact doesn't get rid of empty arrays or hashes, while this recursive vacuum does something like:
=> cleaner.vacuum([1,2,[],{a: {}}])
=> [1,2]
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, it was an oversight, but filter_map would cover that, since you could just recursively call vacuum in it.
lib/trustly/data/base.rb
Outdated
|
|
||
| acc.push(processed_element) | ||
| end | ||
| ret.length.zero? ? nil : ret |
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.
| ret.length.zero? ? nil : ret | |
| ret.empty? ? nil : ret |
Or ret.presence if you're willing to add a dependency to active_support/core_ext.
lib/trustly/data/base.rb
Outdated
| ret = data.each_with_object({}) do |(key, element), acc| | ||
| processed_element = vacuum(element) unless element.nil? | ||
| next if processed_element.nil? | ||
|
|
||
| acc[key] = processed_element | ||
| end | ||
| ret.length.zero? ? nil : ret |
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.
Same as the array method above
lib/trustly/data/base.rb
Outdated
| def vacuum(data) | ||
| case data | ||
| when Array then vacuum_array_data(data) | ||
| when Hash then vacuum_hash_data(data) | ||
| else data | ||
| end | ||
| end |
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 feel like this entire "vacuum" logic belongs in a separate class/module like DataCleaner or something like that. It can still be a child of the trustly/data module if it doesn't apply anywhere else, but I'd rather have it outside the Base class.
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.
It could very well be a module that gets included in the Base class, but it would still be separate in the code.
lib/trustly/data/base.rb
Outdated
| def stringify_hash(hash) | ||
| hash.each_with_object({}) do |(k, v), acc| | ||
| acc[k.to_s] = v.is_a?(Hash) ? stringify_hash(v) : v | ||
| end | ||
| end |
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.
Missed case when v is array
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 should probably rename it to deep_stringify_keys, but right, arrays are not handled here :)
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.
Sometimes I wish active_support/core_ext was part of the Ruby stdlib 🙃
Added rubocop and specs (with simplecov), faraday is used for web requests, Ruby 2.7 is set as a lower bound required version.