Module: Cel::Macro

Defined in:
lib/cel/macro.rb

Class Method Summary collapse

Class Method Details

.all(collection, identifier, predicate, context:) ⇒ Object



45
46
47
48
49
# File 'lib/cel/macro.rb', line 45

def all(collection, identifier, predicate, context:)
  collection.all? do |element, *|
    Program.new(context.merge(identifier.to_sym => element)).evaluate(predicate).value
  end
end

.exists(collection, identifier, predicate, context:) ⇒ Object



51
52
53
54
55
# File 'lib/cel/macro.rb', line 51

def exists(collection, identifier, predicate, context:)
  collection.any? do |element, *|
    Program.new(context.merge(identifier.to_sym => element)).evaluate(predicate).value
  end
end

.exists_one(collection, identifier, predicate, context:) ⇒ Object



57
58
59
60
61
# File 'lib/cel/macro.rb', line 57

def exists_one(collection, identifier, predicate, context:)
  collection.one? do |element, *|
    Program.new(context.merge(identifier.to_sym => element)).evaluate(predicate).value
  end
end

.filter(collection, identifier, predicate, context:) ⇒ Object



63
64
65
66
67
# File 'lib/cel/macro.rb', line 63

def filter(collection, identifier, predicate, context:)
  collection.select do |element, *|
    Program.new(context.merge(identifier.to_sym => element)).evaluate(predicate).value
  end
end

.has(invoke) ⇒ Object

If e evaluates to a protocol buffers version 2 message and f is a defined field:

If f is a repeated field or map field, has(e.f) indicates whether the field is non-empty.
If f is a singular or oneof field, has(e.f) indicates whether the field is set.

If e evaluates to a protocol buffers version 3 message and f is a defined field:

If f is a repeated field or map field, has(e.f) indicates whether the field is non-empty.
If f is a oneof or singular message field, has(e.f) indicates whether the field is set.
If f is some other singular field, has(e.f) indicates whether the field's value is its default
  value (zero for numeric fields, false for booleans, empty for strings and bytes).


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cel/macro.rb', line 15

def has(invoke)
  var = invoke.var
  func = invoke.func

  case var
  when Message
    # If e evaluates to a message and f is not a declared field for the message,
    # has(e.f) raises a no_such_field error.
    raise NoSuchFieldError.new(var, func) unless var.field?(func)

    Bool.new(!var.public_send(func).nil?)
  when Map
    # If e evaluates to a map, then has(e.f) indicates whether the string f
    # is a key in the map (note that f must syntactically be an identifier).
    Bool.new(var.respond_to?(func))
  else
    # In all other cases, has(e.f) evaluates to an error.
    raise EvaluateError, "#{invoke} is not supported"
  end
end

.map(collection, identifier, predicate, context:) ⇒ Object



69
70
71
72
73
# File 'lib/cel/macro.rb', line 69

def map(collection, identifier, predicate, context:)
  collection.map do |element, *|
    Program.new(context.merge(identifier.to_sym => element)).evaluate(predicate)
  end
end

.matches(string, pattern) ⇒ Object



40
41
42
43
# File 'lib/cel/macro.rb', line 40

def matches(string, pattern)
  pattern = Regexp.new(pattern)
  Bool.new(pattern.match?(string))
end

.size(literal) ⇒ Object



36
37
38
# File 'lib/cel/macro.rb', line 36

def size(literal)
  literal.size
end