# Context management require 'fileutils' #require 'ontomde-core/exceptions.rb' #Internal use. # #Used for storing context. class WarningHash < Hash #Returns context value associated to key. #Raises an error if key is not found. # Returns default, when context is undefined for key, and a default value is provided. #if value is missing, key is return when context[:contextReturnKeyIfEmpty] is true. (feature used by web inspector) def [](key,default=Object.class) val=super(key) return val unless val.nil? #Note: Object.class is used as a magic number. return default unless default==Object.class return "#{key}" if context[:contextReturnKeyIfEmpty,false] raise Warning.new(), <'valeur'){ votre code puts context[:#{key}] votre code } FINCODE end def get(key) return self[key] if self.include?(key) return nil end end module Mrdf_Model @@context=WarningHash.new() def context return @@context end # Similar to mtk_context but preserves already defined context. # Used to provide a default context. def mtk_default_context(*additional_context,&block) filtered_context=Array.new additional_context.each { |item| item.each { |k,v| next if @@context.include?(k) filtered_context << item }} mtk_context(*filtered_context,&block) end #Defines context variables for use in block passed as parameter. #Existing context variable are redefined. (cf. mtk_default_context ) #When exiting block, previously defined context variables are restored. # #Example 1: # mtk_context( :build => "build\", :option => true) { # your-code # puts context[:build] # your-code # puts context[:option] # your-code # } # #Example 2: # mtk_context( :build => "BUILD_ONE\") { # your-code # puts context[:build] # BUILD_ONE # mtk_context( :build => "NEW_DIR", :option => true) { # your-code # puts context[:build] # NEW_DIR\ # your-code # puts context[:option] # your-code # } # puts context[:build] # BUILD_ONE # } def mtk_context(*additional_context,&block) context_save=@@context @@context=context_save.clone additional_context.each { |item| item.each { |k,v| @@context[k]=v }} yield @@context=context_save return nil end end module Mrdf_Resource # returns current context def context return @rdf_model.context end # cf: Mrdf_Model::mtk_context def mtk_context(*args,&block) return @rdf_model.mtk_context(*args,&block) end # # cf: Mrdf_Model::mtk_default_context def mtk_default_context(*args,&block) return @rdf_model.mtk_default_context(*args,&block) end end