Class: Bparity::Formal::ContractCompiler
- Inherits:
-
Object
- Object
- Bparity::Formal::ContractCompiler
- Defined in:
- lib/bparity/formal/contract.rb
Constant Summary collapse
- VARIABLES =
%i[result args kwargs pre_state post_state].freeze
- CONSTANTS =
{ "Array" => Array, "Hash" => Hash, "String" => String, "Integer" => Integer, "Float" => Float, "Symbol" => Symbol, "NilClass" => NilClass }.freeze
- CALLS =
%i[== != > >= < <= + - * / % [] size length nil? empty? is_a? between? match? start_with? end_with? include? uniq sort strip !].freeze
Instance Method Summary collapse
Instance Method Details
#compile(expression) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bparity/formal/contract.rb', line 14 def compile(expression) source = expression.gsub(/\breturn\b/, "result") prefix = VARIABLES.map { |name| "#{name} = nil" }.join("; ") parsed = Prism.parse("#{prefix}; #{source}") unless parsed.success? raise ConfigurationError, "Invalid contract expression: #{expression}. Fix the Spec Bundle." end node = parsed.value.statements.body.last validate!(node) ->(context) { evaluate(node, context.transform_keys(&:to_sym)) } end |