Method: Fauna::Query#lambda

Defined in:
lib/fauna/query.rb

#lambda(&block) ⇒ Object

A lambda expression

Reference: FaunaDB Basic Forms

This form generates #var objects for you, and is called like:

Query.lambda do |a|
  Query.add a, a
end
# Produces: {lambda: :a, expr: {add: [{var: :a}, {var: :a}]}}

Query functions requiring lambdas can be passed blocks without explicitly calling #lambda.

You can also use #lambda_expr and #var directly.

block

Takes one or more #var expressions and uses them to construct an expression. If this takes more than one argument, the lambda destructures an array argument. (To destructure single-element arrays use #lambda_expr.)



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fauna/query.rb', line 156

def lambda(&block)
  dsl = Query::QueryDSLContext.new
  vars =
    block.parameters.map do |kind, name|
      fail ArgumentError, 'Splat parameters are not supported in lambda expressions.' if kind == :rest
      name
    end

  case vars.length
  when 0
    fail ArgumentError, 'Block must take at least 1 argument.'
  when 1
    # When there's only 1 parameter, don't use an array pattern.
    lambda_expr vars[0], DSLContext.eval_dsl(dsl, var(vars[0]), &block)
  else
    lambda_expr vars, DSLContext.eval_dsl(dsl, *(vars.map { |v| var(v) }), &block)
  end
end