Sha256: a2fc7de60c4631b8f3397a48bf572b4f7014ec27c4970d881718646cf524e3c3

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

module JMESPath
  # @api private
  module Nodes
    class Projection < Node
      def initialize(target, projection)
        @target = target
        @projection = projection
      end

      def visit(value)
        if (targets = extract_targets(@target.visit(value)))
          list = []
          targets.each do |v|
            vv = @projection.visit(v)
            unless vv.nil?
              list << vv
            end
          end
          list
        end
      end

      def optimize
        if @projection.is_a?(Current)
          fast_instance
        else
          self.class.new(@target.optimize, @projection.optimize)
        end
      end

      private

      def extract_targets(left_value)
        nil
      end
    end

    module FastProjector
      def visit(value)
        if (targets = extract_targets(@target.visit(value)))
          targets.compact
        end
      end
    end

    class ArrayProjection < Projection
      def extract_targets(target)
        if target.respond_to?(:to_ary)
          target.to_ary
        else
          nil
        end
      end

      def fast_instance
        FastArrayProjection.new(@target.optimize, @projection.optimize)
      end
    end

    class FastArrayProjection < ArrayProjection
      include FastProjector
    end

    class ObjectProjection < Projection
      def extract_targets(target)
        if target.respond_to?(:to_hash)
          target.to_hash.values
        elsif target.is_a?(Struct)
          target.values
        else
          nil
        end
      end

      def fast_instance
        FastObjectProjection.new(@target.optimize, @projection.optimize)
      end
    end

    class FastObjectProjection < ObjectProjection
      include FastProjector
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jmespath-1.6.1 lib/jmespath/nodes/projection.rb
jmespath-1.6.0 lib/jmespath/nodes/projection.rb
jmespath-1.5.0 lib/jmespath/nodes/projection.rb