module RocketIO class Controller # define variables to be used on templates alongside provided locals. # # @param name # @param value # def self.define_template_var name, value = nil, &block value || block || raise(ArgumentError, 'A value or a block expected') (@__template_vars__ ||= {})[name.to_sym] = (block || value).freeze define_template_vars_methods end def self.define_template_vars_methods source = self return unless source.instance_variables.include?(:@__template_vars__) vars = source.instance_variable_get(:@__template_vars__).each_with_object(allocate.__template_vars__.dup) do |(name,value),o| o[name] = :"__#{name}_template_var__" if value.is_a?(Proc) define_method(o[name], &value) else define_method(o[name]) {value} end end.freeze define_method(:__template_vars__) {vars} end def __template_vars__; RocketIO::EMPTY_HASH end def template_vars __template_vars__.each_with_object({}) {|(k,v),o| o[k] = __send__(v)} end end end