# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved. # License:: LGPL # $Id: symtbl.rb 567 2005-04-13 08:00:06Z polrop $ require 'symtbl' module TTK class SymTbl < SymTbl include Mutex_m def initialize ( father_env=nil, default=nil ) @frozen_keys = [] super end def [] ( aKey ) super end def []= ( k, v ) if frozen_key?(k) raise(TypeError, 'can\'t modify frozen key') else super end end def frozen_key? ( k ) @local.has_key?(k) and @frozen_keys.include?(k) end def freeze_key ( k ) unless frozen_key?(k) @frozen_keys << k self[k].freeze end end end # class SymTbl end # module TTK class Object def symtbl_gsub ( symtbl ) nil end def do_symtbl_gsub ( symtbl ) result = symtbl_gsub(symtbl) return (result.nil?)? self : result end end class Hash def symtbl_gsub ( symtbl ) changed = false res = self.class.new each do |k,v| new_k = k.symtbl_gsub(symtbl) new_v = v.symtbl_gsub(symtbl) changed = true if new_k or new_v res[new_k || k] = (new_v || v) end changed ? res : nil end end class Array def symtbl_gsub ( symtbl ) changed = false res = self.class.new each do |e| changed = true if new_e = e.symtbl_gsub(symtbl) res << (new_e || e) end changed ? res : nil end end class SymTbl def symtbl_gsub ( symtbl ) nil end end class Pathname def symtbl_gsub ( symtbl ) res = to_s.symtbl_gsub(symtbl) return self.class.new(res) unless res.nil? end end class Regexp def symtbl_gsub ( symtbl ) res = source.symtbl_gsub(symtbl) return self.class.new(res) unless res.nil? end end class Symbol def symtbl_gsub ( symtbl ) res = to_s.symtbl_gsub(symtbl) return res.to_sym unless res.nil? end end class String def symtbl_gsub ( symtbl ) return gsub(/<<(.*?)>>/) { symtbl[$1.to_sym] || $& } if self =~ /<<(.*)>>/ end end