Module: Lev::Handler

Defined in:
lib/lev/handler.rb,
lib/lev/handler/error.rb,
lib/lev/handler/errors.rb,
lib/lev/handler/error_transferer.rb,
lib/lev/handler/error_translator.rb

Overview

Common methods for all handlers. Handlers are classes that are responsible for taking input data from a form or other widget and doing something with it.

All handlers must:

2) include this module ("include Lev::Handler")
3) implement the 'exec' method which takes no arguments and does the 
   work the handler is charged with
4) implement the 'authorized?' method which returns true iff the 
   caller is authorized to do what the handler is charged with

Handlers may:

1) implement the 'setup' method which runs before 'authorized?' and 'exec'.
   This method can do anything, and will likely include setting up some 
   instance objects based on the params.
2) Call the class method "paramify" to declare, cast, and validate parts of
   the params hash. The first argument to paramify is the key in params
   which points to a hash of params to be paramified.  The block passed to
   paramify looks just like the guts of an ActiveAttr model.  Examples:

   when the incoming params includes :search => {:type, :terms, :num_results}
   the Handler class would look like:

   class MyHandler
     include Lev::Handler

     paramify :search do
       attribute :search_type, type: String
       validates :search_type, presence: true,
                               inclusion: { in: %w(Name Username Any),
                                            message: "is not valid" }

       attribute :search_terms, type: String
       validates :search_terms, presence: true

       attribute :num_results, type: Integer
       validates :num_results, numericality: { only_integer: true,
                                         greater_than_or_equal_to: 0 }                               
     end

     def exec
       # By this time, if there were any errors the handler would have
       # already populated the errors object and returned.
       #
       # Paramify makes a 'search_params' attribute available through
       # which you can access the paramified params, e.g.
       x = search_params.num_results
       ...
     end
   end

All handler instance methods have the following available to them:

1) 'params' --  the params from the input
2) 'caller' --  the user submitting the input
3) 'errors' --  an object in which to store errors
4) 'results' -- a hash in which to store results for return to calling code

See the documentation for Lev::RoutineNesting about other requirements and capabilities of handler classes.

The handle methods take a hash of arguments.

caller: the calling user
params: the params object
request: the http request object

These arguments are optional or required depending on the implementation of the specific handler, i.e. if a handler wants to use the ‘caller’ method, it must have been supplied to the handle method.

Example:

class MyHandler
  include Lev::Handler
protected
  def authorized?
    # return true iff exec is allowed to be called, e.g. might
    # check the caller against the params
  def exec
    # do the work, add errors to errors object and results to the results hash as needed
  end
end

Defined Under Namespace

Modules: ClassMethods Classes: Error, ErrorTransferer, ErrorTranslator, Errors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



161
162
163
# File 'lib/lev/handler.rb', line 161

def errors
  @errors
end

Class Method Details

.included(base) ⇒ Object



91
92
93
94
95
96
# File 'lib/lev/handler.rb', line 91

def self.included(base)
  base.extend(ClassMethods)
  base.class_eval do
    include Lev::RoutineNesting
  end
end

Instance Method Details

#handle(options = {}) ⇒ Object Also known as: call



98
99
100
101
102
# File 'lib/lev/handler.rb', line 98

def handle(options={})
  in_transaction do
    handle_guts(options)
  end
end

#transfer_errors_from(source, param_group) ⇒ Object



157
158
159
# File 'lib/lev/handler.rb', line 157

def transfer_errors_from(source, param_group)
  ErrorTransferer.transfer(source, self, param_group)
end