Module: JsonapiSerializer::Utils

Included in:
Base, Polymorphic
Defined in:
lib/jsonapi_serializer/utils.rb

Instance Method Summary collapse

Instance Method Details

#apply_splat(item, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/jsonapi_serializer/utils.rb', line 31

def apply_splat(item, &block)
  if item.is_a? Array
    item.map { |i| block.call(i) }
  else
    block.call(item)
  end
end

#key_intersect(limited, full) ⇒ Object



39
40
41
# File 'lib/jsonapi_serializer/utils.rb', line 39

def key_intersect(limited, full)
  limited && limited & full || full
end

#normalize_fields(fields) ⇒ Object



25
26
27
28
29
# File 'lib/jsonapi_serializer/utils.rb', line 25

def normalize_fields(fields)
  fields.each_with_object({}) do |(type, attributes), hash|
    hash[type.to_sym] = [*attributes].map(&:to_sym)
  end
end

#normalize_includes(includes) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jsonapi_serializer/utils.rb', line 3

def normalize_includes(includes)
  case includes
  when String
    normalize_includes([includes.to_sym])
  when Symbol
    normalize_includes([includes])
  when Hash
    includes.each_with_object({}) do |(key, val), hash|
      hash[key] = normalize_includes(val)
    end
  when Array
    includes.each_with_object({}) do |entry, hash|
      case entry
      when Symbol
        hash[entry] = {}
      when Hash
        hash.merge!(normalize_includes(entry))
      end
    end
  end
end