Class: Cel::Environment
- Inherits:
-
Object
- Object
- Cel::Environment
- Defined in:
- lib/cel/environment.rb
Instance Attribute Summary collapse
-
#declarations ⇒ Object
readonly
Returns the value of attribute declarations.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Instance Method Summary collapse
- #check(expr) ⇒ Object
- #compile(expr) ⇒ Object
- #decode(encoded_expr) ⇒ Object
- #encode(expr) ⇒ Object
- #evaluate(expr, bindings = nil) ⇒ Object
-
#initialize(declarations: nil, container: nil, extensions: nil, disable_check: false, **kwargs) ⇒ Environment
constructor
A new instance of Environment.
- #process_bindings(bindings) ⇒ Object
- #program(expr) ⇒ Object
- #with(declarations:, disable_check: @disable_check) ⇒ Object
Constructor Details
#initialize(declarations: nil, container: nil, extensions: nil, disable_check: false, **kwargs) ⇒ Environment
Returns a new instance of Environment.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cel/environment.rb', line 7 def initialize( declarations: nil, container: nil, extensions: nil, disable_check: false, **kwargs ) @disable_check = disable_check @declarations = declarations @container = container @extensions = extensions ? EXTENSIONS.merge(extensions) : EXTENSIONS @package = nil @checker = Checker.new(self) if container && !container.empty? module_dir = container.split(".") Dir[File.join(*module_dir, "*.rb")].each do |path| require path end proto_module_name = container.split(".").map do |sub| sub.capitalize.gsub(/_([a-z\d]*)/) do ::Regexp.last_match(1).capitalize end end.join("::") @package = Object.const_get(proto_module_name) if Object.const_defined?(proto_module_name) end @parser = Parser.new(package, **kwargs) end |
Instance Attribute Details
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
5 6 7 |
# File 'lib/cel/environment.rb', line 5 def declarations @declarations end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
5 6 7 |
# File 'lib/cel/environment.rb', line 5 def extensions @extensions end |
#package ⇒ Object (readonly)
Returns the value of attribute package.
5 6 7 |
# File 'lib/cel/environment.rb', line 5 def package @package end |
Instance Method Details
#check(expr) ⇒ Object
57 58 59 60 |
# File 'lib/cel/environment.rb', line 57 def check(expr) expr = @parser.parse(expr) if expr.is_a?(::String) @checker.check(expr) end |
#compile(expr) ⇒ Object
41 42 43 44 45 |
# File 'lib/cel/environment.rb', line 41 def compile(expr) ast = @parser.parse(expr) @checker.check(ast) ast end |
#decode(encoded_expr) ⇒ Object
51 52 53 54 55 |
# File 'lib/cel/environment.rb', line 51 def decode(encoded_expr) ast = Encoder.decode(encoded_expr) @checker.check(ast) unless @disable_check ast end |
#encode(expr) ⇒ Object
47 48 49 |
# File 'lib/cel/environment.rb', line 47 def encode(expr) Encoder.encode(compile(expr)) end |
#evaluate(expr, bindings = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/cel/environment.rb', line 68 def evaluate(expr, bindings = nil) declarations, bindings = process_bindings(bindings) context = Context.new(self, bindings) expr = @parser.parse(expr) if expr.is_a?(::String) @checker.check(expr) unless @disable_check with(declarations: declarations) do Program.new(context, self).evaluate(expr) end end |
#process_bindings(bindings) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cel/environment.rb', line 78 def process_bindings(bindings) return [@declarations, bindings] unless @container && @package.nil? declarations = @declarations.dup bindings = bindings.dup prefix = "#{@container}." cbinding_keys = bindings.keys.select { |k| k.start_with?(prefix) } cbinding_keys.each do |k| cbinding = k.to_s.delete_prefix(prefix).to_sym bindings[cbinding] = bindings.delete(k) declarations[cbinding] = declarations.delete(k) if declarations.key?(k) end [declarations, bindings] end |
#program(expr) ⇒ Object
62 63 64 65 66 |
# File 'lib/cel/environment.rb', line 62 def program(expr) expr = @parser.parse(expr) if expr.is_a?(::String) @checker.check(expr) unless @disable_check Runner.new(self, expr) end |
#with(declarations:, disable_check: @disable_check) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cel/environment.rb', line 98 def with(declarations:, disable_check: @disable_check) prev_declarations = @declarations prev_checker = @checker prev_disable_check = @disable_check @declarations = prev_declarations ? prev_declarations.merge(declarations) : declarations @checker = prev_checker.merge(@declarations) @disable_check = disable_check yield ensure @declarations = prev_declarations @checker = prev_checker @disable_check = prev_disable_check end |