Sha256: 5ce59df38924343076c39a5e66dc9da567b87ce7e5ba12a967e9b4fa99c85e67

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

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)
          private_api << define_method(o[name], &value)
        else
          private_api << define_method(o[name]) {value}
        end
      end.freeze
      private_api << 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rocketio-0.1.0 lib/rocketio/controller/render/template_vars.rb