require 'facet/kernel/constant' require 'facet/string/camelize' module Nitro # Helpers are standard Ruby modules that contain utility # methods. By using the special 'helper' # macro provided by Helpers, the utility methods are # included as private methods. # # The helper also requires the ruby file. # # === Examples # # class MyController < Nitro::Controller # helper :xhtml # == include Nitro::XhtmlHelper # end module Helpers module Utils #-- # gmosx: dont name constant to avoid loop. #++ def self.const(mod) return constant(mod) rescue NameError => ex return nil end end def self.append_features(base) super base.module_eval do def self.helper(*modules) for mod in modules # If the mod is a string or symbol, also try to # auto require the source file. #-- # gmosx, FIXME: temp solution, will fix! #++ if mod.is_a?(String) or mod.is_a?(Symbol) begin # gmosx: dont interpolate (RDoc fix). require('src/helper/' + mod.to_s) rescue LoadError #do not hide SyntaxError, NameError # you can do helper "foo" to load a module withouth requiring begin require('nitro/helper/' + mod.to_s) rescue LoadError # supress this error. end end end unless mod.is_a? Module modname = mod.to_s.camelize # gmosx: check xxxHelper before xxx. mod = Utils.const("#{modname}Helper") || Utils.const("Nitro::#{modname}Helper") || Utils.const(modname) end # name mismatch unless mod raise "helper #{modname} not found (module not defined), check name" end symbols = mod.instance_methods.collect { |m| m.to_sym } self.send(:include, mod) self.send(:private, *symbols) self.send(:private, mod.to_s[/[^:]+$/].to_sym) end end end end end end