Sha256: 2fde2a38be8a76a7e2faad3620edb67fba27322dd1e9174202ebdcae0ef45503

Contents?: true

Size: 807 Bytes

Versions: 1

Compression:

Stored size: 807 Bytes

Contents

require_relative './enum'
require_relative '../../exceptions'

module Dentaku
  module AST
    class Pluck < Enum
      def self.min_param_count
        2
      end

      def self.max_param_count
        3
      end

      def value(context = {})
        collection = Array(@args[0].value(context))

        unless collection.all? { |elem| elem.is_a?(Hash) }
          raise ArgumentError.for(:incompatible_type, value: collection),
                'PLUCK() requires first argument to be an array of hashes'
        end

        pluck_path = @args[1].identifier
        default    = @args[2]

        collection.map { |h|
          h.transform_keys(&:to_s).fetch(pluck_path, default&.value(context))
        }
      end
    end
  end
end

Dentaku::AST::Function.register_class(:pluck, Dentaku::AST::Pluck)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dentaku-3.5.4 lib/dentaku/ast/functions/pluck.rb