Sha256: d4ec292ae7a63031d92d4bb5a15ba8e298a9999ad2558150bbd968743ebc8a47

Contents?: true

Size: 752 Bytes

Versions: 23

Compression:

Stored size: 752 Bytes

Contents

module Hobo::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

23 entries across 23 versions & 1 rubygems

Version Path
hobo-1.0.3 lib/hobo/dryml/scoped_variables.rb
hobo-1.0.2 lib/hobo/dryml/scoped_variables.rb
hobo-1.0.1 lib/hobo/dryml/scoped_variables.rb
hobo-1.0.0 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.106 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.105 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.104 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.103 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.102 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.101 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.100 lib/hobo/dryml/scoped_variables.rb
hobo-0.9.0 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.10 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.9 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.8 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.5 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.1 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.2 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.3 lib/hobo/dryml/scoped_variables.rb
hobo-0.8.4 lib/hobo/dryml/scoped_variables.rb