require 'erb' require 'yaml' class YamlHelp # default help files dir is app_root/help DEFAULT_HELP_FILES_DIR = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/help" : "../../../help" # w/o the controller_name all help files are loaded def initialize(controller_name = nil, help_files_dir = nil) @yaml_hash = {} help_files_dir ||= DEFAULT_HELP_FILES_DIR raise IOError, "Help files directory is not valid path -- #{help_files_dir}" unless File.exists? help_files_dir controller_name ||= '*' # all help files if none specified help_files_path = help_files_dir.concat("/#{controller_name}.yml") Dir[help_files_path].select {|f| true }.each do |file_path| @controller_name = File.basename(file_path, ".yml").to_sym @yaml_hash[@controller_name] = {} yaml_string = IO.read(file_path) if yaml = YAML::load(erb_render(yaml_string)) yaml.each do |name, data| # hash self[name] = data end end end end def []=(key,value) @yaml_hash[@controller_name].merge!({key.to_sym => value}) end def [](key) @yaml_hash[key] end private def erb_render(help_content) ERB.new(help_content).result end end