Sha256: 1d4f4211eb16a874fa30fa3fff1fe7c23c3841d35b6785a5a1a5aa578b1899f0

Contents?: true

Size: 746 Bytes

Versions: 67

Compression:

Stored size: 746 Bytes

Contents

module Dryml

  class ScopedVariables

    def initialize(variables=nil)
      @scopes = variables ? [variables] : []
    end

    def [](key)
     s = scope_with_key(key) and s[key]
    end

    def []=(key, val)
      s = scope_with_key(key) or raise ArgumentError, "no such scoped variable: #{key}"
      s[key] = val
    end

    def new_scope(variables)
      @scopes << variables.dup
      res = yield
      @scopes.pop
      res
    end

    def scope_with_key(key)
      @scopes.reverse_each do |s|
        return s if s.has_key?(key)
      end
      nil
    end

    def method_missing(name, *args)
      if name.to_s =~ /=$/
        self[name.to_s[0..-2].to_sym] = args.first
      else
        self[name]
      end
    end

  end

end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
dryml-2.0.0.pre7 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre6 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre5 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre4 lib/dryml/scoped_variables.rb
dryml-1.3.2 lib/dryml/scoped_variables.rb
dryml-1.3.1 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre3 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre2 lib/dryml/scoped_variables.rb
dryml-2.0.0.pre1 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre8 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre7 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre6 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre5 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre4 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre3 lib/dryml/scoped_variables.rb
dryml-1.4.0.pre2 lib/dryml/scoped_variables.rb
dryml-1.1.0 lib/dryml/scoped_variables.rb
dryml-1.3.0 lib/dryml/scoped_variables.rb
dryml-1.3.0.RC4 lib/dryml/scoped_variables.rb
dryml-1.3.0.RC3 lib/dryml/scoped_variables.rb