Sha256: 4b2e56cabbafe0a3ccee8c3a39df0e662e7b2f9fe8f7c6bd41c6c56547b67006

Contents?: true

Size: 1018 Bytes

Versions: 3

Compression:

Stored size: 1018 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jmespath-1.6.1 lib/jmespath/util.rb
jmespath-1.6.0 lib/jmespath/util.rb
jmespath-1.5.0 lib/jmespath/util.rb