module Lev::Handler::ClassMethods

Public Instance Methods

handle(options={}) click to toggle source
# File lib/lev/handler.rb, line 123
def handle(options={})
  call(options)
end
paramify(group = :paramify, options={}, &block) click to toggle source
# File lib/lev/handler.rb, line 127
def paramify(group = :paramify, options={}, &block)

  method_name = "#{group.to_s}_params"
  variable_sym = "@#{method_name}".to_sym

  # Generate the dynamic ActiveAttr class given
  # the paramify block; I think the caching of the class
  # in paramify_classes is only necessary to maintain
  # the name of the class set in the const_set statement

  if paramify_classes[group].nil?
    paramify_classes[group] = Class.new(Lev::Paramifier) do
      cattr_accessor :group
    end

    # Attach a name to this dynamic class
    const_set("#{group.to_s.capitalize}Paramifier",
              paramify_classes[group])

    paramify_classes[group].class_eval(&block)
    paramify_classes[group].group = group
  end

  # Define the "#{group}_params" method to get the paramifier
  # instance wrapping the params.  Choose the subset of params
  # based on the group, choosing all params if the default group
  # is used.

  define_method method_name.to_sym do
    if !instance_variable_get(variable_sym)
      params_subset = group == :paramify ? params : params[group]
      instance_variable_set(variable_sym,
                            self.class.paramify_classes[group].new(params_subset))
    end
    instance_variable_get(variable_sym)
  end

  # Keep track of the accessor for the params so we can check
  # errors in it later
  paramify_methods.push(method_name.to_sym)
end
paramify_classes() click to toggle source
# File lib/lev/handler.rb, line 173
def paramify_classes
  @paramify_classes ||= {}
end
paramify_methods() click to toggle source
# File lib/lev/handler.rb, line 169
def paramify_methods
  @paramify_methods ||= []
end