Class: Dentaku::AST::Pluck

Inherits:
Enum show all
Defined in:
lib/dentaku/ast/functions/pluck.rb

Constant Summary

Constants inherited from Function

Function::DIG

Instance Attribute Summary

Attributes inherited from Function

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Enum

#dependencies, #initialize, #validate_identifier

Methods inherited from Function

#accept, #dependencies, get, #initialize, numeric, register, register_class, registry

Methods inherited from Node

arity, #dependencies, #name, precedence, resolve_class, #type

Constructor Details

This class inherits a constructor from Dentaku::AST::Enum

Class Method Details

.max_param_countObject



11
12
13
# File 'lib/dentaku/ast/functions/pluck.rb', line 11

def self.max_param_count
  3
end

.min_param_countObject



7
8
9
# File 'lib/dentaku/ast/functions/pluck.rb', line 7

def self.min_param_count
  2
end

Instance Method Details

#value(context = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dentaku/ast/functions/pluck.rb', line 15

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