Module: Raw::Helpers

Defined in:
lib/raw/helper.rb

Overview

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

Defined Under Namespace

Modules: Utils

Class Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/raw/helper.rb', line 34

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("app/helper/" + mod.to_s)
          rescue LoadError #do not hide SyntaxError, NameError
            
            # you can do helper "foo" to load a module withouth requiring
            begin
              require("raw/helper/" + mod.to_s) 
            rescue LoadError 
              # Drink it!
            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("Raw::#{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