ruby isn't just about rails

Post on 12-Sep-2014

9.077 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

A whirlwind tour of the Rails-inspired Ruby ecosystem which has been frantically innovating for the past several years.

TRANSCRIPT

Adam WigginsCodemash 2009

Ruby Isn’tJust About Rails

?

Ruby?

You can’t sell a platform

You can’t sell a platform

...without a killer app.

A programming language is a platform

isthe killer appfor Ruby

Came for Rails,stayed for Ruby

An explosion of Ruby projects in the past 2 years

Let’s take a tour!

Rails is:ORM - ActiveRecordWeb Layer - ActionPackTemplating - ERBTest Framework - Test::Unit

ORM

ActiveRecord

class CreatePosts < ActiveRecord::Migration create_table :posts do |t| t.string :title t.text :body t.datetime :created_at t.integer :post_id endend

$ rake db:migrate

class Post < ActiveRecord::Base has_many :commentsend

Post.find(:first, :conditions => [ "title = ?", "First post" ])

ORM

ActiveRecord

DataMapper

Sequel

Define schema in the code instead of the database

http://datamapper.org

class Post include DataMapper::Resource

property :id, Serial property :title, String property :body, Text property :created_at, DateTime

has n, :commentsend

DataMapper.auto_upgrade!

Post.first(:title => "First Post")

Access a database with just hashes - or map models

http://sequel.rubyforge.org

db = Sequel.connect('mysql://root@localhost/db')

db.create_table :posts do primary_key :id varchar :title text :body datetime :created_atend

db[:posts].filter(:title => "First Post").first

Web Layer

ActionPack

ActionController::Routing::Routes.draw do |map| map.resource :postsend

class PostsController < ApplicationController def create @post = Post.create! params redirect_to(@post) end

def show @post = Post.find(params[:id]) endend

Web Layer

ActionPack

Merb

Sinatra

“No code is faster than no code”

The hacker’s framework

http://merbivore.org

Merb::Router.prepare do |router| resource :postsend

class Posts < Merb::Controller def create @post = Post.create params redirect(url(:post, @post)) end

def show @post = Post.find(params[:id]) display @post endend

use_orm :datamapper use_test :rspec use_template_engine :erb

Merb 2.0 == Rails 3.0

“Exposed simplicity instead of hidden complexity”

The classy microframework for Ruby

http://sinatra.rubyforge.org

Sinatra

require 'rubygems'require 'sinatra'require 'lib/posts'

post '/posts' post = Post.create! params redirect "/posts/#{post.id}"end

get '/posts/:id' do @post = Post.find(params[:id]) erb :postend

Test Framework

Unit tests

TDDTest-Driven Development

BDDBehavior-Driven Development

http://behaviour-driven.org

TATFTTest All The Darn Time

Test Framework

Test::Unit

class PostTest < Test::Unit::TestCase

def test_simple_slug post = Post.new :title => "First Post" assert_equal "first_post", post.slug end

def test_complex_slug post = Post.new :title => "My Post!") assert_equal "my_post", post.slug end

end

Behavior-Driven Development

http://rspec.info

RSpec

describe Post do

it "generates a url slug from the title" do post = Post.new :title => "First Post" post.slug.should == "first_post" end

it "drops punctuation from the url slug" do post = Post.new :title => "My Post!" post.slug.should == "my_post" end

end

$ rake spec:docPost- generates a url slug from the title- drops punctuation from the url slug

Templating Engine

ERB

<div class="post"> <h1><%= @post.title %></h1> <div class="body"> <%= @post.body %> </div></div>

Templating Engine

ERB

Haml

Markup haiku

http://haml.hamptoncatlin.com

Haml

.post %h1= @post.title .body= @post.body

Ties it all together

http://rack.rubyforge.org

The End.

Adam WigginsCodemash 2009

http://adam.blog.heroku.com

top related