-
Notifications
You must be signed in to change notification settings - Fork 17
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 IncludeDirective#merge method #27
Changes from 5 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,29 @@ def [](key) | |
end | ||
end | ||
|
||
# @param another_directive [IncludeDirective] | ||
# @return [IncludeDirective] | ||
def merge(other) | ||
fail ArgumentError, | ||
"parameter MUST be an IncludeDirective" unless | ||
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 |
||
merged_options = merge_options(options, other.options) | ||
|
||
IncludeDirective.new(hash, merged_options) | ||
end | ||
|
||
# @param another_directive [IncludeDirective] | ||
# @return [IncludeDirective] | ||
def merge!(other) | ||
merge_result = 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. As a general good practice, I tend to favor defining the "safe" method in terms of the "dangerous" method rather than the opposite. |
||
@hash = merge_result.hash | ||
@options = merge_result.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. After giving a thought about the options merging thing, I believe the most sane thing to do here is to assume that when one merges an |
||
self | ||
end | ||
|
||
# @return [Hash{Symbol => Hash}] | ||
def to_hash | ||
@hash.each_with_object({}) do |(key, value), hash| | ||
|
@@ -63,5 +88,11 @@ def to_string | |
|
||
string_array.join(',') | ||
end | ||
|
||
private | ||
|
||
def merge_options(opts, other_opts) | ||
opts.merge(other_opts) | ||
end | ||
end | ||
end |
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
)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 😅