Sha256: 5413e2b76d0e6c685ef6cdc9733b8b76bab94b7e709420a9a3d1b7c7f207a60d

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'active_support/core_ext/hash/deep_merge'

module JSONAPI
  class IncludeDirective
    # Utilities to create an IncludeDirective hash from various types of
    # inputs.
    module Parser
      module_function

      # @api private
      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

      # @api private
      def parse_string(include_string)
        include_string.split(',')
          .map(&:strip)
          .each_with_object({}) do |path, hash|
          hash.deep_merge!(parse_path_string(path))
        end
      end

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi-0.1.1.beta1 lib/jsonapi/include_directive/parser.rb