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
50
# File 'lib/cel/macro.rb', line 45

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

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



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

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

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



59
60
61
62
63
64
# File 'lib/cel/macro.rb', line 59

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

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



66
67
68
69
70
# File 'lib/cel/macro.rb', line 66

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



72
73
74
75
76
# File 'lib/cel/macro.rb', line 72

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