Sha256: 5031280ec001832eee4f5d54af7ef85537f05cfe3e68361d614339c51a3b4814

Contents?: true

Size: 935 Bytes

Versions: 4

Compression:

Stored size: 935 Bytes

Contents

module Happy
  module Extras

    # A Rails-like controller that dispatches to individual actions
    # named in the URL.
    #
    # The controller's root URL will call the `index` method, any sub-path
    # will call the method by that name. If a third path is provided, it
    # will be assigned to params['id'].
    #
    # /         # index
    # /foo      # foo
    # /foo/123  # foo (with params['id'] set to 123)
    #
    class ActionController < Happy::Controller
      def route
        on :action do
          on :id do
            dispatch_to_method params['action']
          end

          dispatch_to_method params['action']
        end

        index
      end

      protected

      def dispatch_to_method(name)
        # Only dispatch to public methods
        if public_methods(false).include?(name.to_sym)
          send name
        else
          raise Errors::NotFound
        end
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
happy-0.1.0 lib/happy/extras/action_controller.rb
happy-0.1.0.pre28 lib/happy/extras/action_controller.rb
happy-0.1.0.pre27 lib/happy/extras/action_controller.rb
happy-0.1.0.pre25 lib/happy/extras/action_controller.rb