Module: Lev::Handler::ClassMethods

Defined in:
lib/lev/handler.rb

Instance Method Summary collapse

Instance Method Details

#handle(options = {}) ⇒ Object



123
124
125
# File 'lib/lev/handler.rb', line 123

def handle(options={})
  call(options)
end

#paramify(group = :paramify, options = {}, &block) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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_classesObject



173
174
175
# File 'lib/lev/handler.rb', line 173

def paramify_classes
  @paramify_classes ||= {}
end

#paramify_methodsObject



169
170
171
# File 'lib/lev/handler.rb', line 169

def paramify_methods
  @paramify_methods ||= []
end