Sha256: 10212fc8671e3fa720b45d4cb242e4cc48b65ecf94c8dd3862f5a925e8f889e8

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# SimpleController

Use the Ruby on Rails Controller pattern outside of the Rails request stack.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'simple_controller'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install simple_controller

## Usage

```ruby
class UserController < SimpleController::Base
    before_action do
        @user = User.find(params[:user_id])
    end

    def touch
        @user.touch
        @user
    end
end

UserController.call(:touch, user_id: 1) # => returns User object
UserController.new.call(:touch, user_id: 1) # => same as above
```

It works like a Rails Controller, but has only has the following features:
- Callbacks
- `params`
- `action_name`

## Router
A router is provided to decouple controller classes from identifiers.

```ruby
class Router < SimpleController::Router
end

# Router.instance is a singleton for ease of use
Router.instance.draw do
  match "threes/multiply"
  match "threes/dividing" => "threes#divide"

  controller :threes do
    match :add
    match subtracting: "subtract"
  end
  # custom syntax
  controller :threes, actions: %i[power]
  
  namespace :some_namespace do
    match :magic
  end
  
  # no other Rails-like syntax is available
end

Router.call("threes/multiply", number: 6) # calls ThreesController.call(:multiply, number: 6)
Router.instance.call("threes/multiply", number: 6) # same as above
```

To custom namespace the controller:
```ruby
Router.instance.parse_controller_name {|controller_name| "#{controller_name}_suffix_controller".classify.constantize }

Router.call("threes/multiply", number: 6) # calls ThreesSuffixController.call(:multiply, number: 6)
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_controller-0.1.1 README.md