Module: JSONAPI::IncludeDirective::Parser

Defined in:
lib/jsonapi/include_directive/parser.rb

Overview

Utilities to create an IncludeDirective hash from various types of inputs.

Class Method Summary collapse

Class Method Details

.deep_merge!(src, ext) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
62
# File 'lib/jsonapi/include_directive/parser.rb', line 54

def deep_merge!(src, ext)
  ext.each do |k, v|
    if src[k].is_a?(Hash) && v.is_a?(Hash)
      deep_merge!(src[k], v)
    else
      src[k] = v
    end
  end
end

.parse_array(include_array) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
# File 'lib/jsonapi/include_directive/parser.rb', line 47

def parse_array(include_array)
  include_array.each_with_object({}) do |x, hash|
    deep_merge!(hash, parse_include_args(x))
  end
end

.parse_hash(include_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
43
44
# File 'lib/jsonapi/include_directive/parser.rb', line 40

def parse_hash(include_hash)
  include_hash.each_with_object({}) do |(key, value), hash|
    hash[key.to_sym] = parse_include_args(value)
  end
end

.parse_include_args(include_args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jsonapi/include_directive/parser.rb', line 9

def parse_include_args(include_args)
  case include_args
  when Symbol
    { include_args => {} }
  when Hash
    parse_hash(include_args)
  when Array
    parse_array(include_args)
  when String
    parse_string(include_args)
  else
    {}
  end
end

.parse_path_string(include_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
36
37
# File 'lib/jsonapi/include_directive/parser.rb', line 33

def parse_path_string(include_path)
  include_path.split('.')
    .reverse
    .reduce({}) { |a, e| { e.to_sym => a } }
end

.parse_string(include_string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
# File 'lib/jsonapi/include_directive/parser.rb', line 25

def parse_string(include_string)
  include_string.split(',')
    .each_with_object({}) do |path, hash|
      deep_merge!(hash, parse_path_string(path))
  end
end