# 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 self[: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_Repository Thread.current["@@context"]=WarningHash.new() #returns context hastable. # #Context is used for saving any information usefull for ontomde, #such as current file being written, database persistence mode, ... # # Please refer to #WarningHash [] method for information on # how to access context content. # # Please refer to #mtk_context method for information on # how to populate context. def context return Thread.current["@@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 Thread.current["@@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=Thread.current["@@context"] Thread.current["@@context"]=context_save.clone additional_context.each { |item| item.each { |k,v| Thread.current["@@context"][k]=v }} yield Thread.current["@@context"]=context_save return nil end end module Mrdf_Resource # returns current context # #Context is used for saving any information usefull for ontomde, #such as current file being written, database persistence mode, ... # # # Please refer to #WarningHash [] method for information on # how to access context content. # # Please refer to #mtk_context method for information on # how to populate context. def context return @rdf_Repository.context end # cf: Mrdf_Repository::mtk_context def mtk_context(*args,&block) return @rdf_Repository.mtk_context(*args,&block) end # # cf: Mrdf_Repository::mtk_default_context def mtk_default_context(*args,&block) return @rdf_Repository.mtk_default_context(*args,&block) end end