-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
232 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module ActiveModel | ||
class Serializer | ||
# Description | ||
|
||
class IncludeTree | ||
class << self | ||
# Builds an IncludeTree from a comma separated list of dot separated paths (JSON API format). | ||
# @example `'posts.author, posts.comments.upvotes, posts.comments.author'` | ||
# | ||
# @param [String] included | ||
# @return [IncludeTree] | ||
# | ||
def from_string(included) | ||
new(include_string_to_hash(included)) | ||
end | ||
|
||
# Translates the arguments passed to the include option into an IncludeTree. | ||
# The format can be either a String (see #from_string), an Array of Symbols and Hashes, or a mix of both. | ||
# @example `posts: [:author, comments: [:author, :upvotes]]` | ||
# | ||
# @param [Symbol, Hash, Array, String] included | ||
# @return [IncludeTree] | ||
# | ||
def from_include_args(included) | ||
new(include_args_to_hash(included)) | ||
end | ||
|
||
private | ||
|
||
def include_string_to_hash(included) | ||
included.delete(' ').split(',').reduce({}) do |hash, path| | ||
include_tree = path.split('.').reverse_each.reduce({}) { |a, e| { e.to_sym => a } } | ||
hash.deep_merge!(include_tree) | ||
end | ||
end | ||
|
||
def include_args_to_hash(included) | ||
case included | ||
when Symbol | ||
{ included => {} } | ||
when Hash | ||
included.each_with_object({}) { |(key, value), hash| | ||
hash[key] = include_args_to_hash(value) | ||
} | ||
when Array | ||
included.reduce({}) { |a, e| a.merge!(include_args_to_hash(e)) } | ||
when String | ||
include_string_to_hash(included) | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
|
||
# @param [Hash{Symbol => IncludeTree}] hash | ||
def initialize(hash = {}) | ||
@hash = hash | ||
end | ||
|
||
def key?(key) | ||
@hash.key?(key) || @hash.key?(:*) || @hash.key?(:**) | ||
end | ||
|
||
def [](key) | ||
# TODO(beauby): Adopt a lazy caching strategy for generating subtrees. | ||
case | ||
when @hash.key?(key) | ||
self.class.new(@hash[key]) | ||
when @hash.key?(:*) | ||
self.class.new(@hash[:*]) | ||
when @hash.key?(:**) | ||
self.class.new(:** => {}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class IncludeTree | ||
class FromStringTest < Minitest::Test | ||
def test_simple_array | ||
input = [:comments, :author] | ||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input) | ||
assert(actual.key?(:author)) | ||
assert(actual.key?(:comments)) | ||
end | ||
|
||
def test_nested_array | ||
input = [:comments, posts: [:author, comments: [:author]]] | ||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input) | ||
assert(actual.key?(:posts)) | ||
assert(actual[:posts].key?(:author)) | ||
assert(actual[:posts].key?(:comments)) | ||
assert(actual[:posts][:comments].key?(:author)) | ||
assert(actual.key?(:comments)) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.