yax-fauna 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,899 @@
1
+ module Fauna
2
+ ##
3
+ # Helpers modeling the FaunaDB \Query language.
4
+ #
5
+ # Helpers are usually used via a concise DSL notation. A DSL block
6
+ # may be used directly with Fauna::Client
7
+ #
8
+ # client.query { create(class_('spells'), { data: { name: 'Magic Missile' } }) }
9
+ #
10
+ # To build and return an query expression to execute later, use Fauna::Query.expr
11
+ #
12
+ # Fauna::Query.expr { create(class_('spells'), { data: { name: 'Magic Missile' } }) }
13
+ #
14
+ # Or, you may directly use the helper methods:
15
+ #
16
+ # Fauna::Query.create(Fauna::Query.class_('spells'), { data: { name: 'Magic Missile' } })
17
+ module Query
18
+ extend self, Deprecate
19
+
20
+ class QueryDSLContext < DSLContext # :nodoc:
21
+ include Query
22
+ end
23
+
24
+ ##
25
+ # Build a query expression.
26
+ #
27
+ # Allows for unscoped calls to Fauna::Query methods within the
28
+ # provided block. The block should return the constructed query
29
+ # expression.
30
+ #
31
+ # Example: <code>Fauna::Query.expr { add(1, 2, subtract(3, 2)) }</code>
32
+ def self.expr(&block)
33
+ return nil if block.nil?
34
+
35
+ dsl = QueryDSLContext.new
36
+
37
+ Expr.wrap DSLContext.eval_dsl(dsl, &block)
38
+ end
39
+
40
+ # :section: Values
41
+
42
+ ##
43
+ # Construct a ref value
44
+ #
45
+ # Reference: {FaunaDB Values}[https://fauna.com/documentation/queries#values]
46
+ def ref(str, id = nil)
47
+ if id.nil?
48
+ Expr.new :@ref => Expr.wrap(str)
49
+ else
50
+ Expr.new ref: Expr.wrap(str), id: Expr.wrap(id)
51
+ end
52
+ end
53
+
54
+ ##
55
+ # An abort expression
56
+ #
57
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
58
+ def abort(msg)
59
+ Expr.new abort: Expr.wrap(msg)
60
+ end
61
+
62
+ ##
63
+ # An object expression
64
+ #
65
+ # Query expression constructs can also take a regular ruby object, so the following are equivalent:
66
+ #
67
+ # Fauna.query { { x: 1, y: add(1, 2) } }
68
+ # Fauna.query { object(x: 1, y: add(1, 2)) }
69
+ #
70
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
71
+ def object(fields)
72
+ Expr.new object: Expr.wrap_values(fields)
73
+ end
74
+
75
+ ##
76
+ # A query expression
77
+ #
78
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
79
+ def query(expr)
80
+ Expr.new query: Expr.wrap(expr)
81
+ end
82
+
83
+ # :section: Basic forms
84
+
85
+ ##
86
+ # An at expression
87
+ #
88
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
89
+ def at(timestamp, expr)
90
+ Expr.new at: Expr.wrap(timestamp), expr: Expr.wrap(expr)
91
+ end
92
+
93
+ ##
94
+ # A let expression
95
+ #
96
+ # Only one of +expr+ or +block+ should be provided.
97
+ #
98
+ # Block example: <code>Fauna.query { let(x: 2) { add(1, x) } }</code>.
99
+ #
100
+ # Expression example: <code>Fauna.query { let({ x: 2 }, add(1, var(:x))) }</code>.
101
+ #
102
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
103
+ def let(vars, expr = nil, &block)
104
+ in_ =
105
+ if block.nil?
106
+ expr
107
+ else
108
+ dsl = QueryDSLContext.new
109
+ dslcls = (class << dsl; self; end)
110
+
111
+ vars.keys.each do |v|
112
+ dslcls.send(:define_method, v) { var(v) }
113
+ end
114
+
115
+ DSLContext.eval_dsl(dsl, &block)
116
+ end
117
+
118
+ Expr.new let: Expr.wrap_values(vars), in: Expr.wrap(in_)
119
+ end
120
+
121
+ ##
122
+ # A var expression
123
+ #
124
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
125
+ def var(name)
126
+ Expr.new var: Expr.wrap(name)
127
+ end
128
+
129
+ ##
130
+ # An if expression
131
+ #
132
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
133
+ def if_(condition, then_, else_)
134
+ Expr.new if: Expr.wrap(condition), then: Expr.wrap(then_), else: Expr.wrap(else_)
135
+ end
136
+
137
+ ##
138
+ # A do expression
139
+ #
140
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
141
+ def do_(*expressions)
142
+ Expr.new do: Expr.wrap_varargs(expressions)
143
+ end
144
+
145
+ ##
146
+ # A lambda expression
147
+ #
148
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
149
+ #
150
+ # This form generates #var objects for you, and is called like:
151
+ #
152
+ # Query.lambda do |a|
153
+ # Query.add a, a
154
+ # end
155
+ # # Produces: {lambda: :a, expr: {add: [{var: :a}, {var: :a}]}}
156
+ #
157
+ # Query functions requiring lambdas can be passed blocks without explicitly calling #lambda.
158
+ #
159
+ # You can also use #lambda_expr and #var directly.
160
+ # +block+::
161
+ # Takes one or more #var expressions and uses them to construct an expression.
162
+ # If this takes more than one argument, the lambda destructures an array argument.
163
+ # (To destructure single-element arrays use #lambda_expr.)
164
+ def lambda(&block)
165
+ dsl = Query::QueryDSLContext.new
166
+ vars =
167
+ block.parameters.map do |kind, name|
168
+ fail ArgumentError, 'Splat parameters are not supported in lambda expressions.' if kind == :rest
169
+ name
170
+ end
171
+
172
+ case vars.length
173
+ when 0
174
+ fail ArgumentError, 'Block must take at least 1 argument.'
175
+ when 1
176
+ # When there's only 1 parameter, don't use an array pattern.
177
+ lambda_expr vars[0], DSLContext.eval_dsl(dsl, var(vars[0]), &block)
178
+ else
179
+ lambda_expr vars, DSLContext.eval_dsl(dsl, *(vars.map { |v| var(v) }), &block)
180
+ end
181
+ end
182
+
183
+ ##
184
+ # A raw lambda expression
185
+ #
186
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
187
+ #
188
+ # See also #lambda.
189
+ def lambda_expr(var, expr)
190
+ Expr.new lambda: Expr.wrap(var), expr: Expr.wrap(expr)
191
+ end
192
+
193
+ ##
194
+ # A call expression
195
+ #
196
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
197
+ def call(name, *args)
198
+ Expr.new call: Expr.wrap(name), arguments: Expr.wrap_varargs(args)
199
+ end
200
+
201
+ # :section: Collection Functions
202
+
203
+ ##
204
+ # A map expression
205
+ #
206
+ # Only one of +lambda_expr+ or +lambda_block+ should be provided.
207
+ # For example: <code>Fauna.query { map(collection) { |a| add a, 1 } }</code>.
208
+ #
209
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
210
+ def map(collection, lambda_expr = nil, &lambda_block)
211
+ Expr.new map: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
212
+ end
213
+
214
+ ##
215
+ # A foreach expression
216
+ #
217
+ # Only one of +lambda_expr+ or +lambda_block+ should be provided.
218
+ # For example: <code>Fauna.query { foreach(collection) { |a| delete a } }</code>.
219
+ #
220
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
221
+ def foreach(collection, lambda_expr = nil, &lambda_block)
222
+ Expr.new foreach: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
223
+ end
224
+
225
+ ##
226
+ # A filter expression
227
+ #
228
+ # Only one of +lambda_expr+ or +lambda_block+ should be provided.
229
+ # For example: <code>Fauna.query { filter(collection) { |a| equals a, 1 } }</code>.
230
+ #
231
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
232
+ def filter(collection, lambda_expr = nil, &lambda_block)
233
+ Expr.new filter: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
234
+ end
235
+
236
+ ##
237
+ # A take expression
238
+ #
239
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
240
+ def take(number, collection)
241
+ Expr.new take: Expr.wrap(number), collection: Expr.wrap(collection)
242
+ end
243
+
244
+ ##
245
+ # A drop expression
246
+ #
247
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
248
+ def drop(number, collection)
249
+ Expr.new drop: Expr.wrap(number), collection: Expr.wrap(collection)
250
+ end
251
+
252
+ ##
253
+ # A prepend expression
254
+ #
255
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
256
+ def prepend(elements, collection)
257
+ Expr.new prepend: Expr.wrap(elements), collection: Expr.wrap(collection)
258
+ end
259
+
260
+ ##
261
+ # An append expression
262
+ #
263
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
264
+ def append(elements, collection)
265
+ Expr.new append: Expr.wrap(elements), collection: Expr.wrap(collection)
266
+ end
267
+
268
+ ##
269
+ # A is_empty expression
270
+ #
271
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
272
+ def is_empty(collection)
273
+ Expr.new is_empty: Expr.wrap(collection)
274
+ end
275
+
276
+ ##
277
+ # A is_nonempty expression
278
+ #
279
+ # Reference: {FaunaDB Collections}[https://fauna.com/documentation/queries#collection_functions]
280
+ def is_nonempty(collection)
281
+ Expr.new is_nonempty: Expr.wrap(collection)
282
+ end
283
+
284
+ # :section: Read Functions
285
+
286
+ ##
287
+ # A get expression
288
+ #
289
+ # Reference: {FaunaDB Read functions}[https://fauna.com/documentation/queries#read_functions]
290
+ def get(ref, params = {})
291
+ Expr.new Expr.wrap_values(params).merge(get: Expr.wrap(ref))
292
+ end
293
+
294
+ ##
295
+ # A key_from_secret expression
296
+ #
297
+ # Reference: {FaunaDB Read functions}[https://fauna.com/documentation/queries#read_functions]
298
+ def key_from_secret(secret)
299
+ Expr.new key_from_secret: Expr.wrap(secret)
300
+ end
301
+
302
+ ##
303
+ # A paginate expression
304
+ #
305
+ # Reference: {FaunaDB Read functions}[https://fauna.com/documentation/queries#read_functions]
306
+ def paginate(set, params = {})
307
+ Expr.new Expr.wrap_values(params).merge(paginate: Expr.wrap(set))
308
+ end
309
+
310
+ ##
311
+ # An exists expression
312
+ #
313
+ # Reference: {FaunaDB Read functions}[https://fauna.com/documentation/queries#read_functions]
314
+ def exists(ref, params = {})
315
+ Expr.new Expr.wrap_values(params).merge(exists: Expr.wrap(ref))
316
+ end
317
+
318
+ # :section: Write Functions
319
+
320
+ ##
321
+ # A create expression
322
+ #
323
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
324
+ def create(class_ref, params)
325
+ Expr.new create: Expr.wrap(class_ref), params: Expr.wrap(params)
326
+ end
327
+
328
+ ##
329
+ # An update expression
330
+ #
331
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
332
+ def update(ref, params)
333
+ Expr.new update: Expr.wrap(ref), params: Expr.wrap(params)
334
+ end
335
+
336
+ ##
337
+ # A replace expression
338
+ #
339
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
340
+ def replace(ref, params)
341
+ Expr.new replace: Expr.wrap(ref), params: Expr.wrap(params)
342
+ end
343
+
344
+ ##
345
+ # A delete expression
346
+ #
347
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
348
+ def delete(ref)
349
+ Expr.new delete: Expr.wrap(ref)
350
+ end
351
+
352
+ ##
353
+ # An insert expression
354
+ #
355
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
356
+ def insert(ref, ts, action, params)
357
+ Expr.new insert: Expr.wrap(ref), ts: Expr.wrap(ts), action: Expr.wrap(action), params: Expr.wrap(params)
358
+ end
359
+
360
+ ##
361
+ # A remove expression
362
+ #
363
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
364
+ def remove(ref, ts, action)
365
+ Expr.new remove: Expr.wrap(ref), ts: Expr.wrap(ts), action: Expr.wrap(action)
366
+ end
367
+
368
+ ##
369
+ # A create class expression
370
+ #
371
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
372
+ def create_class(params)
373
+ Expr.new create_class: Expr.wrap(params)
374
+ end
375
+
376
+ ##
377
+ # A create index expression
378
+ #
379
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
380
+ def create_index(params)
381
+ Expr.new create_index: Expr.wrap(params)
382
+ end
383
+
384
+ ##
385
+ # A create database expression
386
+ #
387
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
388
+ def create_database(params)
389
+ Expr.new create_database: Expr.wrap(params)
390
+ end
391
+
392
+ ##
393
+ # A create key expression
394
+ #
395
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
396
+ def create_key(params)
397
+ Expr.new create_key: Expr.wrap(params)
398
+ end
399
+
400
+ ##
401
+ # A create function expression
402
+ #
403
+ # Reference: {FaunaDB Write functions}[https://fauna.com/documentation/queries#write_functions]
404
+ def create_function(params)
405
+ Expr.new create_function: Expr.wrap(params)
406
+ end
407
+
408
+ # :section: Set Functions
409
+
410
+ ##
411
+ # A singleton expression
412
+ #
413
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
414
+ def singleton(ref)
415
+ Expr.new singleton: Expr.wrap(ref)
416
+ end
417
+
418
+ ##
419
+ # An events expression
420
+ #
421
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
422
+ def events(ref_set)
423
+ Expr.new events: Expr.wrap(ref_set)
424
+ end
425
+
426
+ ##
427
+ # A match expression
428
+ #
429
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
430
+ def match(index, *terms)
431
+ Expr.new match: Expr.wrap(index), terms: Expr.wrap_varargs(terms)
432
+ end
433
+
434
+ ##
435
+ # A union expression
436
+ #
437
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
438
+ def union(*sets)
439
+ Expr.new union: Expr.wrap_varargs(sets)
440
+ end
441
+
442
+ ##
443
+ # An intersection expression
444
+ #
445
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
446
+ def intersection(*sets)
447
+ Expr.new intersection: Expr.wrap_varargs(sets)
448
+ end
449
+
450
+ ##
451
+ # A difference expression
452
+ #
453
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
454
+ def difference(*sets)
455
+ Expr.new difference: Expr.wrap_varargs(sets)
456
+ end
457
+
458
+ ##
459
+ # A distinct expression
460
+ #
461
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
462
+ def distinct(set)
463
+ Expr.new distinct: Expr.wrap(set)
464
+ end
465
+
466
+ ##
467
+ # A join expression
468
+ #
469
+ # Only one of +target_expr+ or +target_block+ should be provided.
470
+ # For example: <code>Fauna.query { join(source) { |x| match some_index, x } }</code>.
471
+ #
472
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
473
+ def join(source, target_expr = nil, &target_block)
474
+ Expr.new join: Expr.wrap(source), with: Expr.wrap(target_expr || target_block)
475
+ end
476
+
477
+ # :section: Authentication Functions
478
+
479
+ ##
480
+ # A login function
481
+ #
482
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
483
+ def login(ref, params)
484
+ Expr.new login: Expr.wrap(ref), params: Expr.wrap(params)
485
+ end
486
+
487
+ ##
488
+ # A logout function
489
+ #
490
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
491
+ def logout(all_tokens)
492
+ Expr.new logout: Expr.wrap(all_tokens)
493
+ end
494
+
495
+ ##
496
+ # An identify function
497
+ #
498
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
499
+ def identify(ref, password)
500
+ Expr.new identify: Expr.wrap(ref), password: Expr.wrap(password)
501
+ end
502
+
503
+ ##
504
+ # An identity function
505
+ #
506
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
507
+ def identity
508
+ Expr.new identity: nil
509
+ end
510
+
511
+ ##
512
+ # A has_identity function
513
+ #
514
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
515
+ def has_identity
516
+ Expr.new has_identity: nil
517
+ end
518
+
519
+ # :section: String Functions
520
+
521
+ ##
522
+ # A concat function
523
+ #
524
+ # Reference: {FaunaDB String Functions}[https://fauna.com/documentation/queries#string_functions]
525
+ def concat(strings, separator = nil)
526
+ if separator.nil?
527
+ Expr.new concat: Expr.wrap(strings)
528
+ else
529
+ Expr.new concat: Expr.wrap(strings), separator: Expr.wrap(separator)
530
+ end
531
+ end
532
+
533
+ ##
534
+ # A casefold function
535
+ #
536
+ # Reference: {FaunaDB String Functions}[https://fauna.com/documentation/queries#string_functions]
537
+ def casefold(string, normalizer = nil)
538
+ if normalizer.nil?
539
+ Expr.new casefold: Expr.wrap(string)
540
+ else
541
+ Expr.new casefold: Expr.wrap(string), normalizer: Expr.wrap(normalizer)
542
+ end
543
+ end
544
+
545
+ ##
546
+ # A ngram function
547
+ #
548
+ # Reference: {FaunaDB String Functions}[https://fauna.com/documentation/queries#string_functions]
549
+ def ngram(terms, params = {})
550
+ Expr.new Expr.wrap_values(params).merge(ngram: Expr.wrap(terms))
551
+ end
552
+
553
+ # :section: Time and Date Functions
554
+
555
+ ##
556
+ # A time expression
557
+ #
558
+ # Reference: {FaunaDB Time Functions}[https://fauna.com/documentation/queries#time_functions]
559
+ def time(string)
560
+ Expr.new time: Expr.wrap(string)
561
+ end
562
+
563
+ ##
564
+ # An epoch expression
565
+ #
566
+ # Reference: {FaunaDB Time Functions}[https://fauna.com/documentation/queries#time_functions]
567
+ def epoch(number, unit)
568
+ Expr.new epoch: Expr.wrap(number), unit: Expr.wrap(unit)
569
+ end
570
+
571
+ ##
572
+ # A date expression
573
+ #
574
+ # Reference: {FaunaDB Time Functions}[https://fauna.com/documentation/queries#time_functions]
575
+ def date(string)
576
+ Expr.new date: Expr.wrap(string)
577
+ end
578
+
579
+ # :section: Miscellaneous Functions
580
+
581
+ ##
582
+ # A next_id function
583
+ #
584
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
585
+ def next_id
586
+ Expr.new next_id: nil
587
+ end
588
+
589
+ deprecate :next_id, :new_id
590
+
591
+ ##
592
+ # A new_id function
593
+ #
594
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
595
+ def new_id
596
+ Expr.new new_id: nil
597
+ end
598
+
599
+ ##
600
+ # A database function
601
+ #
602
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
603
+ def database(name, scope = nil)
604
+ return Expr.new database: Expr.wrap(name) if scope.nil?
605
+
606
+ Expr.new database: Expr.wrap(name), scope: Expr.wrap(scope)
607
+ end
608
+
609
+ ##
610
+ # A class function
611
+ #
612
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
613
+ def class_(name, scope = nil)
614
+ return Expr.new class: Expr.wrap(name) if scope.nil?
615
+
616
+ Expr.new class: Expr.wrap(name), scope: Expr.wrap(scope)
617
+ end
618
+
619
+ ##
620
+ # An index function
621
+ #
622
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
623
+ def index(name, scope = nil)
624
+ return Expr.new index: Expr.wrap(name) if scope.nil?
625
+
626
+ Expr.new index: Expr.wrap(name), scope: Expr.wrap(scope)
627
+ end
628
+
629
+ ##
630
+ # A function function
631
+ #
632
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
633
+ def function(name, scope = nil)
634
+ return Expr.new function: Expr.wrap(name) if scope.nil?
635
+
636
+ Expr.new function: Expr.wrap(name), scope: Expr.wrap(scope)
637
+ end
638
+
639
+ ##
640
+ # A databases function
641
+ #
642
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
643
+ def databases(scope = nil)
644
+ Expr.new databases: Expr.wrap(scope)
645
+ end
646
+
647
+ ##
648
+ # A classes function
649
+ #
650
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
651
+ def classes(scope = nil)
652
+ Expr.new classes: Expr.wrap(scope)
653
+ end
654
+
655
+ ##
656
+ # An indexes function
657
+ #
658
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
659
+ def indexes(scope = nil)
660
+ Expr.new indexes: Expr.wrap(scope)
661
+ end
662
+
663
+ ##
664
+ # A functions function
665
+ #
666
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
667
+ def functions(scope = nil)
668
+ Expr.new functions: Expr.wrap(scope)
669
+ end
670
+
671
+ ##
672
+ # A tokens function
673
+ #
674
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
675
+ def tokens(scope = nil)
676
+ Expr.new tokens: Expr.wrap(scope)
677
+ end
678
+
679
+ ##
680
+ # A keys function
681
+ #
682
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
683
+ def keys(scope = nil)
684
+ Expr.new keys: Expr.wrap(scope)
685
+ end
686
+
687
+ ##
688
+ # A credentials function
689
+ #
690
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
691
+ def credentials(scope = nil)
692
+ Expr.new credentials: Expr.wrap(scope)
693
+ end
694
+
695
+ ##
696
+ # An equals function
697
+ #
698
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
699
+ def equals(*values)
700
+ Expr.new equals: Expr.wrap_varargs(values)
701
+ end
702
+
703
+ ##
704
+ # A contains function
705
+ #
706
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
707
+ def contains(path, in_)
708
+ Expr.new contains: Expr.wrap(path), in: Expr.wrap(in_)
709
+ end
710
+
711
+ ##
712
+ # A select function
713
+ #
714
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
715
+ def select(path, from, params = {})
716
+ Expr.new Expr.wrap_values(params).merge(select: Expr.wrap(path), from: Expr.wrap(from))
717
+ end
718
+
719
+ ##
720
+ # A select_all function
721
+ #
722
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
723
+ def select_all(path, from)
724
+ Expr.new select_all: Expr.wrap(path), from: Expr.wrap(from)
725
+ end
726
+
727
+ ##
728
+ # An add function
729
+ #
730
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
731
+ def add(*numbers)
732
+ Expr.new add: Expr.wrap_varargs(numbers)
733
+ end
734
+
735
+ ##
736
+ # A multiply function
737
+ #
738
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
739
+ def multiply(*numbers)
740
+ Expr.new multiply: Expr.wrap_varargs(numbers)
741
+ end
742
+
743
+ ##
744
+ # A subtract function
745
+ #
746
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
747
+ def subtract(*numbers)
748
+ Expr.new subtract: Expr.wrap_varargs(numbers)
749
+ end
750
+
751
+ ##
752
+ # A divide function
753
+ #
754
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
755
+ def divide(*numbers)
756
+ Expr.new divide: Expr.wrap_varargs(numbers)
757
+ end
758
+
759
+ ##
760
+ # A modulo function
761
+ #
762
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
763
+ def modulo(*numbers)
764
+ Expr.new modulo: Expr.wrap_varargs(numbers)
765
+ end
766
+
767
+ ##
768
+ # A less than function
769
+ #
770
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
771
+ def lt(*values)
772
+ Expr.new lt: Expr.wrap_varargs(values)
773
+ end
774
+
775
+ ##
776
+ # A less than or equal function
777
+ #
778
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
779
+ def lte(*values)
780
+ Expr.new lte: Expr.wrap_varargs(values)
781
+ end
782
+
783
+ ##
784
+ # A greater than function
785
+ #
786
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
787
+ def gt(*values)
788
+ Expr.new gt: Expr.wrap_varargs(values)
789
+ end
790
+
791
+ ##
792
+ # A greater than or equal function
793
+ #
794
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
795
+ def gte(*values)
796
+ Expr.new gte: Expr.wrap_varargs(values)
797
+ end
798
+
799
+ ##
800
+ # An and function
801
+ #
802
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
803
+ def and_(*booleans)
804
+ Expr.new and: Expr.wrap_varargs(booleans)
805
+ end
806
+
807
+ ##
808
+ # An or function
809
+ #
810
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
811
+ def or_(*booleans)
812
+ Expr.new or: Expr.wrap_varargs(booleans)
813
+ end
814
+
815
+ ##
816
+ # A not function
817
+ #
818
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
819
+ def not_(boolean)
820
+ Expr.new not: Expr.wrap(boolean)
821
+ end
822
+
823
+ ##
824
+ # Converts an expression to a string literal.
825
+ def to_string(expr)
826
+ Expr.new to_string: Expr.wrap(expr)
827
+ end
828
+
829
+ ##
830
+ # Converts an expression to a number literal.
831
+ def to_number(expr)
832
+ Expr.new to_number: Expr.wrap(expr)
833
+ end
834
+
835
+ ##
836
+ # Converts an expression to a time literal.
837
+ def to_time(expr)
838
+ Expr.new to_time: Expr.wrap(expr)
839
+ end
840
+
841
+ ##
842
+ # Converts an expression to a date literal.
843
+ def to_date(expr)
844
+ Expr.new to_date: Expr.wrap(expr)
845
+ end
846
+
847
+ class Expr # :nodoc:
848
+ attr_reader :raw
849
+
850
+ def initialize(obj)
851
+ @raw = obj
852
+ end
853
+
854
+ def to_s
855
+ "Expr(#{@raw})"
856
+ end
857
+
858
+ alias_method :inspect, :to_s
859
+
860
+ def ==(other)
861
+ return false unless other.is_a? Expr
862
+ raw == other.raw
863
+ end
864
+
865
+ alias_method :eql?, :==
866
+
867
+ def self.wrap(obj)
868
+ # Recursively wrap hashes and arrays
869
+ if obj.is_a? Hash
870
+ Expr.new object: wrap_values(obj)
871
+ elsif obj.is_a? Array
872
+ Expr.new obj.map { |v| wrap(v) }
873
+ # Pass through types the json serializer understands (this includes Expr)
874
+ elsif FaunaJson.serializable_types.any? { |type| obj.is_a? type }
875
+ obj
876
+ # Handle special cases
877
+ elsif obj.is_a? Proc
878
+ Query.lambda(&obj)
879
+ # Fall back to hash conversion
880
+ elsif obj.respond_to? :to_h
881
+ Expr.new object: wrap_values(obj.to_h)
882
+ elsif obj.respond_to? :to_hash
883
+ Expr.new object: wrap_values(obj.to_hash)
884
+ # Fail out
885
+ else
886
+ fail SerializationError.new(obj)
887
+ end
888
+ end
889
+
890
+ def self.wrap_values(obj)
891
+ obj.inject({}) { |h, (k, v)| h[k] = wrap(v); h }
892
+ end
893
+
894
+ def self.wrap_varargs(values)
895
+ wrap(values.length == 1 ? values[0] : values)
896
+ end
897
+ end
898
+ end
899
+ end