Sha256: 095357bfb4834e01a77101779cd00526be376a4922c6b64b9e1f301ccf531406

Contents?: true

Size: 553 Bytes

Versions: 3

Compression:

Stored size: 553 Bytes

Contents

module Bindy

  class Context

    attr_reader :variables

    def initialize(variables={})
      @variables = variables
    end

    def evaluate(expression)
      raise ArgumentError, "Expression must be a string (#{expression})" unless expression.kind_of?(String)
      Language.parse(expression).evaluate(self)
    end

    def var(name)
      name.split('.').inject(variables) do |vars, key| 
        vars.fetch(key) { vars.fetch(key.to_sym) }
      end
    rescue KeyError
      raise ArgumentError, "Undefined variable #{name}"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bindy-0.0.5 lib/bindy/context.rb
bindy-0.0.4 lib/bindy/context.rb
bindy-0.0.3 lib/bindy/context.rb