class SimpleJsonapi::Parameters::IncludeSpec
Represents the include
parameter as defined by the JSONAPI spec.
Public Class Methods
new(*specs)
click to toggle source
@param specs [String,Array<String>,Array<Symbol>]
e.g. <code>"author,comments,comments.author"</code> or <code>["author", "comments", "comments.author"]</code>
# File lib/simple_jsonapi/parameters/include_spec.rb, line 16 def initialize(*specs) @data = {} merge(*specs) if specs.any? end
wrap(specs)
click to toggle source
Wraps an include
parameter in an {IncludeSpec} instance. @param specs [IncludeSpec,String,Array<String>,Array<Symbol>]
# File lib/simple_jsonapi/parameters/include_spec.rb, line 6 def self.wrap(specs) if specs.is_a?(IncludeSpec) specs else IncludeSpec.new(specs) end end
Public Instance Methods
[](relationship_name)
click to toggle source
@param relationship_name [String,Symbol] @return [IncludeSpec]
# File lib/simple_jsonapi/parameters/include_spec.rb, line 41 def [](relationship_name) @data[relationship_name.to_sym] end
include?(relationship_name)
click to toggle source
@param relationship_name [String,Symbol]
# File lib/simple_jsonapi/parameters/include_spec.rb, line 46 def include?(relationship_name) @data.key?(relationship_name.to_sym) end
merge(*specs)
click to toggle source
@param specs [String,Array<String>,Array<Symbol>]
e.g. <code>"author,comments,comments.author"</code> or <code>["author", "comments", "comments.author"]</code>
@return [self]
# File lib/simple_jsonapi/parameters/include_spec.rb, line 24 def merge(*specs) paths = specs.flatten.flat_map { |s| s.to_s.split(",") } paths.each do |path| terms = path.split(".") nested_spec = @data[terms.first.to_sym] ||= IncludeSpec.new if terms.size > 1 nested_spec.merge(terms.drop(1).join(".")) end end self end
to_h()
click to toggle source
@return [Hash]
# File lib/simple_jsonapi/parameters/include_spec.rb, line 51 def to_h @data.each_with_object({}) do |(name, spec), hash| hash[name] = spec.to_h end end