Coder Social home page Coder Social logo

trainset's Introduction

TrainSet

TrainSet is a lightweight MVC framework inspired by Ruby on Rails. Also included is TapeDeck, a lightweight ORM inspired by ActiveRecord. TrainSet provides ControllerBase and Router, which provide a base controller class and basic routing capabilities respectively. TapeDeck provides TapeDeck which gives access to associations

TapeDeck

Classes that inherit TapeDeck have access to a number of ORM functions.

  • parameters can be passed into the initializer or update_attributes as an options hash
sol = OS.new(name: 'Solaris', vendor:'Sun', model:'proprietary',license:'CDDL')
sol.update_attributes(vendor:'Oracle',license:'OTN')
  • Models can optionally override valid? to provide input validation before inserting or updating to DB. The overriden function must return either true or false. Error messages can also be shoveled into errors.
def valid?
    if name.empty?
      errors['name'] = "Name cannot be blank"
      return false
    end
    true
end
  • The model.save method will automatically either INSERT or UPDATE as needed.
  • The following relations are provided:
    • belongs_to
    • has_many
    • has_one_through
    • has_many_through

Example Usage

class OS < TapeDeck
    #every model must finalize!
    self.finalize!

    belongs_to :vendor
    belongs_to :OS_Family,
      foreign_key: :os_family_id
    has_many_through :similar_operating_systems,
      through: :OS_Family,
      source: :operating_systems
end

Models are stores in the $project_dir/models/ folder

TrainSet

Key Features

  • render(template name) : will render the template located in $project_dir/views/$controller_name/ directory
  • render_content(content, type): Will render custom content, such as JSON
render_content(data.to_json, "application/json")
  • redirect_to(url)
  • session: stores key/value pairs in the browser cookie.
  • flash and flash.now: stores single-session cookies and same-session data respectively.

Example Usage

class OSController < TrainSet

  def new
    @os = OS.new
    render :new
  end

  def create
    @os = OS.new(params['OS'])
    if @os.save
      redirect_to "/os/#{os.id}"
    else
      flash.now[:errors]=@os.errors
      render :new
    end
  end

end

Controllers are stored in the $project_dir/controllers/ folder using snake_case naming.

Router

TrainSet includes a lightweight router that can handle custom routes.

Example Usage

get Regexp.new("^/os$"), OSController, :index
get Regexp.new('^/os/(?<id>\\d+)/edit$'), OSController, :edit
put Regexp.new('^/os/(?<id>\\d+)$'), OSController, :update
get Regexp.new('^/os/(?<id>\\d+)$'), OSController, :show
get Regexp.new("^/os/new$"), OSController, :new
post Regexp.new("^/os$"), OSController, :create

Routes are added to the router.draw do block in the $project_dir/config/routes.rb file

Additional Middleware

A static assets middleware is provided. It will serve out of the public folder. By default it will recognize txt, jpg, png, and zip files. Additional MIME types can be added by appending to the MIME_TYPES constant in static.rb

Operation

  1. bundle install
  2. ruby ./bin/server.rb
  3. Visit http://localhost:3000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.