File: flatten.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 (28 lines) | stat: -rw-r--r-- 583 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
# frozen_string_literal: true
module JMESPath
  # @api private
  module Nodes
    class Flatten < Node
      def initialize(child)
        @child = child
      end

      def visit(value)
        value = @child.visit(value)
        if value.respond_to?(:to_ary)
          value.to_ary.each_with_object([]) do |v, values|
            if v.respond_to?(:to_ary)
              values.concat(v.to_ary)
            else
              values.push(v)
            end
          end
        end
      end

      def optimize
        self.class.new(@child.optimize)
      end
    end
  end
end