Sha256: baf28f798d7e1d5c741f124c2db5eea764dc1776537037b2a865eb7c72c89140

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require_relative '../function'
require_relative '../../exceptions'

module Dentaku
  module AST
    class Enum < Function
      def self.min_param_count
        3
      end

      def self.max_param_count
        3
      end

      def initialize(*args)
        super
        validate_identifier(@args[1])
      end

      def dependencies(context = {})
        collection      = @args[0]
        item_identifier = @args[1].identifier
        expression      = @args[2]

        collection_deps = collection.dependencies(context)
        expression_deps = (expression&.dependencies(context) || []).reject do |i|
          i == item_identifier || i.start_with?("#{item_identifier}.")
        end

        collection_deps + expression_deps
      end

      def validate_identifier(arg, message = "#{name}() requires second argument to be an identifier")
        raise ParseError.for(:node_invalid), message unless arg.is_a?(Identifier)
      end

      private

      def mapped_value(expression, context, item_context)
        expression.value(
          context.merge(
            FlatHash.from_hash_with_intermediates(item_context)
          )
        )
      rescue => e
        raise e if context["__evaluation_mode"] == :strict
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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