1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
require 'jsonapi/include_directive/parser'
module JSONAPI
# Represent a recursive set of include directives
# (c.f. http://jsonapi.org/format/#fetching-includes)
#
# Addition to the spec: two wildcards, namely '*' and '**'.
# The former stands for any one level of relationship, and the latter stands
# for any number of levels of relationships.
# @example 'posts.*' # => Include related posts, and all the included posts'
# related resources.
# @example 'posts.**' # => Include related posts, and all the included
# posts' related resources, and their related resources, recursively.
class IncludeDirective
# @param include_args (see Parser.parse_include_args)
def initialize(include_args, options = {})
include_hash = Parser.parse_include_args(include_args)
@hash = include_hash.each_with_object({}) do |(key, value), hash|
hash[key] = self.class.new(value, options)
end
@options = options
end
# @param key [Symbol, String]
def key?(key)
@hash.key?(key.to_sym) ||
(@options[:allow_wildcard] && (@hash.key?(:*) || @hash.key?(:**)))
end
# @return [Array<Symbol>]
def keys
@hash.keys
end
# @param key [Symbol, String]
# @return [IncludeDirective, nil]
def [](key)
case
when @hash.key?(key.to_sym)
@hash[key.to_sym]
when @options[:allow_wildcard] && @hash.key?(:**)
self.class.new({ :** => {} }, @options)
when @options[:allow_wildcard] && @hash.key?(:*)
@hash[:*]
end
end
# @return [Hash{Symbol => Hash}]
def to_hash
@hash.each_with_object({}) do |(key, value), hash|
hash[key] = value.to_hash
end
end
# @return [String]
def to_string
string_array = @hash.map do |(key, value)|
string_value = value.to_string
if string_value == ''
key.to_s
else
string_value
.split(',')
.map { |x| key.to_s + '.' + x }
.join(',')
end
end
string_array.join(',')
end
end
end
|