-
Notifications
You must be signed in to change notification settings - Fork 17
Add IncludeDirective#merge method #27
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
Changes from 3 commits
37daf54
12e5dc4
20b99e3
cd113f5
59ed259
092abaf
9eba292
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ module JSONAPI | |
# @example 'posts.**' # => Include related posts, and all the included | ||
# posts' related resources, and their related resources, recursively. | ||
class IncludeDirective | ||
attr_reader :options, :hash | ||
|
||
# @param include_args (see Parser.include_hash_from_include_args) | ||
def initialize(include_args, options = {}) | ||
include_hash = Parser.parse_include_args(include_args) | ||
|
@@ -40,6 +42,28 @@ def [](key) | |
end | ||
end | ||
|
||
# @param another_directive [IncludeDirective] | ||
# @return [IncludeDirective] | ||
def merge(other) | ||
fail ArgumentError, | ||
"the value of 'other' MUST be an IncludeDirective" unless | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure the error message should reference the parameter name as it would not be clear out of context. |
||
other.is_a?(IncludeDirective) | ||
|
||
hash = to_hash.dup | ||
Parser.deep_merge!(hash, other.to_hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem if you do that is that when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually storing the result after duplicating it with |
||
|
||
IncludeDirective.new(hash, options.merge!(other.options)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The semantics of options is not very clear during a merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can I make it clearer 😄? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem here is that if one has two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I see your point. I took this decision thinking about Currently you only need to specify The only case this is not working as everyone would expect, is when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. I think the proper way of handling that would be to have a dedicated method to merge the options. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, can do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok method is ready, just need a strategy for it, or I can leave the current implementation of |
||
end | ||
|
||
# @param another_directive [IncludeDirective] | ||
# @return [IncludeDirective] | ||
def merge!(other) | ||
new = merge(other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd stay clear from |
||
@hash = new.hash | ||
@options = new.options | ||
self | ||
end | ||
|
||
# @return [Hash{Symbol => Hash}] | ||
def to_hash | ||
@hash.each_with_object({}) do |(key, value), hash| | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'jsonapi/include_directive' | ||
|
||
describe JSONAPI::IncludeDirective, '.merge' do | ||
it 'works' do | ||
d1 = JSONAPI::IncludeDirective.new( | ||
{ | ||
post: { | ||
comments: { | ||
references: [:url] | ||
}, | ||
author: [:address] | ||
} | ||
} | ||
) | ||
d2 = JSONAPI::IncludeDirective.new( | ||
{ | ||
post: { | ||
comments: [:length], | ||
author: {}, | ||
created_at: {} | ||
} | ||
} | ||
) | ||
expected = JSONAPI::IncludeDirective.new( | ||
{ | ||
post: { | ||
comments: { | ||
references: [:url], | ||
length: {} | ||
}, | ||
author: [:address], | ||
created_at: {} | ||
} | ||
} | ||
).to_hash | ||
|
||
expect(d1.merge(d2).to_hash).to eq(expected) | ||
d1.merge!(d2) | ||
expect(d1.to_hash).to eq(expected) | ||
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.
hash
should not have a reader - use theto_hash
method or@hash
instead.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.
unfortunately I need it in #merge! to do
merge_result.hash
(just renamed fromnew
)Uh oh!
There was an error while loading. Please reload this page.
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.
Instead of exposing the hash, you could just make
IncludeDirective
enumerable bythen
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.
And also
JSONAPI::IncludeDirective#hash
overridesObject#hash
method that is used for equality matching.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.
Thanks for the advices!
I will only be able to add more commits from the 20th of September as I'm on holiday at the moment 😅