Sha256: bd87c714716846e2c18b729ae4b3e471d4332fdd58858278c523cfcd3d55dea0

Contents?: true

Size: 951 Bytes

Versions: 2

Compression:

Stored size: 951 Bytes

Contents

module Contextuality
  class Context
    def initialize
      @scopes = []
    end

    def scope_inspect scope
      scope.map do |name, value|
        "#{name}: #{value.inspect}"
      end.to_a.join(', ')
    end

    def push variables
      @scopes.unshift Hash[variables.map { |(name, variable)| [name.to_s, variable] }]
      Contextuality.log "#{'=' * @scopes.size} Entering scope: #{scope_inspect @scopes.first}"
    end

    def pop
      Contextuality.log "#{'=' * @scopes.size} Exiting scope"
      @scopes.shift
    end

    def [] name
      name = name.to_s
      scope = @scopes.detect { |scope| scope.key? name }
      scope ? scope[name] : Contextuality.defaults[name.to_sym]
    end

    def key? name
      name = name.to_s
      @scopes.any? { |scope| scope.key? name }
    end

    def empty?
      !@scopes.any? { |scope| !scope.empty? }
    end

    def method_missing method, *args, &block
      self[method]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contextuality-1.0.4 lib/contextuality/context.rb
contextuality-1.0.3 lib/contextuality/context.rb