An opinionated tiny MVC framework http://localhost:8080/
  • Ruby 67.6%
  • HTML 32.4%
Find a file
2014-10-19 17:45:03 +11:00
bin removed better_errors etc 2014-10-19 17:45:03 +11:00
lib added support for regex in routes, passing arguments to blocks. 2014-10-19 04:32:20 +11:00
static static files, helpers, decorators 2014-10-18 20:20:14 +10:30
.gitignore updates 2014-10-17 14:26:21 +11:00
Gemfile updates 2014-10-17 14:26:21 +11:00
LICENSE.txt updates 2014-10-17 14:26:21 +11:00
micromvc.gemspec removed better_errors etc 2014-10-19 17:45:03 +11:00
Rakefile updates 2014-10-17 14:26:21 +11:00
README.md updated readme 2014-10-18 23:48:59 +11:00

MicroMVC

A tiny toy opinionated MVC framework.

Controllers, Routing and Helpers

Controllers are placed in app/controllers/MyController.rb and subclass Micro::Controller.
Routes are defined inline on a controller, multiple controllers can fulfil a single root but cannote contain duplicate routes.

Helpers can be loaded and are available from both controllers and views by using helper MyHelperModule or helpers [MyHelperA, MyHelperB].

class MyController < Micro::Controller
  root '/'                    # this is the root path for this controller.

  helper MagicHelper          # Explicitly choose which helpers to import

  index do                    # CRUD actions are aliased for easy use.
    render 'Hello, World!'    # At the moment only text rendering.
  end
  
  get '/custom', :custom do   # Custom actions can be defined by http verbs.
    render 'A custom action!'
  end
end                           # FIN.

Models and Decorators

Models are placed in app/models/MyModel.rb and subclass Micro::Model. There are no hard restrictions on what a model can be at the moment, but they are loaded in the context of all controllers.

Models are able to import Decorators similar to how controllers import Helpers

class MyModel < Micro::Model 
  decorator FullNameDecorator
  
  def first_name
    "Ashley"
  end 
  
  def last_name
    "Towns"
  end
end

# model = MyModel.new
# model.first_name 
# model.full_name # from a decorator

Views, Layouts, Templating

Views can be in any language supported by Tilt by default only .erb support is supplied. Views share the scope with the controller similar to how Rails works so for example in your controller if you have

# app/controller/demo_controller.rb
def index do 
  @posts = Post.all
end

Then in your app/views/demo/index.erb you can do

<% @posts.each do |post| %>
...
<% end %>

Helpers imported on the controller are available to views.

A default layout can be placed in app/views/layouts/default.extension and can be overriden on a per-controller basis by using layout <layout name> inside a controller.

Public(Static) files

Static files are served from the public directory based on the type of file from a subdirectory public/js, public/css, public/fonts, public/images and referenced by those folders.

Usage

TODO: everything.

gem install micromvc
micro --init MyAwesomeBlog
cd MyAwesomeBlog
micro --server

Contributing

  1. Fork it ( https://github.com/[my-github-username]/micromvc/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request