File: util.rb

package info (click to toggle)
ruby-jmespath 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 2,039; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download
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
# frozen_string_literal: true
module JMESPath
  # @api private
  module Util
    class << self
      # Determines if a value is false as defined by JMESPath:
      #
      #   https://github.com/jmespath/jmespath.site/blob/master/docs/proposals/improved-filters.rst#and-expressions-1
      #
      def falsey?(value)
        !value ||
          (value.respond_to?(:to_ary) && value.to_ary.empty?) ||
          (value.respond_to?(:to_hash) && value.to_hash.empty?) ||
          (value.respond_to?(:to_str) && value.to_str.empty?) ||
          (value.respond_to?(:entries) && !value.entries.any?)
        # final case necessary to support Enumerable and Struct
      end

      def as_json(value)
        if value.respond_to?(:to_ary)
          value.to_ary.map { |e| as_json(e) }
        elsif value.respond_to?(:to_hash)
          hash = {}
          value.to_hash.each_pair { |k, v| hash[k] = as_json(v) }
          hash
        elsif value.respond_to?(:to_str)
          value.to_str
        else
          value
        end
      end
    end
  end
end